lundi 2 mars 2015

Outsource Stack and Filereader from Main Class



Hello i'm a little bit lost and could use some help. So basically i got a stack of Objects, in this case Strings.



public class Showall extends ActionBarActivity implements OnClickListener {
Stapel<String> s = new EVLStapel<String>();


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

add();


setContentView(R.layout.activity_showall);
init();


}


and i'm running the add() method, which is just reading a textfile and pushes an object with 3 values into my stack & init() method, which is basically pulling them out of the stack again:



public void add(){
AssetManager mngr;
String line = null;


try{
mngr = getAssets();
InputStream is = mngr.open("ausgabe.txt");
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
while((line = br.readLine()) != null)
{
String[] parts = line.split(",");
s.push(parts[0], parts[1], parts[2]);





}
br.close();
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}


Here the important part of init():



while(s.isEmpty() == false)
{
TableRow row = new TableRow(this);
TextView p = new TextView(this);
p.setText(s.peek());

row.addView(p);

TextView sp = new TextView(this);
sp.setText(s.peek1());
row.addView(sp);

TextView pp = new TextView(this);
pp.setText(s.peek2());
row.addView(pp);
table.addView(row);
s.pop();
}


So now my problem. I want to outsource my stack and the add method because, i will be using this method in many different classes and would be much less work if i could make a separated class for my stack and my filereader. But i have no idea how. I tried something like:



public class Reader extends ActionBarActivity {
Stapel<String> s = new EVLStapel<String>();

public void add(){
AssetManager mctr;
String zeile = null;


try{
mctr = getAssets();
InputStream is = mctr.open("ausgabe.txt");
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
while((zeile = br.readLine()) != null)
{

String[] parts = zeile.split(",");
s.push(parts[0], parts[1], parts[2]);


zeile = br.readLine();



}
br.close();
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}




}
}


and change this in my main class



public class Showall extends ActionBarActivity implements OnClickListener {
Reader r = new Reader();
...
while(s.isEmpty() == false)
{
TableRow row = new TableRow(this);
TextView p = new TextView(this);
p.setText(r.s.peek());

row.addView(p);

TextView sp = new TextView(this);
sp.setText(r.s.peek1());
row.addView(sp);

TextView pp = new TextView(this);
pp.setText(r.s.peek2());
row.addView(pp);
table.addView(row);
r.s.pop();
}


Edit: EVLStapel code:



public class EVLStapel<T> implements Stapel<T>{
private class Item {
public Item next;
public T value;
public T value1;
public T value2;
}
private Item first;
private Item last;
private Item pre;

public boolean isEmpty() {
return first == null;
}

public void push(T e, T e1, T e2)
{
Item item = new Item () ;
item.next = null;
item.value = e ;
item.value1 = e1;
item.value2 = e2;
if (isEmpty()) {
first = item;
first.next = null;
}
else {
pre = first;
first = item;
first.next = pre;

}

}

public T peek() {

if (isEmpty()) {
throw new java.util.NoSuchElementException();
}


return first.value;
}
public T peek1() {

if (isEmpty()) {
throw new java.util.NoSuchElementException();
}


return first.value1;
}
public T peek2() {

if (isEmpty()) {
throw new java.util.NoSuchElementException();
}


return first.value2;
}
public void pop() {
if (isEmpty()) {
throw new java.util.NoSuchElementException();
}

first = first.next;



}

}



Aucun commentaire:

Enregistrer un commentaire