ExpandableListView 的 ChildView 中的 CheckBox
CheckBox in ChildView of ExpandableListView
在我的 Android 应用程序中,我有一个 ExpandableListView,每个组只有一个子项,其中包含一个复选框。我在设置复选框的值时遇到问题。
我正在从数据库中检索复选框的值并将其存储在整数 ArrayList 中,值 0 和 1 分别表示已选中和未选中
ArrayList<Integer> trustedList = new ArrayList<Integer>();
最初,我将数据库中的所有条目都设为 1。
ExpandableListView 的 Adapter class 如下:
class DataAdapter extends BaseExpandableListAdapter {
private Context context;
LayoutInflater lat = getLayoutInflater();
CheckBox cb;
public DataAdapter(Context context) {
// TODO Auto-generated constructor stub
this.context = context;
}
@Override
public View getChildView(int group, int child, boolean arg2, View v,
ViewGroup arg4) {
// TODO Auto-generated method stub
v = lat.inflate(R.layout.inflate_list, null);
cb = (CheckBox) v.findViewById(R.id.checkBox1);
if (trustedList.get(group) == 1)
cb.setChecked(true);
else
cb.setChecked(false);
return v;
}
}
但是我得到了复选框的未选中值。此外,在选中复选框并折叠和展开组时,我再次取消选中复选框。
class DataAdapter extends BaseExpandableListAdapter {
private Context context;
LayoutInflater lat = getLayoutInflater();
CheckBox cb;
public DataAdapter(Context context) {
// TODO Auto-generated constructor stub
this.context = context;
}
@Override
public View getChildView(int group, int child, boolean arg2, View v,
ViewGroup arg4) {
// TODO Auto-generated method stub
v = lat.inflate(R.layout.inflate_list, null);
cb = (CheckBox) v.findViewById(R.id.checkBox1);
if (trustedList.get(group) == 1)
cb.setChecked(true);
else
cb.setChecked(false);
cb.setOnCheckChangeListener(new CheckchangeListener(group) );
return v;
}
class CheckchangeListener implements OnCheckedChangeListener {
private int position;
public CheckchangeListener(int position) {
// TODO Auto-generated constructor stub
this.position= position;
}
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
//updateyour list and database here
} else {
//updateyour list and database here
}
}
}
}
在我的 Android 应用程序中,我有一个 ExpandableListView,每个组只有一个子项,其中包含一个复选框。我在设置复选框的值时遇到问题。
我正在从数据库中检索复选框的值并将其存储在整数 ArrayList 中,值 0 和 1 分别表示已选中和未选中
ArrayList<Integer> trustedList = new ArrayList<Integer>();
最初,我将数据库中的所有条目都设为 1。
ExpandableListView 的 Adapter class 如下:
class DataAdapter extends BaseExpandableListAdapter {
private Context context;
LayoutInflater lat = getLayoutInflater();
CheckBox cb;
public DataAdapter(Context context) {
// TODO Auto-generated constructor stub
this.context = context;
}
@Override
public View getChildView(int group, int child, boolean arg2, View v,
ViewGroup arg4) {
// TODO Auto-generated method stub
v = lat.inflate(R.layout.inflate_list, null);
cb = (CheckBox) v.findViewById(R.id.checkBox1);
if (trustedList.get(group) == 1)
cb.setChecked(true);
else
cb.setChecked(false);
return v;
}
}
但是我得到了复选框的未选中值。此外,在选中复选框并折叠和展开组时,我再次取消选中复选框。
class DataAdapter extends BaseExpandableListAdapter {
private Context context;
LayoutInflater lat = getLayoutInflater();
CheckBox cb;
public DataAdapter(Context context) {
// TODO Auto-generated constructor stub
this.context = context;
}
@Override
public View getChildView(int group, int child, boolean arg2, View v,
ViewGroup arg4) {
// TODO Auto-generated method stub
v = lat.inflate(R.layout.inflate_list, null);
cb = (CheckBox) v.findViewById(R.id.checkBox1);
if (trustedList.get(group) == 1)
cb.setChecked(true);
else
cb.setChecked(false);
cb.setOnCheckChangeListener(new CheckchangeListener(group) );
return v;
}
class CheckchangeListener implements OnCheckedChangeListener {
private int position;
public CheckchangeListener(int position) {
// TODO Auto-generated constructor stub
this.position= position;
}
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
//updateyour list and database here
} else {
//updateyour list and database here
}
}
}
}