Is there a way to update multiple TextViews using an ArrayAdapter?

I have a list view that has several text fields:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="16dip" android:textColor="#000000" android:paddingLeft="10dip" android:textStyle="bold"/> <TextView android:id="@+id/address" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="16dip" android:textColor="#000000" android:paddingTop="15dip" android:paddingBottom="15dip" android:paddingLeft="10dip" android:textStyle="bold"/> </RelativeLayout> 

I have a POJO list that has name and address , and I want each item in the list view to be populated with these values.

My POJO is as follows:

 public class Person { private String name; private String address; //getter setter public String toString() {return name;} } 

Question

When I install a list adapter with my list, how do I set both names and addresses?

Currently, I am doing this only by setting the name:

 setListAdapter(new ArrayAdapter<Person>(MyActivity.this, R.layout.list_text, R.id.name, personList)); 
+8
android listview android-arrayadapter listactivity
source share
2 answers

You should create a custom adapter that extends the ArrayAdapter . Everything you can do with the ArrayAdapter is limited.

Something like that:

 public class PersonAdapter extends ArrayAdapter<Person> { private final Context context; private final ArrayList<Person> data; private final int layoutResourceId; public PersonAdapter(Context context, int layoutResourceId, ArrayList<Person> data) { super(context, layoutResourceId, data); this.context = context; this.data = data; this.layoutResourceId = layoutResourceId; } @Override public View getView(int position, View convertView, ViewGroup parent) { View row = convertView; ViewHolder holder = null; if(row == null) { LayoutInflater inflater = ((Activity)context).getLayoutInflater(); row = inflater.inflate(layoutResourceId, parent, false); holder = new ViewHolder(); holder.textView1 = (TextView)row.findViewById(R.id.text1); holder.textView2 = (TextView)row.findViewById(R.id.text2); ... ... holder.textView3 = (TextView)row.findViewById(R.id.text3); row.setTag(holder); } else { holder = (ViewHolder)row.getTag(); } Person person = data.get(position); holder.textView1.setText(person.getName()); holder.textView2.setText(person.getAddress()); ... ... holder.textView3.setText(person.getEtc()); return row; } static class ViewHolder { TextView textView1; TextView textView2; ... ... TextView textView3; } } 

Where textView1 , textView2 ... textView-n are all your text views. Install the adapter as follows:

 setListAdapter(new PersonAdapter(MyActivity.this, R.layout.list_text, personList)); 

Note. I assume that your personList is an object of type List . If it's Array , let me know.

+12
source share

I would look at the TwoLineArrayAdapter from ESV, it is formatted for title and title, but that's almost what you need.

Edit:

 class StringHolder { public String text; public int viewId; } 

For a scenario of n lines, the solution is very similar. Create a custom ArrayAdapter type StringHolder[] or List<StringHolder> depending on whether this amount is fixed. In getView you skip all StringHolder objects, call findViewById and refresh the text content. Alternatively, you can simply use the index in the array / list and map it to the view identifier if they remain unchanged.

This, of course, is only one way to do this, there is still a lot, and I will leave the actual implementation as an exercise for the reader.

0
source share

All Articles