【问题标题】:Create list view with custom set of data in android?在android中使用自定义数据集创建列表视图?
【发布时间】:2015-06-12 11:46:44
【问题描述】:

我正在尝试使用自定义数据集创建ListView,如下所示:

String superType = "random1";
String superTypea = "random12";
String superType12 = "random2";

String superType_amount = "child1";
String childtype_calulated = "2323";

 String superType_amount = "child2";
String childtype_calulated = "23223";

 String superType_amount = "child2";
String childtype_calulated = "amount3";

现在我想用这组数据创建ListView怎么办?

这是列表结构...

row1=superType  |superType_amount |childtype_calulated 
row2=superTypea |superType_amount |childtype_calulated
row3=superType12|superType_amount |childtype_calulated

有什么解决办法吗?

【问题讨论】:

    标签: android list listview custom-lists


    【解决方案1】:

    完全可以做到这一点。首先,我建议将您的数据放入一个集合中。最好将它们放入一个对象中,然后放入这些对象的集合中。从那里您可以将 ListView 添加到主布局,为列表项定义自定义布局,并使用 ArrayAdapter 填充 ListView。

    Here is a really good example of how you can do this well. 它包括从外部源加载数据的示例,您不需要。

    但是,如果您现在开始开发,我建议您也研究一下 RecyclerView。 RecyclerView 是新的,包含在 AppCompat v7 库中,可在 Lollipop 之前的 Android 上使用。对于一个简单的列表,RecyclerView 的实现会稍微复杂一些,但它的可扩展性和效率要高得多。我相信 Google 打算在未来完全用 RecyclerView 替换 ListView。

    Here is a pretty simple introduction to making a list with RecyclerView.



    编辑

    将 ArrayAdapter 与 ListView 一起使用。首先,您需要创建一个模型来存储您的数据,您可以将某种类放入集合中,例如:

    public class Item {
        public String title;
        public String sub1;
        public String sub2;
    
        public void Item(String t, String s1, String s2) {
            title = t;
            sub1 = s1;
            sub2 = s2;
        }
    }
    

    然后你需要为列表中的项目定义布局:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="horizontal" >
        <TextView
          android:id="@+id/title"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" />
       <TextView
          android:id="@+id/sub1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" />
       <TextView
          android:id="@+id/sub2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" />
    </LinearLayout>
    

    然后你需要通过扩展 ArrayAdapter 类来制作你的自定义 ArrayAdapter:

    public class ItemAdapter extends ArrayAdapter<Item> {
        public ItemAdapter(Context context, ArrayList<Item> items) {
           super(context, 0, items);
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
           Item item = getItem(position);    
    
           if (convertView == null) {
              convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_layout, parent, false);
           }
    
           TextView title = (TextView) convertView.findViewById(R.id.title);
           TextView sub1 = (TextView) convertView.findViewById(R.id.sub1);
           TextView sub2 = (TextView) convertView.findViewById(R.id.sub2);
    
           title.setText(item.title);
           sub1.setText(item.sub1);
           sub2.setText(item.sub2);
    
           return convertView;
       }
    }
    

    那么你需要做的就是在你的主类中创建一个适配器实例并将你的集合附加到它:

    ArrayList<Item> data = new ArrayList<Item>();
    
    ItemAdapter adapter = new ItemAdapter(this, data);
    
    ListView listView = (ListView) findViewById(R.id.list_view);
    listView.setAdapter(adapter);
    

    这应该用列表中您需要的所有项目填充您的 ListView。我没有运行任何代码,所以可能会有一两个小错误需要您修复。

    【讨论】:

    • 抱歉,我不知道该怎么做? :(请建议
    • 检查我的编辑,我给了你一些可以帮助你的代码。
    • 好棒的答案:)
    猜你喜欢
    • 2014-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-30
    • 1970-01-01
    • 2020-08-20
    相关资源
    最近更新 更多