【问题标题】:Arraylist<String> ArrayAdaptor to display 2 Arraylist<String> in one ListViewArraylist<String> ArrayAdaptor 在一个 ListView 中显示 2 个 Arraylist<String>
【发布时间】:2014-09-08 19:21:48
【问题描述】:

我有两个ArrayList&lt;String&gt;,我想在一个ListView 中按顺序并排显示它们。

我已经设置了一个自定义的“row”、ListView,并了解如何使用list.setAdapter(myAdapter)

我的问题是myAdaptor 不起作用。

如何修复这个ArrayAdaptor 以显示两个ArrayList&lt;String&gt;

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;
    }
}

【问题讨论】:

  • 您从未在适配器的 getView 方法中实际设置 TextViews arrayOne 和 arrayTwo 上的文本,这就解释了为什么您什么都看不到。此外,您的适配器中不需要有 2 个 ArrayList ,因为您的 Word 对象包含来自您的 ArrayList 的对字符串的引用(我假设)。
  • 好的,我将删除ArrayList&lt;Word&gt; 和 setText 为 "" + arrayOne.size() 看看是否有效
  • 查看下面我的回答以获取更多信息stackoverflow.com/a/24812054/1426565
  • 将这两行剪切粘贴到 if(v == null) TextView arrayOne = (TextView) v.findViewById(R.id.text_view_1); TextView arrayTwo = (TextView) v.findViewById(R.id.text_view_2);

标签: android listview arraylist android-arrayadapter


【解决方案1】:

来自我的评论:

您实际上从未在适配器的 getView 方法中设置 TextViews arrayOne 和 arrayTwo 上的文本,这解释了为什么您什么都看不到。此外,您的适配器中不需要有 2 个 ArrayList,因为您的 Word 对象包含对来自您的 ArrayList 的字符串的引用(我假设)

这是一个调整后的适配器,符合我最初的建议:

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()。

【讨论】:

    【解决方案2】:

    首先将这些内容粘贴到 (v == null) 的“if block”中的 2 行以下

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

    实际上你还没有为你的文本视图设置值。所以你需要为这些文本视图设置值。

    见下文:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-25
      • 2011-12-31
      • 2016-02-22
      • 1970-01-01
      • 1970-01-01
      • 2017-08-30
      • 2015-12-07
      相关资源
      最近更新 更多