如何将 RecyclerView 中项目视图(itemView)中的 switchButton 与相应的对象绑定?

huangapple go评论74阅读模式
英文:

How to bind switchButton in itemView to respective object in RecyclerView?

问题

以下是您提供的代码的翻译部分:

public class WordAdapter extends RecyclerView.Adapter<WordAdapter.WordHolder> {

    private List<Word> words = new ArrayList<>();
    private OnItemClickListener listener;

    @NonNull
    @Override
    public WordHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.word_item, parent, false);
        return new WordHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull WordHolder holder, int position) {
        Word currentWord = words.get(position);
        holder.textViewSwedish.setText(currentWord.getSwedish());
        holder.textViewRussian.setText(currentWord.getRussian());     
        holder.switchStatus.setOnCheckedChangeListener((buttonView, isChecked) -> {
            int newStatus = isChecked ? 200 : 300;
            currentWord.setStatus(newStatus);
            updateWordInDatabase(currentWord);
        });
        holder.switchStatus.setChecked(currentWord.getStatus() == 200);
    }

    class WordHolder extends RecyclerView.ViewHolder {
        private TextView textViewSwedish;
        private TextView textViewRussian;
        private Switch switchStatus;

        public WordHolder(@NonNull View itemView) {
            super(itemView);
            textViewSwedish = itemView.findViewById(R.id.text_view_swedish);
            textViewRussian = itemView.findViewById(R.id.text_view_russian);
            switchStatus = itemView.findViewById(R.id.btn_switch);
        }
    }

    private void updateWordInDatabase(Word word) {
        // 在这里实现更新数据库的逻辑
    }
}

请注意,为了实现在RecyclerView中切换状态并更新数据库,我在onBindViewHolder方法中添加了一个setOnCheckedChangeListener来监听开关状态的变化,并根据状态更改更新了数据库中的单词。另外,我还根据当前单词的状态设置了开关的初始选中状态。

请确保在updateWordInDatabase方法中实现适当的逻辑来更新数据库中的单词信息。

如果需要在您的Word类中添加getter和setter方法来访问和修改单词的状态,请确保在Word类中进行相应的更改。

英文:

I am beginner, I'm trying to figure out.

I have objects Word (String swedish, String russian, int status). Status defines in which gamelevel the word should appear (for now it can be 200 or 300).

How can I do so that switchButton "switchStatus" in every item of RecyclerView would change word.status between 200 (for true) and 300 (for false)? And update the word in database, of course.

public class WordAdapter extends RecyclerView.Adapter&lt;WordAdapter.WordHolder&gt; {
private List&lt;Word&gt; words = new ArrayList&lt;&gt;();
private OnItemClickListener listener;
@NonNull
@Override
public WordHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.word_item, parent, false);
return new WordHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull WordHolder holder, int position) {
Word currentWord = words.get(position);
holder.textViewSwedish.setText(currentWord.getSwedish());
holder.textViewRussian.setText(currentWord.getRussian());     
holder.switchStatus.set//??????????;
}
class WordHolder extends RecyclerView.ViewHolder {
private TextView textViewSwedish;
private TextView textViewRussian;
private Switch switchStatus;
public WordHolder(@NonNull View itemView) {
super(itemView);
textViewSwedish = itemView.findViewById(R.id.text_view_swedish);
textViewRussian = itemView.findViewById(R.id.text_view_russian);
switchStatus = itemView.findViewById(R.id.btn_switch);
}
}
}

This is WordList.activity:

public class WordList extends AppCompatActivity {
private WordViewModel wordViewModel;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
final WordAdapter adapter = new WordAdapter();
recyclerView.setAdapter(adapter);
wordViewModel = new ViewModelProvider(this,
ViewModelProvider.AndroidViewModelFactory.getInstance(this.getApplication())).get(WordViewModel.class);
wordViewModel.getAllWords().observe(this, new Observer&lt;List&lt;Word&gt;&gt;() {
@Override
public void onChanged(List&lt;Word&gt; words) {
adapter.setWords(words);
}
});
}

This is the Word object:

@Entity(tableName = &quot;word_table&quot;)
public class Word {
@PrimaryKey(autoGenerate = true)
private int id;
private String russian;
private String swedish;
private int status;
public Word(String russian, String swedish, String sound, int status) {
this.russian = russian;
this.swedish = swedish;
this.status = status;
}

WordViewModel.class (if needed):

public class WordViewModel extends AndroidViewModel {
private WordRepository repository;
private LiveData&lt;List&lt;Word&gt;&gt; allWords;
private LiveData&lt;List&lt;Word&gt;&gt; practiceWords;
private LiveData&lt;List&lt;Word&gt;&gt; archivedWords;
public WordViewModel(@NonNull Application application) {
super(application);
repository = new WordRepository(application);
allWords = repository.getAllWords();
practiceWords = repository.getPracticeWords();
archivedWords = repository.getArchivedWords();
}
public void insert(Word word) {
repository.insert(word);
}
public void update(Word word) {
repository.update(word);
}
public void delete(Word word) {
repository.delete(word);
}
public void deleteAllWords() {
repository.deleteAllWords();
}
public LiveData&lt;List&lt;Word&gt;&gt; getAllWords() {
return allWords;
}
public LiveData&lt;List&lt;Word&gt;&gt; getPracticeWords() {
return practiceWords;
}
public LiveData&lt;List&lt;Word&gt;&gt; getArchivedWords() {
return archivedWords;
}

答案1

得分: 1

设置开关的值,您必须设置为true或false。因此在您的情况下,如果值为200,则为true,否则为false。您可以使用以下代码设置开关的值:

holder.switchStatus.setChecked(currentWord.getStatus()==200)
英文:

To set value for switch you have to set true or false, So in your case if value is 200 it's true and otherwise it's false. You can set switch value like below code:

holder.switchStatus.setChecked(currentWord.getStatus()==200)

huangapple
  • 本文由 发表于 2020年10月24日 01:00:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/64504327.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定