简体   繁体   English

将自定义数据添加到ListView和ArrayAdapter项

[英]Adding custom data to ListView & ArrayAdapter items

I'm creating an Android application. 我正在创建一个Android应用程序。 Inside a Fragment I have a ListView that is populated using an ArrayAdapter and an ArrayList . 在一个Fragment我有一个使用ArrayAdapterArrayList填充的ListView I'm using android.R.layout.simple_list_item_1 for the layout for the list items. 我将android.R.layout.simple_list_item_1用于列表项的布局。 I want to have an OnItemClickListener , so that when an item is clicked it will show another Activity based on its data. 我想要一个OnItemClickListener ,以便在单击某个项目时将根据其数据显示另一个Activity

The problem is, there may be items with the same name. 问题是,可能有同名物品。 I'd like to attach an ID value to each of the elements, so that my code can distinguish them from each other. 我想将ID值附加到每个元素,以便我的代码可以将它们彼此区分开。

My items that I use to populate the list are of a custom class to hold their data, but the important fields here are the ID and the name (which is shown in the ListView ). 我用来填充列表的项目是一个自定义类,用于保存其数据,但是这里的重要字段是ID和名称(如ListView所示)。

My code for populating the ListView : 我的代码用于填充ListView

List<String> items;
ArrayAdapter<String> adapter;
List<MyCustomDataObject> listOfDataObjects;

...

// Get the ListView
ListView list = (ListView) layoutRootView.findViewById(R.id.listView1);
// Create the item List and the ArrayAdapter for it
items = new ArrayList<String>();
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items);
// Set the list adapter
list.setAdapter(adapter);
// Add the data items
for (MyCustomDataObject obj : listOfDataObjects) {
    items.add(obj.name);
}
items.add(getResources().getString(R.string.no_items));
// Create the item click listener
list.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // Open the Activity based on the item
    }
});

How could I add an ID to the list items for identifying each item? 如何为列表项添加ID以标识每个项?

The solution is quite simple actually. 解决方案实际上很简单。

You're populating the ListView from a List . 您正在从List填充ListView The List is an ordered collection of items, so when adding it as the datasource for the ListView you will always know the index of each item. List是项目的有序集合,因此将其添加为ListView的数据源时,您将始终知道每个项目的索引。

So when selecting an item from the ListView you get the position of the View clicked. 因此,当从ListView选择一个项目时,您将获得单击View的位置。 This position will correspond to the position in your List . 此位置将与您的List的位置相对应。

You won't really need the id field of your MyCustomDataObject , but of course when you populate the List of MyCustomDataObject you could use a normal for-loop (not enhanced) and use the index to set the id of each MyCustomDataObject . 您实际上并不需要MyCustomDataObjectid字段,但是当然,当您填充MyCustomDataObjectList ,可以使用常规的for循环(未增强)并使用索引来设置每个MyCustomDataObject的id。

Lookup the position in listOfDataObjects to find the ID: 在listOfDataObjects中查找位置以找到ID:

// Create the item click listener
list.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (position==listOfDataObjects.size()) {   .... no_items clicked ... }
        else {
          MyCustomDataObject obj = listOfDataObjects.get(position);
          ... // Open the Activity based on the item
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM