这是在 android 中使用 BaseAdapter 的自定义列表视图的代码,它工作正常:
public class CustomListAdapter extends BaseAdapter {
private ArrayList<TaskClass> _listData;
Context _c;
public CustomListAdapter(Context context, ArrayList<TaskClass> listData) {
_listData = listData;
_c = context;
}
@Override
public int getCount() {
return _listData.size();
}
@Override
public Object getItem(int position) {
return _listData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position,View convertView,ViewGroup parent) {
View v = convertView;
if (v == null)
{
LayoutInflater layoutInflator = (LayoutInflater) _c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = layoutInflator.inflate(R.layout.listview_row, null);
}
TextView titleText = (TextView)v.findViewById(R.id.holdTitleText);
TextView catText = (TextView)v.findViewById(R.id.holdCatText);
TextView descText = (TextView)v.findViewById(R.id.holdDescText);
TextView dateText = (TextView)v.findViewById(R.id.holdDateText);
//CheckBox checkBoxForEachItem = (CheckBox)v.findViewById(R.id.)
TaskClass taskClassInstance = _listData.get(position);
titleText.setText(taskClassInstance.getTitle());
catText.setText(taskClassInstance.getTaskCategory());
descText.setText(taskClassInstance.getDescription());
dateText.setText(taskClassInstance.getTaskDate());
return v;
}
}
并在活动中使用自定义适配器绑定列表视图:
listViewInstance.setAdapter(new CustomListAdapter(getApplicationContext(),taskClasslistInstance));
其中“taskclasslistinstance”是我的arraylist conatining data from DB 它现在工作正常我需要为listitemclick编写函数,这样当用户单击任何listitem时,我可以从数据库中获取 该listitem记录的rowid 。所以在获得rowwid后我可以从中删除记录listview 和来自 db 并且可以编辑信息