【问题标题】:notifyDataSetChanged in Curser adapter not working with listfragmentCurser 适配器中的 notifyDataSetChanged 不能与 listfragment 一起使用
【发布时间】:2013-05-29 00:29:35
【问题描述】:

我有一个列表片段,它显示来自数据库的列表。除了一个我似乎无法清理的小物品外,我的一切工作正常。列表视图看起来像这样。

-----------------------------------------------------
text text text | some more text | delete row button |
-----------------------------------------------------

我的问题:当用户按下删除行按钮时,该行会从数据库中删除,但在活动停止并重新开始之前不会从屏幕上删除。

我知道notifyDataSetChanged() 是更新这些列表的首选方法,但它似乎没有做任何事情......无论如何,下面是我的光标适配器代码,其中调用了notifyDataSetChanged()

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;
    }




}

感谢您的帮助。

更新:

我的适配器中的新按钮代码:

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();


    }
};

【问题讨论】:

  • 你是如何解决问题的
  • 嘿 Marek 你解决了这个问题如果是的话请告诉我们答案。

标签: android android-listfragment


【解决方案1】:

我之前也遇到过同样的问题,在这种情况下,我认为 notifyDataSetChanged 无法单独帮助您。更改的实际上是您的光标,因此您需要使用自定义适配器上的函数changeCursor 重新加载它。这是一个例子:

changeCursor(yourCursor);
notifyDataSetChanged();

这是我在 Fragment 类中如何管理它的完整代码:

ImageButton newCat = (ImageButton)categoriesListTitle.findViewById(R.id.new_category);

            newCat.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(final View v) {
                    categoriesDialog.dismiss();

                    AlertDialog.Builder newCatAlert = new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), R.style.AlertDialogStyle));

                    newCatAlert.setTitle("New category");

                    // Set an EditText view to get user input 
                    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));//Here I update the cursor used in my adapter
                            //In your case (you do not use a dialog), you have to add this line I think:
                            //mAdapter.notifyDataSetChanged();

                            categoriesDialog.show();
                        }

                    });

                    newCatAlert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            dialog.cancel();
                        }
                    });
                    newCatAlert.show();
                }
            });


            categoriesLV.setAdapter(mAdapter);

【讨论】:

  • 我在删除数据库后添加了Curser newCursor = getCursor(); changeCursor(newCursor);,但它什么也没做......它不会抛出错误,它只是什么都不做!感谢您的帮助!
  • 我编辑了我的答案,就我而言,我不需要使用notifyDataSetChanged,因为我正在使用弹出窗口再次显示列表,但对你来说它可能有用。试一试。
  • 我已经编辑了我的主要帖子,以便通过这些更改更好地显示我的适配器中我的按钮的代码...我想验证您是否知道我正在使用列表片段...不确定这是否重要,因为这发生在适配器而不是片段中。
  • 什么会返回你的getCursor 函数?它应该再次调用您的数据库以获取更新的游标。
  • 我觉得这很难,因为我在片段中使用了 loadermanager。尽管我已经尝试过,但我无法在适配器内部启动加载器......而且因为它在片段内部而适配器在片段外部,所以我无法告诉它重新 -查询...对不起,我不太擅长这个。
猜你喜欢
  • 2011-04-26
  • 1970-01-01
  • 1970-01-01
  • 2015-02-27
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多