PostValue有效,但SetValue不能MutableLiveData-AndroidX

时间:2019-11-06 06:41:06

标签: java android android-recyclerview android-databinding android-livedata

在OnCreate中执行

public void Start() {
            ModelSingleHotTrendingNews modelSingleHotTrendingNewsX = ViewModelProviders.of(getActivity()).get(ModelSingleHotTrendingNews.class);
            modelSingleHotTrendingNewsX.gametitle.setValue("Start");
            modelSingleHotTrendingNewsX.likecounter.setValue(10);
            modelSingleHotTrendingNewsX.islike.setValue(10);
    }

It should be 10

当我按下“赞”按钮时执行

public void Finish() {
            ModelSingleHotTrendingNews modelSingleHotTrendingNewsX = ViewModelProviders.of(getActivity()).get(ModelSingleHotTrendingNews.class);
            modelSingleHotTrendingNewsX.gametitle.postValue("Finish");
            modelSingleHotTrendingNewsX.likecounter.postValue(11);
            modelSingleHotTrendingNewsX.islike.postValue(11);
    }

This is Correct because the number is updated

这是我的模特

public class ModelSingleHotTrendingNews extends ViewModel implements Serializable {

private String idcontent;
public MutableLiveData<String> gametitle = new MutableLiveData<>();

private String gamedate;
private String gameimage;
private String gamedescription;

private int seencounter;
private int commentcounter;
public MutableLiveData<Integer> likecounter = new MutableLiveData<>();

private int iscomment;
public MutableLiveData<Integer> islike = new MutableLiveData<>();
private int trendingnewssaving;

private String value;
private String message;



public ModelSingleHotTrendingNews() {
}

public ModelSingleHotTrendingNews(MutableLiveData<String> gametitle) {
    this.gametitle = gametitle;
}

public ModelSingleHotTrendingNews(String idcontent, String gamedate, String gameimage, String gamedescription, int seencounter, int commentcounter, int iscomment, int trendingnewssaving, String value, String message) {
    this.idcontent = idcontent;
    this.gamedate = gamedate;
    this.gameimage = gameimage;
    this.gamedescription = gamedescription;
    this.seencounter = seencounter;
    this.commentcounter = commentcounter;
    this.iscomment = iscomment;
    this.trendingnewssaving = trendingnewssaving;
    this.value = value;
    this.message = message;
}


public MutableLiveData<String> getGametitle() {
    gametitle.setValue(gametitle.getValue());
    return gametitle;
}

public void setGametitle(MutableLiveData<String> gametitle) {
    this.gametitle = gametitle;
}


public MutableLiveData<Integer> getLikecounter() {
    likecounter.setValue(likecounter.getValue());
    return likecounter;
}

public void setlikecounter(MutableLiveData<Integer> likecounter) {
    this.likecounter = likecounter;
}

public MutableLiveData<Integer> getIslike() {
    islike.setValue(islike.getValue());
    return islike;
}

public void setIslike(MutableLiveData<Integer> islike) {
    this.islike = islike;
}


public String getIdcontent() {
    return idcontent;
}

public void setIdcontent(String idcontent) {
    this.idcontent = idcontent;
}




public String getGamedate() {
    return gamedate;
}

public void setGamedate(String gamedate) {
    this.gamedate = gamedate;
}

public String getGameimage() {
    return gameimage;
}

public void setGameimage(String gameimage) {
    this.gameimage = gameimage;
}

public String getGamedescription() {
    return gamedescription;
}

public void setGamedescription(String gamedescription) {
    this.gamedescription = gamedescription;
}

public int getSeencounter() {
    return seencounter;
}

public void setSeencounter(int seencounter) {
    this.seencounter = seencounter;
}

public int getCommentcounter() {
    return commentcounter;
}

public void setCommentcounter(int commentcounter) {
    this.commentcounter = commentcounter;
}



public int getIscomment() {
    return iscomment;
}

public void setIscomment(int iscomment) {
    this.iscomment = iscomment;
}



public int getTrendingnewssaving() {
    return trendingnewssaving;
}

public void setTrendingnewssaving(int trendingnewssaving) {
    this.trendingnewssaving = trendingnewssaving;
}

public String getValue() {
    return value;
}

public void setValue(String value) {
    this.value = value;
}

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}
}

