我有一个Listview,listview的每一行都有一个复选框,现在我想根据来自数据库的数据动态检查或取消选中Checkboxes,因为我使用的是checkBox.setChecked(true)方法,但是复选框不正确根据来自数据库的数据检查和取消选中,请任何人帮助我。提前致谢。这是我的代码:
public class Adaptor_CategoryList extends ArrayAdapter<Category> {
public Context mContext;
public ArrayList<Category> listCategory;
public LayoutInflater inflater;
// public OnCheckedChangeListener listnerCheckBox;
public OnCheckedChangeListener listnerCheckBox;
// this arraylist for making checkbox checked and unchecked
public ArrayList<Integer> listCatIds;
public Adaptor_CategoryList(Context context, int resource,
List<Category> objects, OnCheckedChangeListener listener,
ArrayList<Integer> listIds) {
super(context, resource, objects);
// TODO Auto-generated constructor stub
mContext = context;
listCategory = (ArrayList<Category>) objects;
inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
listnerCheckBox = listener;
listCatIds = listIds;
} // constructor ends
public View getView(int position, View convertView, ViewGroup parent) {
View holder = convertView;
if (holder == null) {
holder = inflater.inflate(R.layout.dialog_categorylist, null);
}
if (listCategory.size() != 0) {
TextView txtName = (TextView) holder
.findViewById(R.id.EditItem_dialogtxtName);
CheckBox checkCategory = (CheckBox) holder
.findViewById(R.id.EditItem_dialogCheckBox);
checkCategory.setTag(position);
checkCategory.setOnCheckedChangeListener(listnerCheckBox);
// checkCategory.setOnCheckedChangeListener(listnerCheckBox);
Category objCategory = listCategory.get(position);
if (objCategory != null) {
if (listCatIds.size() > 0) {
// code for making checkbox checked accroding to item
// category
for (int i = 0; i < listCatIds.size(); i++) {
int catId = listCatIds.get(i);
int id = objCategory.getCatId();
if (id == catId) {
checkCategory.setChecked(true);
System.out
.println("checkbox is checked ////// id is "
+ id);
} else {
checkCategory.setChecked(false);
}
}
}
String strName = objCategory.getName();
if (strName != null) {
txtName.setText(strName);
}
}
}
return holder;
}// method ends
} //最终课程结束
这是我在其他Activty中定义的监听器:
CompoundButton.OnCheckedChangeListener listenerCheckBox = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
try {
// adding item to category on checkbox checked
if (isChecked) {
Object tag = buttonView.getTag();
if (tag != null) {
int position = (Integer) tag;
Category objCat = listCategory.get(position);
int catId = objCat.getCatId();
db.open();
long rowId = db.addItemToCategory(itemId, catId);
// Toast.makeText(mContext,
// "Add Id is"+rowId,Toast.LENGTH_LONG).show();
db.close();
}
}
// removing item from category on checkbox unchecked
else if (!isChecked) {
Object tag = buttonView.getTag();
if (tag != null) {
int position = (Integer) tag;
Category objCat = listCategory.get(position);
int catId = objCat.getCatId();
db.open();
long rowId = db.deleteItemFromCategory(
itemId + "".trim(), catId + "".trim());
db.close();
}
}
} catch (Exception e) {
// TODO: handle exception
}
}
};
但是,这个日志System.out.println(“复选框被检查////// id是”+ id);如果条件满足但是复选框没有改变它的状态,则在Logcat上打印。请给我一些建议。
答案 0 :(得分:1)
您必须更新列表视图,以便它可以反映新数据。
有两种方法可以做到这一点 - 第一种方法是在适配器上调用notifyDataSetChanged()
,这将刷新列表中的每个可循环视图。它只需要一行代码,但它有点浪费,导致不必要的失效和绘图。
第二种方法是手动调用适配器的getView
方法。这是我个人所做的:
public void updateSingleRow(int position)
{
// Get the view manually in order to update it, but only if
// the position in need of invalidation is visible in the list view.
if (position >= listView.getFirstVisiblePosition() && position < listView.getLastVisiblePosition())
{
View v = ((ViewGroup) listView.getChildAt(listView.getHeaderViewsCount() + (position - listView.getFirstVisiblePosition()))).getChildAt(0);
adapter.getView(position, v, listView);
}
}
答案 1 :(得分:0)
这很可能是因为您没有在UI线程上更新它。
试试这个:
runOnUiThread(new runnable(){
public void run(){
checkCategory.setChecked(true);
}
});