Listview With Multiple Strings
Solution 1:
You can write a custom ListAdapter to do this:
MyAdapteradapter=newMyAdapter (this);
for (inti=0; i < 10; i++) {
adapter.addAdapterItem(newAdapterItem("first", "second", "third");
}
view.setListAdapter(adapter);
class AdapterItem:
publicclassAdapterItem {
publicString first;
pubilc String second;
publicString third;
publicAdapterItem(String first, String second, String third) {
this.first = first;
this.second = second;
this.third = third;
}
}
class MyAdapter:
publicclassMyAdapterextendsBaseAdapter {
private List<AdapterItem> items;
publicvoidaddAdapterItem(AdapterItem item) {
items.add(item);
}
publicintgetCount() {
return items.size();
}
public Object getItem(int position) {
return items.get(position);
}
public View getView(int position, View convertView, ViewGroup parent)
View rowView = getLayoutInflater().inflate(R.layout.rowLayout);
TextView firstTextView = (TextView) rowView.findViewById(R.id.firstView);
firstTextView.setText(items.get(postion).first);
// do the same with second and thirdreturn rowView;
}
Layout:
<LinearLayoutandroid:Layout_width=".."android:layout_height=".."><TextViewandroid:id="firstView" /><TextViewandroid:id="secondView" /><TextViewandroid:id="thirdView" /></LinearLayout>
The code is not complete, just to give you the idea:
-You create a new ListAdapter that handles your items
- You can store your items in an extra class (
AdapaterItem
in this example) inside your adapter - Your
ListAdapter
implementation has to provide common methods likegetCount()
andgetItem()
- Within the
getView(..)
method you can return your custom view for each row. If you want to display 3 different text messages you have to define 3 Textviews in your layout and assign the strings ingetView(..)
Solution 2:
First, create a layout that organizes the views you want to appear in a single row. (See here for an example that groups two strings and an image.) Let's say you save this in a layout file called row_layout.xml
and it has three TextViews with ids t1
, t2
, and t3
.
Second, decide what data structure you are going to use for your data. We'll suppose it's an array of arrays of String, where each element contains the three strings you want to display in each row.
Third, you need to create a custom list adapter. If you're using the above array data structure, it might look something like this (adapted from here):
publicclassMySimpleArrayAdapterextendsArrayAdapter<String[]> {
privatefinal Context context;
privatefinal String[][] values;
publicMySimpleArrayAdapter(Context context, String[][] values) {
super(context, R.layout.row_layout, values);
this.context = context;
this.values = values;
}
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
LayoutInflaterinflater= (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewrowView= inflater.inflate(R.layout.row_layout, parent, false);
TextViewtextView1= (TextView) rowView.findViewById(R.id.t1);
TextViewtextView2= (TextView) rowView.findViewById(R.id.t2);
TextViewtextView3= (TextView) rowView.findViewById(R.id.t3);
textView1.setText(values[position][0]);
textView2.setText(values[position][3]);
textView3.setText(values[position][2]);
return rowView;
}
}
Finally, in your ListActivity, add this in onCreate:
setListAdapter(newMySimpleArrayAdapter(this, values));
where values
is the array of string arrays that you want displayed.
Solution 3:
Have you checked the official ListView tutorial?
- Create an XML file as your layout that will contain the 3 TextViews. This will be your list item
- Create Custom Adapter class by extending
BaseAdapter
. In this class you will pass the 3 values for each textbox and fill them accordingly. - Set the
setListAdapter
to your new collection, passing the layout as parameter and your custom adapter.
It takes a bit of work, but in the end, you will learn a lot.
Post a Comment for "Listview With Multiple Strings"