Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign object property to listview

I have an ArrayList of an object which has properties Object.name and Object.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.

What i have now is this:

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

Any help would be appreciated :)

like image 476
jsw Avatar asked Dec 27 '22 20:12

jsw


1 Answers

1.) You have your ArrayList:

main_list

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>

Do something like this:

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

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.) 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.) Set the adapter to the ListView (given an ArrayAdapter named 'aa'):

livefeed.setAdapter(aa);

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

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.

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.

EDIT:

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
    }
});
like image 110
Vinay Avatar answered Dec 30 '22 11:12

Vinay