Listview With Multiple Strings
Solution 1:
You can write a custom ListAdapter to do this:
MyAdapter adapter = new MyAdapter (this);
for (int i = 0; i < 10; i++) {
adapter.addAdapterItem(new AdapterItem("first", "second", "third");
}
view.setListAdapter(adapter);
class AdapterItem:
public class AdapterItem {
public String first;
pubilc String second;
public String third;
public AdapterItem(String first, String second, String third) {
this.first = first;
this.second = second;
this.third = third;
}
}
class MyAdapter:
public class MyAdapter extends BaseAdapter {
private List<AdapterItem> items;
public void addAdapterItem(AdapterItem item) {
items.add(item);
}
public int getCount() {
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 third
return rowView;
}
Layout:
<LinearLayout android:Layout_width=".." android:layout_height="..">
<TextView android:id="firstView" />
<TextView android:id="secondView" />
<TextView android: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):
public class MySimpleArrayAdapter extends ArrayAdapter<String[]> {
private final Context context;
private final String[][] values;
public MySimpleArrayAdapter(Context context, String[][] values) {
super(context, R.layout.row_layout, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.row_layout, parent, false);
TextView textView1 = (TextView) rowView.findViewById(R.id.t1);
TextView textView2 = (TextView) rowView.findViewById(R.id.t2);
TextView textView3 = (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(new MySimpleArrayAdapter(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"