【问题标题】:Custom listview Onclick android自定义列表视图 Onclick android
【发布时间】:2013-10-08 13:43:37
【问题描述】:

我正在制作一个表单,我必须在列表视图中使用复选框。我已经制作了一个带有复选框的列表。当列表项被选中和取消选中时,我无法在 edittext 中显示信息。 这是我的代码----

private void getCustomListitem(){
        _lvcustom=(ListView)findViewById(R.id.lvcustom);        
                 String datas[]={"one","two"};

        simpleListAdap = new SimpleListAdapter_Custom(_act , data);
        _lvcustom.setAdapter(simpleListAdap);

public class SimpleListAdapter_Custom extends ArrayAdapter<String>{
private Activity _parent;
private String[] _data;
private EditText _et;
public SimpleListAdapter_Custom(Activity parent,  String[] data) {
    super(parent, R.layout.simple_list_custom, data);
    // TODO Auto-generated constructor stub
    this._parent = parent;
    this._data = data;

}
@Override
public View getView(int position, View convertView, ViewGroup parent){
    LayoutInflater inflater = this._parent.getLayoutInflater();
    View curView = inflater.inflate(R.layout.simple_list_custom, parent,false);

    final CheckBox cb = (CheckBox) curView.findViewById(R.id.cb01);
//  cb.setChecked(_data.length);
    cb.setTag(_data);

    final TextView chkTxtView = (TextView) curView.findViewById(R.id.label);
    chkTxtView.setText(this._data[position]);

    return curView;
}
}

【问题讨论】:

  • 有什么问题?
  • 当列表中的任何项目被点击时,我想在编辑文本中显示其相关信息

标签: android checkbox android-listview


【解决方案1】:
    public class SimpleListAdapter_Custom extends ArrayAdapter<String>{
private Activity _parent;
private String[] _data;
private EditText _et;
private boolean[] checkBoxState;

public SimpleListAdapter_Custom(Activity parent,  String[] data) {
    super(parent, R.layout.simple_list_custom, data);
    // TODO Auto-generated constructor stub
    this._parent = parent;
    this._data = data;
    checkBoxState = new boolean[data.getCount()];

}
@Override
public View getView(int position, View convertView, ViewGroup parent){
    LayoutInflater inflater = this._parent.getLayoutInflater();
    View curView = inflater.inflate(R.layout.simple_list_custom, parent,false);

    final CheckBox cb = (CheckBox) curView.findViewById(R.id.cb01);
    final TextView chkTxtView = (TextView) curView.findViewById(R.id.label);
//  cb.setChecked(_data.length);
//    cb.setTag(_data);
    cb.setChecked(checkBoxState[position]);

    cb.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            if (((CheckBox) v).isChecked())
                checkBoxState[position] = true;
            else
                checkBoxState[position] = false;
        }
    });
    if (checkBoxState[position])
        chkTxtView.setText(this._data[position]);

    return curView;
}

将您的自定义适配器更改为:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多