无法更新RecyclerView中的TextView字段

时间:2015-05-10 21:45:53

标签: android listview android-sqlite android-recyclerview

我正在从ListView迁移到RecyclerView,我最后一件事就是更新其中一个字段。我的应用程序有一个简单的列表,在互联网上找到一些图像当您选择一个时,图像会下载到手机以供以后离线查看。适配器从SQLite database获取数据,因此当您点击某个图像时,数据库会更新(文本更改从“点击此处下载”“已下载”)并且RecyclerView应该遵循。

我遇到与ListView相同的问题,但每次App更新db时我都会调用populateList();。我知道这不是理想的解决方案,但它有效。现在我想用notifyDataSetChanged()或更好notifyItemChanged(position)来做到这一点,但我无法让它发挥作用。

无论如何这里是代码,抱歉有点乱。这只是一个测试代码(RecyclerView代码来自样本):

public class RecyclerFragment extends Fragment {

    private imgAdapter mAdapter;
    private DatabaseHandler db;
    private ProgressDialog mProgressDialog;


    public RecyclerFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {



        db = new DatabaseHandler(getActivity());
        List<Images> listdb = db.getImages();
        mAdapter = new ImgAdapter(getActivity(), listdb);
        db.close();

        View view = inflater.inflate(R.layout.fragment_main, container, false);
        RecyclerView recyclerView = (RecyclerView) view.findViewById(android.R.id.list);
        recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new GridLayoutManager(
                getActivity(), getResources().getInteger(R.integer.list_columns)));
        recyclerView.setAdapter(mAdapter);
        return view;
    }


    private class ImgAdapter extends RecyclerView.Adapter<ViewHolder>
            implements ItemClickListener {

        public List<Images> mList;
        private Context mContext;

        public ImgAdapter(Context context, List<Images> listdb) {
            super();
            mContext = context;
            mList = listdb;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            View view = inflater.inflate(R.layout.list_row1, parent, false);
            return new ViewHolder(view, this);
        }

        @Override
        public void onBindViewHolder(final ViewHolder holder, final int position) {
            Images image = mList.get(position);
            holder.mTitle.setText(image.getTitle());
            holder.mDownloadStatus.setText(image.getDownloadStatus());
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public int getItemCount() {
            return mList == null ? 0 : mList.size();
        }


        @Override
        public void onItemClick(View view, int position) {
            switch (position) {
                case 0:
                    Log.v("POSITION 0:", " " + position);
                    break;
                case 1:
                   /*
                   some logic nvm
                    */
                   String imgurl = "http:/...";
                   String imagename = "Second image";
                   new GetImages(imgurl, imagename).execute();

                   /*
                   I've tried mAdapter.notitfy... , no luck aswell
                   This does nothing:
                    */
                   notifyItemChanged(position);
                   notifyDataSetChanged();
                 break;
               //....

            }
        }
    }

    private static class ViewHolder extends RecyclerView.ViewHolder
            implements View.OnClickListener {

        TextView mTitle;
        TextView mDownloadStatus;
        ItemClickListener mItemClickListener;

        public ViewHolder(View view, ItemClickListener itemClickListener) {
            super(view);
            mTitle = (TextView) view.findViewById(R.id.text1);
            mDownloadStatus = (TextView) view.findViewById(R.id.text2);
            mItemClickListener = itemClickListener;
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            mItemClickListener.onItemClick(v, getPosition());
        }
    }

    interface ItemClickListener {
        void onItemClick(View view, int position);
    }

    /*
     This is my AsyncTask class used for downloading image and updating db
     I removed some code just to to make it cleaner
      */
    private class GetImages extends AsyncTask<Object, Object, Object> {
        private final String requestUrl;
        private final String imagename_;
        private String imagename;
        public int numberofbits;


        private GetImages(String requestUrl, String _imagename_) {
            this.requestUrl = requestUrl;
            this.imagename_ = _imagename_;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            //pDialog...
        }

        @Override
        protected Object doInBackground(Object... objects) {
            try {
                URL url = new URL(requestUrl);
                URLConnection conn = url.openConnection();
                bitmap = BitmapFactory.decodeStream(conn.getInputStream());
                numberofbits = bitmap.getByteCount();
            } catch (Exception ignored) {
            }
            return null;
        }


        @Override
        protected void onPostExecute(Object o) {
            if (!ImageStorage.checkifImageExists(imagename)) {
                ImageStorage.saveToSdCard(bitmap, imagename_);
            }

            if (numberofbits > 50) {
                db = new DatabaseHandler(getActivity());
                db.updateImages(new Images(dbid, "Downloaded", imagename_));
                db.close();
                //populateList(); -> THIS I USED FOR A LISTVIEW
                Toast.makeText(getActivity(), "Downloaded!", Toast.LENGTH_SHORT).show();
                mProgressDialog.dismiss();

                //If image gets downloaded open it in Viewer class
                Intent intent = new Intent(getActivity(), DisplayImage.class);
                intent.putExtra("imagePath", path);
                startActivity(intent);

            } else {
                //Toast.makeText(getActivity(), "Unable to download", Toast.LENGTH_SHORT).show();
                mProgressDialog.dismiss();

            }
        }
    }
}

