I keep getting an EmptyStackException
in MyAdapter.java
(see below) even though the stack is not empty.
I checked and double checked everything, I can't seem to figure it out. Any help would me much appreciated.
MainActivity.java
package test.testapp;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Stack;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv = (ListView)findViewById(R.id.list);
ArrayList<Stack<String>> list = new ArrayList<>();
Stack one = new Stack();
one.push("a");
one.push("b");
list.add(one);
Stack two = new Stack();
two.push("c");
two.push("d");
list.add(two);
lv.setAdapter(new MyAdapter(this,list));
}
}
MyAdapter.java
package test.testapp;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Stack;
public class MyAdapter extends BaseAdapter {
private Context context;
private ArrayList<Stack<String>> list;
public MyAdapter(Context context, ArrayList<Stack<String>> list){
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Stack<String> stack = (Stack) getItem(position);
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout row = (LinearLayout) inflater.inflate(R.layout.row,parent,false);
((TextView)row.findViewById(R.id.tv1)).setText(stack.pop()); // <--- Empty stack exception here
((TextView)row.findViewById(R.id.tv1)).setText(stack.pop());
return row;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="test.testapp.MainActivity">
<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text 1"/>
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text 2"/>
</LinearLayout>
You're not just popping the stack once.
Every single row item is popping the stack, and although you may have two items, I wouldn't be surprised if getView
is being called more than twice
Try using a List of Lists, or a Map<String, List<String>>
if you're looking for a ExpandableListView
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.