这是适配器

public class AdapterSingleHotTrendingNews extends RecyclerView.Adapter{

private Context mContext;
private ArrayList<ModelSingleHotTrendingNews> modelSingleHotTrendingNews;

private RecyclerViewClickListener mListener;


public AdapterSingleHotTrendingNews(Context mContext, ArrayList<ModelSingleHotTrendingNews> modelSingleHotTrendingNews, RecyclerViewClickListener mListener) {
    this.mContext = mContext;
    this.modelSingleHotTrendingNews = modelSingleHotTrendingNews;
    this.mListener = mListener;
}




public void setmodelSingleHotTrendingNewsSD(ArrayList<ModelSingleHotTrendingNews> modelSingleHotTrendingNews) {
    this.modelSingleHotTrendingNews = modelSingleHotTrendingNews;
    notifyDataSetChanged();
}



//Container
@Override
public SingleHotTrendingNewsViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
    /*View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_single_hottrendingnews, null);
    return new SingleHotTrendingNewsViewHolder(v, mListener);*/


    View v;
    /*ModelSingleHotTrendingNews modelSingleHotTrendingNewsX;
        modelSingleHotTrendingNewsX = modelSingleHotTrendingNews.get(0);
        fragmentTabhotBinding.setRfg(modelSingleHotTrendingNewsX);*/
    final ItemSingleHottrendingnewsBinding itemSingleHottrendingnewsBinding;
    itemSingleHottrendingnewsBinding = DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()),
            R.layout.item_single_hottrendingnews, viewGroup, false);
    itemSingleHottrendingnewsBinding.setLifecycleOwner((FragmentActivity) mContext);

    final ModelSingleHotTrendingNews modelSingleHotTrendingNewsX;
    modelSingleHotTrendingNewsX = new ViewModelProvider((FragmentActivity) mContext).get(ModelSingleHotTrendingNews.class);
    modelSingleHotTrendingNewsX.gametitle.observe((FragmentActivity) mContext, new Observer<String>() {
        @Override
        public void onChanged(String s) {
            itemSingleHottrendingnewsBinding.TVGameTitle.setText(s);
        }
    });
    modelSingleHotTrendingNewsX.likecounter.observe((FragmentActivity) mContext, new Observer<Integer>() {
        @Override
        public void onChanged(Integer integer) {
            itemSingleHottrendingnewsBinding.TVLikeCounter.setText(String.valueOf(integer));
        }
    });
    modelSingleHotTrendingNewsX.islike.observe((FragmentActivity) mContext, new Observer<Integer>() {
        @Override
        public void onChanged(Integer integer) {
            if(integer == 0){
                itemSingleHottrendingnewsBinding.IMGLikeView.setImageResource(R.drawable.heart_off);
            }else if(integer == 1){
                itemSingleHottrendingnewsBinding.IMGLikeView.setImageResource(R.drawable.heart_on);
            }
        }
    });


    return new SingleHotTrendingNewsViewHolder(itemSingleHottrendingnewsBinding, mListener);
}


