首页 > 解决方案 > notifyDataSetChange 不适用于适配器

问题描述

这是我的适配器代码

我尝试了更多方法,但仍然无法正常工作

不知道哪里有问题

public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.CommentViewHolder> {
    private Context mContext;
    private List<CommentModel> mData;
    RequestManager glide;
    private LayoutInflater mInflater = null;
    private OnItemClickListener mOnItemClickListener = null;

    public CommentAdapter(List<CommentModel> mData, Context mContext){
        super();
        this.mContext = mContext;
        this.mData = mData;
        this.glide = Glide.with(mContext);
    }


    @NonNull
    @Override
    public CommentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //View row = LayoutInflater.from(mContext).inflate(R.layout.row_comment,parent,false);

        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_comment, parent, false);
        CommentViewHolder commentViewHolder = new CommentViewHolder(v);
        return commentViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull CommentViewHolder holder, final  int position) {

        final CommentModel commentModel = mData.get(position);


        //glide.load(mData.get(position).getUserPhoto()).into(holder.img_user);
        Glide.with(mContext).load(mData.get(position).getUserPhoto()).into(holder.img_user);
        holder.tv_name.setText(mData.get(position).getUsername());
        holder.tv_content.setText(mData.get(position).getComment());
        holder.tv_date.setText(mData.get(position).getTime());
    }

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


    public void addTheCommentData(CommentModel commentModel){
        if(commentModel!=null){
            mData.add(commentModel);
            this.notifyDataSetChanged();
        }else {
            throw new IllegalArgumentException("No Data!");
        }

    }

    public interface OnItemClickListener {
        void onClick(View parent, int position);
    }


    public class CommentViewHolder extends RecyclerView.ViewHolder{

        ImageView img_user;
        TextView tv_name,tv_content,tv_date;


        public CommentViewHolder(View itemView) {
            super(itemView);
            img_user = itemView.findViewById(R.id.comment_user_img);
            tv_name = itemView.findViewById(R.id.comment_username);
            tv_content = itemView.findViewById(R.id.comment_comment);
            tv_date = itemView.findViewById(R.id.comment_date);
        }
    }
}

这是我的评论代码,

当我输入信息并提交时,它将写入我的服务器,但 notifyDataSetChanged 不起作用。

//This method will parse json data
    private void parseData(JSONArray array) {
        for (int i = 0; i < array.length(); i++) {
            //Creating the newFeedModel object

            CommentModel newCommentModel = new CommentModel();
            JSONObject json = null;
            try {
                //Getting json
                json = array.getJSONObject(i);

                String TAG_CommentID = "CommentID"; 
                String TAG_CommentFID = "CommentFID";
                String TAG_CommentUID = "CommentUID";
                String TAG_Comment = "Comment";
                String TAG_PostUserPhoto = "PostUserPhoto";
                String TAG_Username = "Username";
                String TAG_Time = "Time";
                //String TAG_CommentPhoto = "CommentPhoto"; 

                //Adding data to the newFeedModel object
                //Log.d("photo", json.getString(TAG_PostUserPhoto));
                newCommentModel.setCommentID(json.getString(TAG_CommentID));
                newCommentModel.setFeedID(json.getString(TAG_CommentFID));
                newCommentModel.setUserID(json.getString(TAG_CommentUID));
                newCommentModel.setComment(json.getString(TAG_Comment));
                newCommentModel.setUserPhoto(json.getString(TAG_PostUserPhoto));
                newCommentModel.setUsername(json.getString(TAG_Username));
                newCommentModel.setTime(json.getString(TAG_Time));
                //newCommentModel.setTimestamp(json.getString(TAG_CommentPhoto));

            } catch (JSONException e) {
                e.printStackTrace();
            }
            //Adding the newFeedModel object to the list
            //listCommentModel.add(newCommentModel);
            adapter.addTheCommentData(newCommentModel);
        }

        //Notifying the adapter that data has been added or changed

        this.adapter.notifyDataSetChanged();
        //getNewDate();
    }

但它不能工作,我不知道发生了什么。

谁能告诉我哪里需要修改?

我需要离开活动然后返回将显示。

标签: androidnotifydatasetchanged

解决方案


首先添加 CommentModel 的 ArrayList,然后通过适配器的构造函数添加您的适配器一个传递列表

CommentAdapter adapter;
ArrayList<CommentModel> list;

内部解析

list = new ArrayList<CommentModel>();
list.add(newCommentModel)
adapter = new CommentAdapter(list,this);

推荐阅读