英文:
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<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.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<List<Word>>() {
@Override
public void onChanged(List<Word> words) {
adapter.setWords(words);
}
});
}
This is the Word object:
@Entity(tableName = "word_table")
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<List<Word>> allWords;
private LiveData<List<Word>> practiceWords;
private LiveData<List<Word>> 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<List<Word>> getAllWords() {
return allWords;
}
public LiveData<List<Word>> getPracticeWords() {
return practiceWords;
}
public LiveData<List<Word>> 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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论