Reputation: 5854
I'm trying to display separate items in a list row. So i use 4 textviews. But i use arrayadapter for my class extending Activity. Now i have to put the list of strings in a row according to that textview. How to achieve this? any help is highly appreciated and thanks in advance...
Upvotes: 1
Views: 4194
Reputation: 4580
Instead of ArrayAdapter try by using SimpleAdapter.
ArrayList<HashMap<String, String>> sampleArrayList;
SimpleAdapter sampleListAdapter;
HashMap<String, String> sampleObjectMap;
for (SampleObject sampleObj : sampleList) {
sampleObjectMap= new HashMap<String, String>();
sampleObjectMap.put("value1", sampleObj.getValue1());
sampleObjectMap.put("value2", sampleObj.getValue2());
sampleObjectMap.put("value3", sampleObj.getValue3());
sampleObjectMap.put("value4", sampleObj.getValue4());
sampleArrayList.add(sampleObjectMap);
}
sampleListAdapter= new SimpleAdapter(
context, sampleArrayList,
R.layout.custom_list_layout, new String[] {
"value1", "value2" , "value3", "value4"},
new int[] { R.id.list_content_column1,
R.id.list_content_column2,
R.id.list_content_column3,
R.id.list_content_column4});
sampleListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap<String, String> sampleObjectMapLocal = sampleArrayList
.get(position);
final String value1Obj = sampleObjectMapLocal .get("value1");
System.out.println("value1Obj : " + value1Obj);
}
});
In the above code the listView contents are populated using an ArrayList sampleArrayList and so the items at any column could be accessed using key ("value1", "value2" , "value3", "value4").
Hope this would help.
Upvotes: 4
Reputation: 12643
Usually I'm extending ArrayAdapter
in such cases. Generally You need to override only two functions in the adapter - getView() and one of constructors.
The code of the adapter is following:
/** class to act as list adapter for rows List */
private static class FourTextListAdapter extends ArrayAdapter<MyDataClass> {
/** To cache views of item */
private static class ViewHolder {
private TextView text1;
private TextView text2;
private TextView text3;
private TextView text4;
/**
* General constructor
*/
ViewHolder() {
// nothing to do here
}
}
/** Inflater for list items */
private final LayoutInflater inflater;
/**
* General constructor
*
* @param context
* @param resource
* @param textViewResourceId
* @param objects
*/
public FourTextListAdapter(final Context context,
final int resource,
final int textViewResourceId,
final List<User> objects) {
super(context, resource, textViewResourceId, objects);
this.inflater = LayoutInflater.from(context);
}
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
View itemView = convertView;
ViewHolder holder = null;
final MyDataClass item = getItem(position);
if(null == itemView) {
itemView = this.inflater.inflate(R.layout.four_texts_item, parent, false);
holder = new ViewHolder();
holder.text1 = (TextView)itemView.findViewById(R.id.text1);
holder.text2 = (TextView)itemView.findViewById(R.id.text2);
holder.text3 = (TextView)itemView.findViewById(R.id.text3);
holder.text4 = (TextView)itemView.findViewById(R.id.text4);
itemView.setTag(holder);
} else {
holder = (ViewHolder)itemView.getTag();
}
holder.text1.setText(item.getText1());
holder.text2.setText(item.getText2());
holder.text3.setText(item.getText3());
holder.text4.setText(item.getText4());
return itemView;
}
}
Upvotes: 8