//Fill Container with Model Setter Getter
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

    final SingleHotTrendingNewsViewHolder singleHotTrendingNewsViewHolder = (SingleHotTrendingNewsViewHolder) holder;
    final ModelSingleHotTrendingNews modelSingleHotTrendingNewsX = modelSingleHotTrendingNews.get(position);


    //Set
    singleHotTrendingNewsViewHolder.TVGameDate.setText(modelSingleHotTrendingNewsX.getGamedate());
    singleHotTrendingNewsViewHolder.TVGameTitle.setText(modelSingleHotTrendingNewsX.gametitle.getValue());
    Glide.with(mContext).load(modelSingleHotTrendingNewsX.getGameimage()).into(singleHotTrendingNewsViewHolder.IMGGameImage);
    /*singleHotTrendingNewsViewHolder.IMGGameImage.setImageResource(modelSingleHotTrendingNewsX.getGameimage());*/

    singleHotTrendingNewsViewHolder.TVSeenCounter.setText(String.valueOf(modelSingleHotTrendingNewsX.getSeencounter()));
    singleHotTrendingNewsViewHolder.TVCommentCounter.setText(String.valueOf(modelSingleHotTrendingNewsX.getCommentcounter()));
    singleHotTrendingNewsViewHolder.TVLikeCounter.setText(modelSingleHotTrendingNewsX.likecounter.getValue()));

    if (modelSingleHotTrendingNewsX.getIscomment() == 0) {
        singleHotTrendingNewsViewHolder.IMGCommentView.setImageResource(R.drawable.comment_off);
    } else if (modelSingleHotTrendingNewsX.getIscomment() == 1) {
        singleHotTrendingNewsViewHolder.IMGCommentView.setImageResource(R.drawable.comment_on);
    }

    /*if (modelSingleHotTrendingNewsX.islike.getValue() != null){
        if (modelSingleHotTrendingNewsX.islike.getValue().equals(0)) {
            singleHotTrendingNewsViewHolder.IMGLikeView.setImageResource(R.drawable.heart_off);
        } else if (modelSingleHotTrendingNewsX.islike.getValue().equals(1)) {
            singleHotTrendingNewsViewHolder.IMGLikeView.setImageResource(R.drawable.heart_on);
        }
    }*/

    if(modelSingleHotTrendingNewsX.getTrendingnewssaving() == 0) {
        singleHotTrendingNewsViewHolder.ICONHotTrendingNewsSaving.setImageResource(R.drawable.saved_off);
        singleHotTrendingNewsViewHolder.ICONHotTrendingNewsSaving.setVisibility(View.GONE);
    }else if(modelSingleHotTrendingNewsX.getTrendingnewssaving() == 1) {
        singleHotTrendingNewsViewHolder.ICONHotTrendingNewsSaving.setImageResource(R.drawable.saved_on);
    }

    singleHotTrendingNewsViewHolder.ICONHotTrendingNewsSaving.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            final int booltrendingnewssaving = modelSingleHotTrendingNewsX.getTrendingnewssaving();

            final String idcontent = modelSingleHotTrendingNewsX.getIdcontent();

            if (booltrendingnewssaving == 1){
                Toast.makeText(v.getContext(), "Turn Off Saved News " + idcontent, Toast.LENGTH_SHORT).show();
                singleHotTrendingNewsViewHolder.ICONHotTrendingNewsSaving.setImageResource(R.drawable.saved_off);
                modelSingleHotTrendingNewsX.setTrendingnewssaving(0);
                /*updateSavedNewsSaving("update_newssaving", id, false);*/
            } else if(booltrendingnewssaving == 0) {
                Toast.makeText(v.getContext(), "Turn On Saved News " + idcontent, Toast.LENGTH_SHORT).show();
                singleHotTrendingNewsViewHolder.ICONHotTrendingNewsSaving.setImageResource(R.drawable.saved_on);
                modelSingleHotTrendingNewsX.setTrendingnewssaving(1);
                /*updateSavedNewsSaving("update_newssaving", id, true);*/
            }
        }
    });



   /* Glide.with(mContext)
            .load(feedItem.getImageURL())
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .centerCrop()
            .error(R.drawable.bg)
            .into(feedListRowHolder.thumbView);*/

    /*singleHotTrendingNewsViewHolder.ROWHotTrendingNewsContainer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(v.getContext(), modelSingleHotTrendingNews.get(position).getGamedescription(), Toast.LENGTH_SHORT).show();

            //Passing Data to GameDescriptionActivity
            Intent intent = new Intent(mContext, GameDetailActivity.class);
            intent.putExtra("ImagePKG",  modelSingleHotTrendingNews.get(position).getGameimage());
            intent.putExtra("NamePKG",  modelSingleHotTrendingNews.get(position).getGamedate());
            intent.putExtra("UrlPKG",  modelSingleHotTrendingNews.get(position).getIdcontent());
            intent.putExtra("DescriptionPKG",  modelSingleHotTrendingNews.get(position).getGametitle());
            mContext.startActivity(intent);
        }
    });*/
}

@Override
public int getItemCount() {
    int itemCount = modelSingleHotTrendingNews.size();
    return itemCount;
}

