在recyclerview中调用notifydatasetchanged时位置发生了变化

时间:2015-12-15 12:26:28

标签: android android-recyclerview

我使用RecyclerView显示行列表,每行包含下载按钮。当特定行的项目下载完成时,notifydatasetchanged将调用并下载行的下载按钮必须更改为" read"。但在我的情况下,其他行按钮更改而不是下载的行。请任何人建议我如何解决此问题。

// adapter
class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
        private Cursor mDataset;


        public class MyViewHolder extends RecyclerView.ViewHolder {
            // each data item is just a string in this case


            public Button download_btn;

            public MyViewHolder(View v) {
                super(v);

                download_btn = (Button) v.findViewById(R.id.download_btn);

            }
        }


        void setDataSet(Cursor mDataset) {
            this.mDataset = mDataset;
            notifyDataSetChanged();
        }

        public MyAdapter(Cursor myDataset) {

            this.mDataset = myDataset;

        }

        // Create new views (invoked by the layout manager)
        @Override
        public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,

                v = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_layout, parent, false);
            MyViewHolder vh = new MyViewHolder(v);

            return vh;
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {

            final MyViewHolder myViewHolder = (MyViewHolder) holder;

            this.mDataset.moveToPosition(position);

            switch (this.mDataset.getInt(this.mDataset.getColumnIndex("IS_DOWNLOADED"))) {

                case 0:
                   // when download not completed. 
                     myViewHolder.download_btn.setText("Download");
                     break;


                case 1:
                  // when download  completed.  
                    myViewHolder.download_btn.setText("Read");
                    break;
            }
        }        
        @Override
        public int getItemCount() {

          return this.mDataset.getCount();

        }

    }




// notifydatasetchanged called when download completes

  myAdapter.setDataSet(cursor);

当调用notifydatasetchanged时,它使用旧的游标值而不是新游标。

0 个答案:

没有答案