您应该制作自己的自定义适配器类并实现其方法。. .
这是自定义适配器类的完整示例:
private class Adapter extends BaseAdapter {
private ArrayList<String> array;
private Context ctx;
public Adapter(Context ctx) {
this.ctx = ctx;
array = new ArrayList<String>();
}
@Override
public int getCount() {
return this.array.size();
}
@Override
public Object getItem(int position) {
return this.array.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
/**
* add a String Item in a List
*
* @param item
*/
public void addItem(String item) {
this.array.add(item);
this.notifyDataSetChanged();
}
/**
* Delete an Item from a List
*
* @param position
*/
public void deleteItem(int position) {
this.array.remove(position);
this.notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View newView;
if (convertView == null) {
newView = View.inflate(ctx, R.layout.item_layout, null);
} else {
newView = convertView;
}
String txt = (String) this.getItem(position);
// Here is what you're looking for:
((TextView) newView.findViewById(R.id.item_numb)).setText(""
+ (position + 1) + ". ");
// End;
((TextView) newView.findViewById(R.id.item_text)).setText(txt);
return newView;
}
}
之后您可以像这样使用适配器:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_to_do_list);
ListView ls = (ListView) findViewById(R.id.listView);
Adapter adapter = new Adapter(this);
ls.setAdapter(adapter);
}