我有一個自定義ListView
與TextView
和CheckBox
。我還有一個自定義SimpleAdapter
,其中我重寫了getView()
方法,並能夠檢索TextView
和CheckBox
更改的點擊次數。我的問題是我不知道如何在OnCheckedChanged
或OnClick
內獲得正確的點擊ListItem
或CheckBox
。獲取ListItem在自定義SimpleLayout中的位置
UPDATE增加整個CustomAdapter
類:
public class CustomAdapter extends SimpleAdapter {
private LayoutInflater mInflater;
private List<HashMap<String, String>> mItems = null;
private Context mContext;
private int mPosicion;
public CustomAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) {
super(context, items, resource, from, to);
mInflater = LayoutInflater.from(context);
mItems = items;
mContext = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
mPosicion = position;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_row_view, null);
holder = new ViewHolder();
holder.chkbxEstado = (CheckBox) convertView.findViewById(R.id.chkbxCompletado);
holder.txtTextoAgenda = (TextView) convertView.findViewById(R.id.txtTextoLista);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtTextoAgenda.setText(mItems.get(position).get("descripcion"));
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.i("Posicion",""+mPosicion);//I don't know how to retrieve clicked position
}
});
holder.chkbxEstado.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.i("Posicion",""+mPosicion);//Or checked
}
});
return convertView;
}
private static class ViewHolder {
TextView txtTextoAgenda;
CheckBox chkbxEstado;
}
}
任何幫助表示讚賞。
您有參數位置,您還需要什麼? – gwa 2012-01-30 01:55:18
對不起,如果我沒有正確解釋。位置參數總是返回某個位置,但如果點擊不同的行或複選框則不更新。 – 2012-01-30 19:51:04
這很奇怪,你可以發佈整個適配器嗎? – gwa 2012-01-30 20:47:58