这是Images类

public class Images {

    //private variables
    private int _id;
    private String _title;
    private String _downloadstatus;

    // Empty constructor
    public Images () {

    }

    // constructor
    public Images(int id, String title, String downloadstatus) {
        this._id = id;
        this._title = title;
        this._downloadstatus = downloadstatus;
    }


    public int getID() {
        return this._id;
    }

    public void setID(int id) {
        this._id = id;
    }

    public String getTitle() {
        return this._title;
    }

    public void setTitle(String title) {
        this._title = title;
    }


    public String getDownloadStatus() {
        return this._downloadstatus;
    }

    public void setDownloadStatus(String downloadstatus) {
        this._downloadstatus = downloadstatus;
    }
}

这是一个XML(我正在尝试更新“text2”):

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:foreground="?attr/selectableItemBackground"
    android:layout_height="wrap_content"
    android:padding="8dp">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        >


        <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="@dimen/imagename"
            android:textStyle="bold"
            android:paddingStart="4sp"
            android:paddingEnd="40sp" />

        <TextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/text1"
            android:textSize="@dimen/downloaded"
            android:paddingStart="4sp"
            android:paddingEnd="1sp"
            android:textColor="@color/graydownloaded"        />


    </RelativeLayout>

</FrameLayout>

2 个答案:

答案 0 :(得分:1)

尝试这样做:在适配器中覆盖getItemId方法,如下所示

@Override
public long getItemId(int position) {
  return mList.get(position).getID();
}

recyclerView.setHasFixedSize(true)之后添加此行:

recyclerView.setHasStableId(true);

编辑:

mAdapter.setHasStableId(true);

让我知道是否有诀窍。

答案 1 :(得分:1)

好的,我终于开始工作,实际上非常简单......

//....     
if (numberofbits > 50) {
                    db = new DatabaseHandler(getActivity());
                    db.updateImages(new Images(dbid, "Downloaded", imagename_));
                    //This is what I added:
                    List<Images> listdb = db.getImages();
                    mAdapter = new ImgAdapter(getActivity(), listdb);
                    recyclerView.setAdapter(mAdapter);
                    db.close();
                    //populateList(); -> THIS I USED FOR A LISTVIEW
                    Toast.makeText(getActivity(), "Downloaded!", Toast.LENGTH_SHORT).show();
                    mProgressDialog.dismiss();

                    //If image gets downloaded open it in Viewer class
                    Intent intent = new Intent(getActivity(), DisplayImage.class);
                    intent.putExtra("imagePath", path);
                    startActivity(intent);

                } else {
//...