开发者

notifyDataSetChanged doesn't update list item

I'm trying to use notifyDataSetChanged to update the list item but when I click the checkbox, database changes but the the checkbox doesn't.

public class MyAdapter extends ResourceCursorAdapter {

    private final SQLiteDatabase db;
    private final LayoutInflater inflater;
    //private final ListView listView;

    public MyAdapter(final ListActivity activity, final Cursor cursor, final SQLiteDatab开发者_运维问答ase db) {
        super(activity, R.layout.textview_checkbox_row, cursor);
        this.db = db;
        this.inflater = LayoutInflater.from(activity);
        //this.listView = activity.getListView();
    }

    private static class ViewHolder {
        private TextView dictName;
        private CheckBox dictEnabled;
    }

    @Override
    public View newView(final Context context, final Cursor cursor, final ViewGroup parent) {
        final View view =
            inflater.inflate(R.layout.textview_checkbox_row, parent, false);

        // Create view holder and store it.
        final ViewHolder viewHolder = new ViewHolder();
        viewHolder.dictName = (TextView) view.findViewById(R.id.dictionaryName);
        viewHolder.dictEnabled =
            (CheckBox) view.findViewById(R.id.dictionaryEnabled);
        view.setTag(viewHolder);

        viewHolder.dictEnabled.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
                final int dictID = (Integer) buttonView.getTag();
                final ContentValues value = new ContentValues();
                final String where =
                    DatabaseHelper.DICTIONARY_ID_COLUMN + " = " + dictID;
                value.put(DatabaseHelper.DICTIONARY_ENABLED_COLUMN, isChecked == true
                        ? 1 : 0);
                db.update(DatabaseHelper.DICTIONARY_TABLE_NAME, value, where, null);

                //////// It doesn't update my list item. //////////
                notifyDataSetChanged();
            }
        });
        return view;
    }

    @Override
    public void bindView(final View view, final Context context, final Cursor cursor) {
        System.out.println("bindView " + cursor.getPosition() + " " + view);
        final ViewHolder viewHolder = (ViewHolder) view.getTag();

        // Get dictionary id
        viewHolder.dictEnabled.setTag(cursor.getInt(cursor.getColumnIndex(DatabaseHelper.DICTIONARY_ID_COLUMN)));

        // Populate the elements of list item.
        viewHolder.dictName.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.DICTIONARY_NAME_COLUMN)));
        viewHolder.dictEnabled.setChecked(cursor.getInt(cursor.getColumnIndex(DatabaseHelper.DICTIONARY_ENABLED_COLUMN)) == 0
                ? false : true);

    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