基本适配器 - 空堆栈异常?

Base adapter - empty stack exception?

即使堆栈不为空,我仍然在 MyAdapter.java 中得到一个 EmptyStackException(见下文)。

我检查并仔细检查了所有内容,我似乎无法弄清楚。如果有任何帮助,我将不胜感激。

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>

您不只是弹出堆栈一次。

每个单行项目都弹出堆栈,虽然您可能有两个项目,但如果 getView 被调用两次以上我也不会感到惊讶

尝试使用列表的列表,或者 Map<String, List<String>> 如果您正在寻找 ExpandableListView