【发布时间】:2012-01-29 23:15:00
【问题描述】:
我有一个自定义的ListView 和一个TextView 和一个CheckBox。我还有一个自定义的SimpleAdapter,在其中我覆盖了getView() 方法,并且能够检索对TextView 和CheckBox 更改的点击。我的问题是我不知道如何在OnCheckedChanged 或OnClick 中正确点击ListItem 或CheckBox。
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;
}
}
感谢任何帮助。
【问题讨论】:
-
你有参数位置,还需要什么?
-
对不起,如果我没有正确解释。 position 参数总是返回某个位置,但如果我单击不同的行或复选框,它不会更新。
-
这很奇怪,你能把整个适配器贴出来吗?
-
更新为
CustomAdapter。 -
仍然不清楚点击位置是什么意思?只能有一个位置给出你的列表项的位置,你还在说什么“位置”?
标签: java android listview android-layout android-adapter