【问题标题】:Android ListView with Arrays带有数组的 Android ListView
【发布时间】:2013-12-04 19:59:49
【问题描述】:

所以,我对 Android 还是有点陌生​​,并试图在网上找到一些帮助,但我似乎找不到明确的解决方案。我正在创建一个仅包含 ListView 的应用程序。我创建了一个包含 2 个文本视图的 xml 文件(它与内置的 android.R.layout.simple_list_item_2 布局基本相同,但我猜我可能需要更改它)。它看起来像这样:

TextView1
TextView2

没什么特别的。我遇到的问题是使用数组。对于我的应用程序,TextView1 将只是一个集合字符串。但是,从技术上讲,Textview2 不仅仅是一个 TextView。我想取一个数组,创建必要数量的 TextView,并将它们放在 TextView1 下的 ListView 的每一行中。

例如,如果我有这 3 个数组:

String[] array1 = {TextView1A, TextView1B, TextView1C, TextView1D};
String[] array2 = {TextView2A, TextView2B};
String[] array3 = {TextView3A, TextView3B, TextView3C};

我希望输出看起来像这样:

TextView1
TextView1A
TextView1B
TextView1C
TextView1D

TextView2
TextView2A
TextView2B

TextView3
TextView3A
TextView3B
TextView3C

就像我说的,我对自学 android 还是有点陌生​​。我已经通过 ArrayAdapters 完成了简单的 ListViews,但我不确定这是否可以在这里工作。我想用更多的数组来创建它,每个数组都可以有不同数量的元素,所以我真的不认为简单地创建大量的自定义 ListView 行是一种选择。我真的不知道如何开始实施这个,所以任何朝着正确方向的推动都会受到赞赏。也欢迎有关如何执行此操作的任何其他建议。谢谢。

【问题讨论】:

  • 你可以看看 ExpandableListView。这是一个参考:stackoverflow.com/questions/3271772/android-treeview
  • 感谢您向我指出这一点。我从未尝试过使用 ExpandableListView(甚至不知道它存在),但在阅读它之后,这可能是最好的选择。

标签: android arrays listview android-listview


【解决方案1】:

所以您只想将所有数组合并到一个列表中?

在我看来,您仍然可以使用单个 ArrayAdapter 完成此操作。您需要一个额外的数组作为 ArrayAdapter 的数据源,其中包含连接在一起的所有数组,您只需手动保持同步即可。

如果该设置不适合您,您也可以查看MergeAdapter 它包装了任意数量的任意类型的适配器,并将它们全部组合到一个列表中。

【讨论】:

    【解决方案2】:

    如果您将 ListView 中的每一行视为其数据的所有者,我认为您将获得更好的实现。例如,创建一个 MyObject 类,它具有一行所需的所有属性。在我的自定义适配器下面的 MyObject 可以有四个不同的String,但它可以是任何东西。

    然后您实现一个自定义适配器,将其附加到您的ListView

    public class MyCustomAdapter extends ArrayAdapter<MyObject> {
    
        Context context;
    
        public MyCustomAdapter(Context context, int resourceId, List<MyObject> items) {
            super(context, resourceId, items);
            this.context = context;
        }
    
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            MyObject myObj = getItem(position);
    
            LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.my_listview_item, null);
    
                holder = new ViewHolder();
                holder.textView1 = (TextView) convertView.findViewById(R.id.textView1);
                holder.textView2 = (TextView) convertView.findViewById(R.id.textView2);
    
                convertView.setTag(holder);
            } else
                holder = (ViewHolder) convertView.getTag();
    
            String one = myObj.getString1();
            String two = myObj.getString2();
            String three = myObj.getString3();
            String four = myObj.getString4();
    
            // Depending if any of those strings are empty/null set textView1 and textView2 appropriatelly
    
            return convertView;
        }
    
        /* private view holder class */
        private class ViewHolder {
            TextView textView1;
            TextView textView2;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-03-24
      • 1970-01-01
      • 2011-02-11
      • 1970-01-01
      • 1970-01-01
      • 2015-10-08
      • 1970-01-01
      • 2019-12-17
      • 1970-01-01
      相关资源
      最近更新 更多