public void setmodelSingleHotTrendingNews(ArrayList<ModelSingleHotTrendingNews> modelSingleHotTrendingNews) {
    this.modelSingleHotTrendingNews = modelSingleHotTrendingNews;
    notifyDataSetChanged();
}

public class SingleHotTrendingNewsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    TextView TVGameDate;
    TextView TVGameTitle;
    ImageView IMGGameImage;

    TextView TVSeenCounter;
    TextView TVCommentCounter;
    TextView TVLikeCounter;

    ImageView IMGSeenView;
    ImageView IMGCommentView;
    ImageView IMGLikeView;

    ImageView ICONHotTrendingNewsSaving;
    private RelativeLayout ROWHotTrendingNewsContainer;

    private RecyclerViewClickListener mListener;

    //
    private ItemSingleHottrendingnewsBinding itemSingleHottrendingnewsBinding;

    ModelSingleHotTrendingNews modelSingleHotTrendingNewsZ;


    public SingleHotTrendingNewsViewHolder(ItemSingleHottrendingnewsBinding itemSingleHottrendingnewsBinding, RecyclerViewClickListener listener) {
        super(itemSingleHottrendingnewsBinding.getRoot());

        this.itemSingleHottrendingnewsBinding = itemSingleHottrendingnewsBinding;

        TVGameDate = itemView.findViewById(R.id.TV_GameDate);
        TVGameTitle = itemView.findViewById(R.id.TV_GameTitle);
        IMGGameImage = itemView.findViewById(R.id.IMG_GameImage);

        TVSeenCounter = itemView.findViewById(R.id.TV_SeenCounter);
        TVCommentCounter = itemView.findViewById(R.id.TV_CommentCounter);
        TVLikeCounter = itemView.findViewById(R.id.TV_LikeCounter);

        IMGSeenView = itemView.findViewById(R.id.IMG_SeenView);
        IMGCommentView = itemView.findViewById(R.id.IMG_CommentView);
        IMGLikeView = itemView.findViewById(R.id.IMG_LikeView);

        ICONHotTrendingNewsSaving = itemView.findViewById(R.id.ICON_HotTrendingNewsSaving);
        ROWHotTrendingNewsContainer = itemView.findViewById(R.id.ROW_HotTrendingNewsContainer);

        mListener = listener;
        ROWHotTrendingNewsContainer.setOnClickListener(this);
        IMGCommentView.setOnClickListener(this);
        IMGLikeView.setOnClickListener(this);

        //
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ROW_HotTrendingNewsContainer:
                mListener.onRowHotTrendingNewsContainerClick(ROWHotTrendingNewsContainer, getAdapterPosition());
                break;
            case R.id.IMG_CommentView:
                mListener.onRowCommentViewClick(IMGCommentView, getAdapterPosition());
                break;
            case R.id.IMG_LikeView:
                mListener.onRowLikeViewClick(IMGLikeView, getAdapterPosition());
                break;
            default:
                break;
        }
    }
}

public interface RecyclerViewClickListener {
    void onRowHotTrendingNewsContainerClick(View view, int position);
    void onRowCommentViewClick(View view, int position);
    void onRowLikeViewClick(View view, int position);
}

} 我不确定为什么函数start中的setValue不会将值设置为10,而是设置为0 当我尝试PostValue时,数字已按预期更新为11

1 个答案:

答案 0 :(得分:0)

我已经找到答案了,只需在OnBindViewHolder中修改

来自

ModelSingleHotTrendingNews modelSingleHotTrendingNewsX = modelSingleHotTrendingNews.get(position);
    singleHotTrendingNewsViewHolder.TVLikeCounter.setText(String.valueOf(modelSingleHotTrendingNewsX.likecounter.getValue()));

收件人

ModelSingleHotTrendingNews modelSingleHotTrendingNewsZ;
    modelSingleHotTrendingNewsZ = new ViewModelProvider((FragmentActivity) mContext).get(ModelSingleHotTrendingNews.class);
    singleHotTrendingNewsViewHolder.TVLikeCounter.setText(String.valueOf(modelSingleHotTrendingNewsZ.likecounter.getValue()));

谢谢