Update text within card view list?

江枫思渺然 提交于 2019-12-06 04:21:32

notifyDataSetChanged should be used as a last resort, since it does not specify what has happened and therefore the adapter asumes nothing is valid anymore, which causes the list to not only rebind, but also relayout all visible views. You should instead use one of the notifyItem[Range][Inserted|Changed|Removed]() methods instead, in your case notifyItemChanged(int position) will do.

In updateText() you edit the text, but don't store the result in any field. Then later, bindViewHolder() will be called, which calls setText(getTime()) and resets the text to its previous value. That is probably why nothing changes. You should add a method to your ViewHolder which first stores the result and then calls TextView.setText().

If your onClick() is never called, you might want to check that you have set the view focusable and clickable in its layout-file.

This is my first awnser on StackOverflow and English is not my first language, so excuse me if I typed something wrong.

Darxval

Two things,

  • I am not actually seeing where your updateText is being called.
  • You should call notifyDataSetChanged() to update the adapter to have your adapter redraw the list.

I added a simple recycler view and I added a listener to a textView. It should work after you call the text update and then call notifyDataSetChanged(), see below

public class MyRecyclerAdapter extends RecyclerView.Adapter<FeedListRowHolder> {

    private List<FeedItem> feedItemList;
    private Context mContext;

    public MyRecyclerAdapter(Context context, List<FeedItem> feedItemList) {
        this.feedItemList = feedItemList;
        this.mContext = context;
    }

    @Override
    public FeedListRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_row, null);
        FeedListRowHolder mh = new FeedListRowHolder(v);
        return mh;
    }

    @Override
    public void onBindViewHolder(FeedListRowHolder feedListRowHolder, int i) {
        final FeedItem feedItem = feedItemList.get(i);

        feedListRowHolder.title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateText(feedItem);
                notifyDataSetChanged();
            }
        });
        Picasso.with(mContext).load(feedItem.getThumbnail())
            .error(R.drawable.placeholder)
            .placeholder(R.drawable.placeholder)
            .into(feedListRowHolder.thumbnail);

        feedListRowHolder.title.setText(Html.fromHtml(feedItem.getTitle()));
    }

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

    private void updateText(FeedItem feedItem)
    {
        feedItem.setTitle("TEST!");
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!