gpt4 book ai didi

android - 在滚动 recyclerview 更改消失

转载 作者:行者123 更新时间:2023-11-29 01:05:17 27 4
gpt4 key购买 nike

我创建了一个词汇表 Android 应用程序,我们可以通过单击单词附近的星形图标将单词添加到收藏夹。单击星形图标时,它会变为实心星形图标,但如果我们滚动 RecyclerView,实心星形图标将再次变为普通星形图标。

图像滚动之前:

Image before scrolling

图像滚动后:

Image after scrolling

代码如下:

public class WordListAdapter extends RecyclerView.Adapter<WordListViewHolder> {

TextToSpeech ttsObject;

private List<WordListModel> mWordList;
private Context context;
private Activity activity;
private int result;

public WordListAdapter(TextToSpeech ttsObject, int result) {
this.ttsObject = ttsObject;
this.result = result;
}

public WordListAdapter(List<WordListModel> mWordList, Context context, TextToSpeech ttsObject, int result) {
this.mWordList = mWordList;
this.context = context;
activity = (Activity)context;
this.result = result;
this.ttsObject = ttsObject;
}

@Override
public WordListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.word_list_layout,parent,false);

return new WordListViewHolder(view);
}

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

final WordListModel wordmeaning = mWordList.get(position);
holder.mNameTv.setText(wordmeaning.getmWord());
holder.mNameTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

if (result == TextToSpeech.LANG_NOT_SUPPORTED || result == TextToSpeech.LANG_MISSING_DATA){
Toast.makeText(activity,"Feature Not Support in your Device",Toast.LENGTH_LONG).show();
}else {
ttsObject.speak(wordmeaning.getmWord(), TextToSpeech.QUEUE_FLUSH, null);
}
}
});
holder.mMeaningTv.setText(wordmeaning.getmMeaning());
holder.mMeaningTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (result == TextToSpeech.LANG_NOT_SUPPORTED || result == TextToSpeech.LANG_MISSING_DATA){
Toast.makeText(activity,"Feature Not Support in your Device",Toast.LENGTH_LONG).show();
}else {
ttsObject.speak(wordmeaning.getmMeaning(), TextToSpeech.QUEUE_FLUSH, null);
}
}
});

holder.mFavIv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

DatabaseHelper mydb = new DatabaseHelper(context);

try {
if (wordmeaning.getIsFav().equals("false")){
boolean check = mydb.updateData("true",wordmeaning.getmWord(),wordmeaning.getDbTable());
if (check){
holder.mFavIv.setImageResource(R.drawable.ic_star_black_24dp);
}
Log.d("TAG",Boolean.toString(check));
}else {
boolean check = mydb.updateData("false",wordmeaning.getmWord(),wordmeaning.getDbTable());
if (check){
holder.mFavIv.setImageResource(R.drawable.ic_star_border_black_24dp);
}
Log.d("TAG",Boolean.toString(check));
}
}catch (Exception e){
Log.d("TAG",e.toString());
}

}
});

if (wordmeaning.getIsFav().equals("false")){
holder.mFavIv.setImageResource(R.drawable.ic_star_border_black_24dp);
}else {
holder.mFavIv.setImageResource(R.drawable.ic_star_black_24dp);
}

}

@Override
public int getItemCount() {
return mWordList.size();
}

最佳答案

这是因为当您滚动时, View 会被回收。在这里你只是更新了 db 中的值而不是列表中的值,所以你还想更新 arrayList 中的值,因为你想跟踪项目所以改变你的 mFavIv Onclick 方法的代码,如下所示

 holder.mFavIv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

DatabaseHelper mydb = new DatabaseHelper(context);

try {
if (wordmeaning.getIsFav().equals("false")){
boolean check = mydb.updateData("true",wordmeaning.getmWord(),wordmeaning.getDbTable());
if (check){
holder.mFavIv.setImageResource(R.drawable.ic_star_black_24dp);
wordmeaning.setIsFav("true"); // updted your arryListvalue
}
Log.d("TAG",Boolean.toString(check));
}else {
boolean check = mydb.updateData("false",wordmeaning.getmWord(),wordmeaning.getDbTable());
if (check){
holder.mFavIv.setImageResource(R.drawable.ic_star_border_black_24dp);
wordmeaning.setIsFav("false"); // updted your arryListvalue
}
Log.d("TAG",Boolean.toString(check));
}
}catch (Exception e){
Log.d("TAG",e.toString());
}

}
});

关于android - 在滚动 recyclerview 更改消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47759667/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com