我正在尝试在列表行中显示单独的项目。所以我使用了4个textviews。但我使用arrayadapter扩展Activity。现在我必须根据textview将字符串列表放在一行中。怎么做到这一点?任何帮助都非常感谢,并提前感谢...
答案 0 :(得分:8)
通常我会在这种情况下延长ArrayAdapter
。通常,您只需要在适配器中覆盖两个函数 - getView()和一个构造函数。
适配器的代码如下:
/** 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;
}
}
答案 1 :(得分:4)
而不是使用ArrayAdapter尝试使用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);
}
});
在上面的代码中,使用ArrayList sampleArrayList 填充listView内容 所以可以使用键(“value1”,“value2”,“value3”,“value4”)访问任何列的项目。
希望这会有所帮助。