Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set both lines of a ListView using simple_list_item_2?

So the following will create a ListView where the rows have their "primary" textview filled by the values array.

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,      android.R.layout.simple_list_item_2, android.R.id.text1, values); 

Changing the third parameter to android.R.id.text2 sets the "secondary" textview. Is there any simple way to set both?

like image 305
arjs Avatar asked Jun 29 '12 05:06

arjs


2 Answers

This questions ranks high with Google but I consider the given answer to be way too complicated. As pointed out in other answers, the desired functionality can be achieved using ArrayAdapter with a very easy trick.

You can override the getView method of the ArrayAdapter:

ArrayAdapter adapter = new ArrayAdapter(context, android.R.layout.simple_list_item_2, android.R.id.text1, list) {   @Override   public View getView(int position, View convertView, ViewGroup parent) {     View view = super.getView(position, convertView, parent);     TextView text1 = (TextView) view.findViewById(android.R.id.text1);     TextView text2 = (TextView) view.findViewById(android.R.id.text2);      text1.setText(persons.get(position).getName());     text2.setText(persons.get(position).getAge());     return view;   } }; 

If you didn't notice: the trick is to supply android.R.id.text1 as (principally unneccessary) parameter, otherwise the call to super will cause an exception.

Also, this solution does not make use of TwoLineListItem, which was deprecated in API 17.

like image 197
winne2 Avatar answered Oct 31 '22 15:10

winne2


AFAIK simple_list_item_2 contains a TwoLineListItem containing two TextViews. ArrayAdapter is not going to work here,you'll either have to create a custom adapter or use one that supports it like SimpleCursorAdapter.

ListAdapter adapter = new SimpleCursorAdapter(                  this,                  android.R.layout.simple_list_item_2,                  mCursor,     // Pass in the cursor to bind to.                  new String[] {People.NAME, People.COMPANY}, // Array of cursor columns to bind to.                  new int[] {android.R.id.text1, android.R.id.text2});  // Parallel array of which template objects to bind to those columns.           // Bind to our new adapter.          setListAdapter(adapter); 

Or if you dont want SimpleCursorAdapter You will have to create Custom ArrayAdapter or BaseAdapter

Create a custom ArrayAdapter,apply the object(Having two items) array to the custom adapter, and feed it to getListView.setAdapter.

Override the ArrayAdapter's getView method to apply your name strings to TextViews in your custom list row view.

Following Snippet will help you.

SampleActivity.java

package org.sample;  import java.util.ArrayList;  import android.app.ListActivity; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import android.widget.TwoLineListItem;  public class SampleActivity extends ListActivity {      @Override     public void onCreate(Bundle icicle) {         super.onCreate(icicle);          Person person;          ArrayList<Person> persons = new ArrayList<Person>();          person = new Person();         person.setName("Vipul");         person.setAge(20);         persons.add(person);          person = new Person();         person.setName("Anil");         person.setAge(22);         persons.add(person);          setListAdapter(new MyAdapter(this, persons));     }  } 

Person.java

class Person {     String name;     int age;      public String getName() {         return name;     }      public void setName(String name) {         this.name = name;     }      public int getAge() {         return age;     }      public void setAge(int age) {         this.age = age;     }  } 

MyAdapter.java

class MyAdapter extends BaseAdapter {      private Context context;     private ArrayList<Person> persons;      public MyAdapter(Context context, ArrayList<Person> persons) {         this.context = context;         this.persons = persons;     }      @Override     public int getCount() {         return persons.size();     }      @Override     public Object getItem(int position) {         return persons.get(position);     }      @Override     public long getItemId(int position) {         return 0;     }      @Override     public View getView(int position, View convertView, ViewGroup parent) {          TwoLineListItem twoLineListItem;          if (convertView == null) {             LayoutInflater inflater = (LayoutInflater) context                     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);             twoLineListItem = (TwoLineListItem) inflater.inflate(                     android.R.layout.simple_list_item_2, null);         } else {             twoLineListItem = (TwoLineListItem) convertView;         }          TextView text1 = twoLineListItem.getText1();         TextView text2 = twoLineListItem.getText2();          text1.setText(persons.get(position).getName());         text2.setText("" + persons.get(position).getAge());          return twoLineListItem;     } } 
like image 33
Vipul Avatar answered Oct 31 '22 15:10

Vipul