添加自定义数据的ListView&安培; ArrayAdapter项目 [英] Adding custom data to ListView & ArrayAdapter items

查看:191
本文介绍了添加自定义数据的ListView&安培; ArrayAdapter项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个Android应用程序。在片段我有一个的ListView 正在使用填充的 ArrayAdapter 的ArrayList 。我使用 android.R.layout.simple_list_item_1 的布局列表项。我想有一个 OnItemClickListener ,以便在点击一个项目时,它会显示基于其数据的另一个活动。

I'm creating an Android application. Inside a Fragment I have a ListView that is populated using an ArrayAdapter and an ArrayList. I'm using android.R.layout.simple_list_item_1 for the layout for the list items. I want to have an OnItemClickListener, so that when an item is clicked it will show another Activity based on its data.

的问题是,有可能是具有相同的名称的项目。我想的ID值附加到每个元件,使我code可以彼此区分开来。

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.

我的项目是一个自定义类来保存自己的数据,但这里的重要领域是标识和名称(这是在的ListView显示)。

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).

我的code用于填充的ListView

My code for populating the 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
    }
});

我怎么能确定每个项目的ID添加到列表中的项目?

How could I add an ID to the list items for identifying each item?

推荐答案

解决的办法很简单其实。

The solution is quite simple actually.

您正在填充的ListView 列表。在列表是项目的有序集合,所以将其添加为数据源的的ListView 你将永远知道何时指数的每个项目的。

You're populating the ListView from a List. 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.

于是选择从的ListView项目时你的查看位置点击。这一立场将对应的位置在列表

So when selecting an item from the ListView you get the position of the View clicked. This position will correspond to the position in your List.

您不会真正需要的 ID 字段中输入 MyCustomDataObject ,当然,当你填充列表 MyCustomDataObject 你可以使用普通的for循环(未强化),并使用索引来设置每个ID MyCustomDataObject

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.

这篇关于添加自定义数据的ListView&安培; ArrayAdapter项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