爲什麼不在ListView本身上使用onItemClickListener?
您的適配器應該包含一個對象類型的列表(沒有嚴格的規則,但它有助於更輕鬆地管理項目)。例如
class MsgObject{
String msg;
String data
//TODO Getters/Setters goes here
}
然後你CustomAdapter將只包含
List<MsgObject> objectList;
然後你getView將類似於這樣
MsgObject m = (MsgObject)getObject(position);
TextView text=(TextView)vi.findViewById(R.id.text);
ImageView image=(ImageView)vi.findViewById(R.id.image);
text.setText(m.getMsg());
text.getLineCount();
imageLoader.DisplayImage(m.getData(), image);
//Tag id is predefined in xml
vi.setTag(R.id.listItemTag, m);
return vi;
現在你的觀點會處理這個作爲一個對象,而不是一個佈局具有多個值。
然後,我們把所有的點擊動作到ListView控件駐留在活動。
listView.setOnItemClickListener(){
new AdapterView.OnItemClickListener(){
@override
public onItemClick(AdapterView<?> parent, View view, int position, long id){
MsgObject m = (MsgObject)view.getTag(R.id.listItemTag);
Toast.makeText(context, "Pos[" + position
+ "] clicked, with msg: " + m.getMessage(), Toast.LENGTH_SHORT).show();
}
}
};
這是我有我的延遲加載的ImageView ListView控件以及方式。然後,您將能夠訪問與該視圖關聯的對象以及被點擊的位置。
如果你想分開msg和數據。你可以使用setTag(id,obj);兩個對象等
setTag(R.id.listItemMsg, msg[position]);
setTag(R.id.listItemData, data[position]);
UPDATE:我CustomAdapter的例子
/**
* Adapter for displaying Place selection list.
* @author Poohdish Rattanavijai
*
*/
public class PlaceAdapter extends BaseAdapter {
private static final String TAG = PlaceAdapter.class.getSimpleName();
private List<PlaceVO> list; // <-- list of PlaceVOs
private Context context;
private int viewResourceId;
/**
*
* @param context Context
* @param viewResourceId Layout ID for each item
* @param list resource list to populate
*/
public PlaceAdapter(Context context, int viewResourceId, List<PlaceVO> list){
this.context = context;
this.viewResourceId = viewResourceId;
this.list = list;
}
/**
* Number of result in the list plus one (for +add at the last item)
*/
@Override
public int getCount() {
if(null != list){
return list.size();
}
return 1;
}
@Override
public Object getItem(int arg0) {
if(null != list){
try {
return list.get(arg0);
} catch (IndexOutOfBoundsException e) {
return null;
}
}
return null;
}
@Override
public long getItemId(int position) {
// if(null != list){
// try {
// return list.get(position).getId();
// } catch (IndexOutOfBoundsException e) {
// return 0;
// }
// }
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(null == convertView){
/**
* View does not exist, populate.
*/
LayoutInflater inflater = LayoutInflater.from(this.context);
convertView = inflater.inflate(this.viewResourceId, parent, false);
}
ViewHolder holder = (ViewHolder)convertView.getTag(R.id.adpter_view);
if(null == holder){
Log.d(TAG, "holder not found, init.");
/**
* ViewHolder does not exists for this view; create and assign respective view.
*/
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.details = (TextView) convertView.findViewById(R.id.details);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
holder.progress = (ProgressBar) convertView.findViewById(R.id.progress);
}
PlaceVO v = (PlaceVO)getItem(position); // <-- GetItem
if(null != v){
Log.d(TAG, "Place not null");
if(HelperUtil.IsNotNullOrEmpty(v.getName())){
Log.d(TAG, "Name: " + v.getName());
holder.title.setText(v.getName());
}
if(HelperUtil.IsNotNullOrEmpty(v.getVicinity())){
Log.d(TAG, "details: " + v.getVicinity());
holder.details.setText(v.getVicinity());
}
if(HelperUtil.IsNotNullOrEmpty(v.getIcon())){
holder.progress.setVisibility(View.VISIBLE);
holder.icon.setVisibility(View.GONE);
//TODO Initialize LazyLoad
}else{
holder.progress.setVisibility(View.VISIBLE);
holder.icon.setVisibility(View.GONE);
}
}
// Two tags, one for holder, another for the VO
convertView.setTag(R.id.adpter_view, holder);
convertView.setTag(R.id.adpter_object, v);
return convertView;
}
static class ViewHolder{
TextView title;
TextView details;
ImageView icon;
ProgressBar progress;
}
}
內活動我處理項目點擊行動
OnItemClickListener itemClick = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
PlaceVO v = (PlaceVO)arg1.getTag(R.id.adpter_object); // <-- get object using tag.
switchToPlaceScreen(v);
}
};
listView.setOnItemClickListener(itemClick);
希望這回答你的問題:)
產品編號從哪裏來?你爲什麼不直接使用'setTag'和特定視圖的特定項目號? – Cody 2012-02-05 18:40:42