Android 使用setChecked动态选中复选框(True)

Android 使用setChecked动态选中复选框(True),android,Android,我有一个Listview,Listview的每一行都有一个复选框,现在我想根据来自数据库的数据动态选中或取消选中复选框,为此我使用checkbox.setChecked(true)方法,但是没有根据来自数据库的数据正确选中和取消选中复选框,请任何人帮助我。提前谢谢。这是我的密码: public class Adaptor_CategoryList extends ArrayAdapter<Category> { public Context mContext; public Arra

我有一个Listview,Listview的每一行都有一个复选框,现在我想根据来自数据库的数据动态选中或取消选中复选框,为此我使用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

但是,此日志系统为.out.println(“选中复选框时,/////id为“+id”);如果条件满足,则在Logcat上打印,但复选框不更改其状态。请提供一些建议。

这很可能是因为您没有在UI线程上更新它

试试这个:

runOnUiThread(new runnable(){
    public void run(){
        checkCategory.setChecked(true);
    }
});

您必须更新列表视图,使其能够反映新数据。 有两种方法可以做到这一点-第一种是在适配器上调用
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);
    }
}
public void updateSingleRow(int位置)
{
//手动获取视图以进行更新,但仅当
//需要失效的位置在列表视图中可见。
如果(位置>=listView.getFirstVisiblePosition()&&position
它在没有侦听器的情况下工作吗?即,不带行
checkCategory.setOnCheckedChangeListener(listnerCheckBox)但我已经从Ui线程更新了它。请解释你的答案。实际上这个listview在对话框中,所以我已经调用了notifydatasetChanged()方法。
    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);
    }
}