Я создаю свое первое приложение для Android, и у меня возникла проблема, на которую я не могу найти ответ в Google.
Мне нужен список элементы с флажками. Я хочу, чтобы и сам элемент, и флажок были активными.
public class MyItem extends ListActivity {
private ArrayList<MyItem> items;
public void onCreate(Bundle savedInstanceState) {
/* code which creates instances of MyItem and inserts them on the *list* variable */
MyArrayAdapter adapter = new MyArrayAdapter(this, R.layout.my_item, list);
setListAdapater(adapter);
setContentView(R.layout.items_list);
}
public onListItemClick(ListView l, View v, int position, long id){
//handles the click on an item
}
public class MyArrayAdapter extends ArrayAdapter<MyItem>{
private MyItem item;
public MyArrayAdapter(Context context, int resourceId, ArrayList<MyItem> list){
//code for the constructor
}
public getView(int position, View convertView, ViewGroup parent){
convertView = inflater.inflate(resourceId, null);
this.item = list.get(position);
if (this.item == null) {
return convertView;
}
else{
if (resourceId == R.layout.my_item) {
final CheckBox cb = (CheckBox)convertView.findViewById(R.id.checkbox);
if(cb != null){
//initially
if(chosen)
cb.setChecked(true);
else
cb.setChecked(false);
//set listener
cb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if(cb.isChecked())
chosen = true;
else
chosen = false;
}
});
}
}
return convertView;
}
}
}
Не беспокойтесь о выбранной переменной. Я написал это просто в код. Фактически это соответствует значению в базе данных. Нажатие на элемент работает нормально. Однако, когда я устанавливаю флажок, происходит следующее:
I был бы признателен за любую помощь, которую вы могли бы мне предоставить. Заранее спасибо.