Arraylist <string> ArrayAdaptor在一个ListView中显示2个Arraylist <string> </string> </string>

时间:2014-07-17 19:16:24

标签: android listview arraylist android-arrayadapter

我有两个ArrayList<String>,我想在一个ListView内按顺序并排显示它们。

我已设置自定义&#34; row&#34;,ListView,并了解如何list.setAdapter(myAdapter)

我的问题是myAdaptor无效。

如何解决此问题ArrayAdaptor以显示两个ArrayList<String>

CustomArrayAdaptor.java

public class CustomArrayAdapter extends ArrayAdapter<Words> {
    private ArrayList<Words> arrayOne;
    private ArrayList<Words> arrayTwo;

    public CustomArrayAdapter(Context context, int textViewResourceId, ArrayList<Words> arrayOne, ArrayList<Words> arrayTwo) {
        super(context, textViewResourceId, arrayOne, arrayTwo);
        this.arrayOne = arrayOne;
        this.generates = arrayTwo;
    }

    public View getView(int position, View convertView, ViewGroup parent){
        View v = convertView;
        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.list_item, null);
        }

        Words a = arrayOne.get(position);
        Words g = arrayTwo.get(position);

        if (a != null) {
            TextView arrayOne = (TextView) v.findViewById(R.id.text_view_1);
            TextView arrayTwo = (TextView) v.findViewById(R.id.text_view_2);
        }

        return v;
    }
}

2 个答案:

答案 0 :(得分:0)

来自我的评论:

你实际上从未在适配器的getView方法中设置TextViews arrayOne和arrayTwo上的文本,这解释了为什么你没有看到任何东西。此外,您的适配器中不需要2个ArrayList,因为您的Word对象从您的ArrayList(我假设)中保存对Strings的引用

这是经过调整的适配器符合我原来的建议:

public class CustomArrayAdapter extends ArrayAdapter<Words> {

    private ArrayList<Words> arrayOne;

    public CustomArrayAdapter(Context context, int textViewResourceId, ArrayList<Words> arrayOne) {
        super(context, textViewResourceId, arrayOne);
        this.arrayOne = arrayOne;
    }

    public View getView(int position, View convertView, ViewGroup parent){
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_item, null);
        }

        Words a = arrayOne.get(position);
        if (a != null) {
            TextView text1 = (TextView) convertView.findViewById(R.id.text_view_1);
            text1.setText(a.getWordOne());

            TextView text2 = (TextView) convertView.findViewById(R.id.text_view_2);
            text2.setText(a.getWordTwo());
        }

        return convertView;
    }
}

我还建议查看View Holder Pattern的效率,这样你就不会总是在每个布局传递上调用findViewById()。

答案 1 :(得分:0)

首先将这些内容粘贴在2行以下&#34;如果阻止&#34; of(v == null)

    TextView arrayOne = (TextView) v.findViewById(R.id.text_view_1);
    TextView arrayTwo = (TextView) v.findViewById(R.id.text_view_2);

实际上您还没有为textview设置值。因此,您需要为这些文本视图设置值。

见下文:

    arrayOne.setText("" + arrayOne.get(position));
    arrayTwo.setText("" + arrayTwo.get(position));

只需在getView方法中复制粘贴这两行。你的问题将得到解决。