notifyDataSetChanged在游标适配器中与ListFragment无法正常工作。

14 浏览
0 Comments

notifyDataSetChanged在游标适配器中与ListFragment无法正常工作。

我有一个显示来自数据库的列表的ListFragment。除了一个我似乎无法解决的小问题外,我已经正确地完成了所有工作。列表视图大致如下。\n

-----------------------------------------------------
文本文本文本 | 更多文本 | 删除行按钮 |
-----------------------------------------------------

\n我的问题是:当用户按下删除行按钮时,行从数据库中被删除,但直到活动停止并重新开始后才会从屏幕上移除。\n我知道notifyDataSetChanged()是更新这些列表的首选方法,但它似乎没有起作用...无论如何,以下是我的游标适配器的代码,其中调用了notifyDataSetChanged()。\n

public class CalcCursorAdapter extends SimpleCursorAdapter{
    private Context mContext;
    private ListView mListView;
    private int mLayout;
    private Cursor mcursor;
    protected static class ViewHolder {
        protected TextView text;
        protected ImageButton button;
        private int position;
      }
    @SuppressWarnings("deprecation")
    public CalcCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) 
    {
        super(context, layout, c, from, to);
        this.mContext = context;
        this.mLayout = layout;
        this.mcursor = c;
        //mListView = .getListView();
    }       
    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        TextView summary = (TextView)view.findViewById(R.id.calctvPrice);
        TextView savings = (TextView)view.findViewById(R.id.calctvSavings);
        TextView percentOff = (TextView)view.findViewById(R.id.calctvpercentOff);
        summary.setText(cursor.getString(cursor.getColumnIndexOrThrow("qcFinalPrice")));
    }
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        ViewHolder holder = new ViewHolder();
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.calc_list_item, parent, false);
        holder.button = (ImageButton) v.findViewById(R.id.qcbtnDelete);
        holder.button.setOnClickListener(deleteButton);
        holder.position = cursor.getInt(cursor.getColumnIndexOrThrow("_id"));
        bindView(v, context, cursor);
        v.setTag(holder);
        return v;
    }
    private OnClickListener deleteButton = new OnClickListener() {
        @SuppressWarnings("deprecation")
        public void onClick(View v){
            View view = (View) v.getParent();
            ViewHolder holder = (ViewHolder) view.getTag();
            int position = holder.position;
            DbHelper mDbHelper;
            mDbHelper = new DbHelper(mContext);
            mDbHelper.open();
            mDbHelper.deleteCalc(position);
            mDbHelper.close();
            String test = Integer.toString(position);
            Toast.makeText(mContext.getApplicationContext(), test, Toast.LENGTH_SHORT).show();  
            notifyDataSetChanged();
            //ListView list = getListView();
        }
    };
    public long qcItemId(int position) {
        return position;
    }
}

\n谢谢你的帮助。\n更新:\n我的适配器中的新按钮代码:\n

private OnClickListener deleteButton = new OnClickListener() {
    @SuppressWarnings("deprecation")
    public void onClick(View v){
        v.invalidate();
        View view = (View) v.getParent();
        view.invalidate();
        ViewHolder holder = (ViewHolder) view.getTag();
        int position = holder.position;
        DbHelper mDbHelper;
        mDbHelper = new DbHelper(mContext);
        mDbHelper.open();
        mDbHelper.deleteCalc(position);
        mDbHelper.close();
        String test = Integer.toString(position);
        Toast.makeText(mContext.getApplicationContext(), test, Toast.LENGTH_SHORT).show();  
        Cursor newCursor = getCursor();
        changeCursor(newCursor);
        notifyDataSetChanged();
        //ListView list = getListView();
    }
};

0
0 Comments

问题出现的原因是在CursorAdapter中使用notifyDataSetChanged()方法不起作用。解决方法是使用changeCursor()方法重新加载cursor。具体代码如下:

changeCursor(yourCursor);
notifyDataSetChanged();

在Fragment类中的完整代码如下:

ImageButton newCat = (ImageButton)categoriesListTitle.findViewById(R.id.new_category);
newCat.setOnClickListener(new OnClickListener() {
    public void onClick(final View v) {
        categoriesDialog.dismiss();
        AlertDialog.Builder newCatAlert = new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), R.style.AlertDialogStyle));
        newCatAlert.setTitle("New category");
        final EditText newCatET = new EditText(v.getContext());
        newCatAlert.setView(newCatET);
        newCatAlert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                String newCatName = newCatET.getText().toString();
                db.addCategory(newCatName);
                mAdapter.changeCursor(db.getAllCategoriesByRate(currentRate));
                categoriesDialog.show();
            }
        });
        newCatAlert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                dialog.cancel();
            }
        });
        newCatAlert.show();
    }
});
categoriesLV.setAdapter(mAdapter);

问题的根本原因是getCursor()函数没有正确返回更新后的cursor。你应该在Fragment中调用changeCursor()函数,而不是在Adapter中。另外,你可以尝试使用notifyDataSetChanged()方法。如果你使用的是ListFragment,你需要将监听器移回到ListFragment类中。

0