Set Listview Object Property

I have an ArrayList object that has the properties Object.name and Object.url .

I want to loop through an ArrayList and apply Object "name" to the android ListView. I also want to save the properties of the object in tactics so that I can call the "url" property in the onClick method.

What I have now:

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

But it’s clear that this is not what I need ...

Any help would be appreciated :)

+4
source share
1 answer

1.) You have an ArrayList:

 main_list 

2.) Create a ListView in your XML file (say main.xml) and capture 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); 

in your activity (if you are located somewhere else, for example, OnClickListener, replace "this" with the View variable, which was passed as a variable in OnClickListener).

3.) Define your ArrayAdapter. Note that one of its parameters (the third in your case) will be the identifier of the TextView. This is because the ArrayAdapter class by default returns a TextView in a ListView. If you override the ArrayAdapter class, you can use custom layouts to place elements with custom views in your ListView, but this is not necessary for what you specified in your question, and it seems that you already have one.

4.) Install the adapter in the ListView (given the ArrayAdapter named "aa"):

 livefeed.setAdapter(aa); 

Now, as the ArrayAdapter works, it calls each toString () object and sets each TextView in the ListView to this line. So make the toString () method in your Object class, which returns its name property:

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

Also note that if you add objects to an ArrayList, notify the ArrayAdapter so that it can update the ListView accordingly with the changes (given the ArrayAdapter named "aa"):

 aa.notifyDataSetChanged(); 

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

Also note that at some point you may need to cross-reference the ArrayAdapter and ArrayList between your activity and the Object class. It is very useful to make these fields static for this.

EDIT:

You also wanted to learn how to access a specific object when you click on an item in a ListView. Here it is (if your ListView is called 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 data } }); 
+9
source

All Articles