Reputation: 186
i am making a form where i have to use check box in list view.i have made a list with check box.i cant show information in edittext when the list items are checked and unchecked. here is my code----
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;
}
}
Upvotes: 0
Views: 65
Reputation: 1037
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;
}
change your custom adapter to this:
Upvotes: 1