分配对象属性的ListView

分配对象属性的ListView

本文介绍了分配对象属性的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有属性 Object.name 目标对象的的ArrayList 的.url

I have an ArrayList of an object which has properties Object.name and Object.url.

我想通过ArrayList的循环和应用对象的名到Android的ListView。我也想保持在机智对象的其他属性,这样我可以调用onClick的方法,在URL属性。

I want to loop through the ArrayList and apply the Object's "name" to an android ListView. I also want to keep the Object's other properties in tact, so that i can call the "url" property in the onClick method.

我现在是这样的:

main_list.setAdapter(new ArrayAdapter<RomDataSet>(this, android.R.layout.simple_list_item_1, android.R.id.text1, mRoms));

但显然这不是我所需要的...

But clearly that is not what I need...

任何帮助将是AP preciated:)

Any help would be appreciated :)

推荐答案

1)你有你的ArrayList:

1.) You have your ArrayList:

main_list

2)建立在XML文件中一个ListView(比如说,main.xml中)并获取其ID。也就是说,给定

2.) Create a ListView in your XML file (say, main.xml) and grab its id. That is, given:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/liveFeed"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>
</LinearLayout>

做这样的事情:

ListView livefeed = (ListView)this.findViewById(R.id.liveFeed);

您的活动中(如果你在别的地方,如OnClickListener是,取代本与已作为一个变量进入OnClickListener通过查看变量)。

within your activity (if you're in somewhere else such as an OnClickListener, replace the "this" with the View variable that was passed as a variable into the OnClickListener).

3)定义你的ArrayAdapter。需要注意的是它的一个参数(第三个中的情况下)将是一个TextView标识。这是因为一个ArrayAdapter类,默认情况下,返回在ListView一个TextView。如果重写一个ArrayAdapter类,你可以使用自定义布局,以与你的ListView中的自定义视图的项目,但是这是没有必要的,你在你的问题已经概述了什么,好像你已经得到它了。

3.) Define your ArrayAdapter. Note that one of its parameters (the third one in your case) will be a TextView id. This is because the ArrayAdapter class, by default, returns a TextView in the ListView. If you override the ArrayAdapter class, you can use custom layouts to have items with custom Views within your ListView, but this is not necessary for what you've outlined in your question, and it seems like you've got it already.

4)适配器设置到ListView(给予ArrayAdapter评为AA):

4.) Set the adapter to the ListView (given an ArrayAdapter named 'aa'):

livefeed.setAdapter(aa);

现在一个ArrayAdapter的工作方式是每次调用对象的toString()方法,并设置每个TextView的ListView中这个字符串。因此,请在你的对象的类toString()方法返回其名称属性:

Now the way the ArrayAdapter works is it invokes each Object's toString() method and sets each TextView in the ListView to this String. So make a toString() method in your Object's class that returns its name property:

public String toString(){return name;} //assuming name is a String

另外要注意的是,如果你的对象添加到ArrayList,通知你,因此它可以相应地更新与修改你的ListView(给定一个名为'AA'的ArrayAdapter)一个ArrayAdapter:

Also note that, if you add Objects to the ArrayList, notify the ArrayAdapter that you have so it can accordingly update your ListView with the modifications (given an ArrayAdapter named 'aa'):

aa.notifyDataSetChanged();

让我知道如果你需要更多的帮助。与往常一样,检查答案对勾,如果这回答了你的问题。

Let me know if you need any more help. As always, check the answer check mark if this answered your question.

另外要注意的是,在一个点上,你可能希望交叉引用您的活动和对象类之间的ArrayAdapter和ArrayList。这是非常有益的,使这些领域的静态为了做到这一点。

Also note that, at one point you may wish to cross reference your ArrayAdapter and ArrayList between your activity and Object class. It's very helpful to make these fields static in order to do so.

编辑:

您想也知道如何当你点击ListView中访问项目的特定对象。这里是(给你的ListView名为livefeed):

You wanted to also know how to access a specific Object when you click on an item in the ListView. Here it is (given your ListView is named livefeed):

livefeed.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> a, View v, int position, long id) {

    //in here you may access your Object by using livefeed.getItemAtPosition(position)
    //for example:
        Object current = livefeed.getItemAtPosition(position);
        //do whatever with the Object's data
    }
});

这篇关于分配对象属性的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 06:30