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

时间:2017-06-09 01:02:05

标签: java android listview baseadapter

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

我检查并仔细检查了一切,我似乎无法弄明白。任何帮助都会非常感激。

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>

1 个答案:

答案 0 :(得分:0)

你不只是弹出一次堆栈。

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

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