MainActivity和MVVM中的公共列表
我需要一些MVVM架构方面的帮助。 我有一个接收LiveData并完美显示它的RecyclerView,然而,我的RecyclerView需要另一个数据源来定制文本视图的颜色和背景。目前,我使用的是Mainactivity中声明的公共列表,但我已经了解到这不是一个好的做法。 是否可以从RecyclerView内部对数据库执行非实时请求,以替换公共列表?如果不是的话,我真的想要一些建议 这是我的onBindViewHolder:MainActivity和MVVM中的公共列表,mvvm,android-recyclerview,android-room,android-mvvm,Mvvm,Android Recyclerview,Android Room,Android Mvvm,我需要一些MVVM架构方面的帮助。 我有一个接收LiveData并完美显示它的RecyclerView,然而,我的RecyclerView需要另一个数据源来定制文本视图的颜色和背景。目前,我使用的是Mainactivity中声明的公共列表,但我已经了解到这不是一个好的做法。 是否可以从RecyclerView内部对数据库执行非实时请求,以替换公共列表?如果不是的话,我真的想要一些建议 这是我的onBindViewHolder: @Override public void onBindViewHo
@Override
public void onBindViewHolder(@NonNull ResultRecyclerViewAdapter.ResultHolder holder, int position) {
Results currentResult = results.get(position);
holder.ston1.setText(currentResult.getSton1());
holder.ston2.setText(String.valueOf(currentResult.getSton2()));
holder.ston1.setBackgroundColor(0xFF12FF45);
holder.ston2.setBackgroundColor(0xFF12FF45);
holder.ston1.getBackground().setAlpha(100);
holder.ston2.getBackground().setAlpha(100);
for (Ston ston: MainActivity.Stons){
if (currentResult.getStonCode().equals(ston.getStonCode()) && currentResult.getStonType().equals(ston.getStonType())){
switch (ston.getStonSelected()) {
case "WADS":
holder.ston1.getBackground().setAlpha(255);
break;
case "WQAS":
holder.ston2.getBackground().setAlpha(255);
break;
}
break;
}
}
holder.ston1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Boolean found = false;
for (Ston ston: MainActivity.Stons){
if (currentResult.getStonCode().equals(ston.getStonCode())){
found = true;
break;
}
}
if (!found) {
holder.ston1.getBackground().setAlpha(255);
MainActivity.Stons.add(new Stons(currentResult.getStonCode(),"WADS",
currentResult.getStonType()));
}
else {
for (Ston ston : MainActivity.Stons) {
if (currentResult.getStonCode().equals(ston.getStonCode()) && ston.getStonSelected().equals("WADS") &&
ston.getStonType().equals(currentResult.getStonType())){
MainActivity.Stons.remove(ston);
holder.ston1.getBackground().setAlpha(100);
break;
}
}
}
}
});
holder.ston2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Boolean found = false;
for (Ston ston: MainActivity.Stons){
if (currentResult.getStonCode().equals(ston.getStonCode())){
found = true;
break;
}
}
if (!found) {
holder.ston2.getBackground().setAlpha(255);
MainActivity.Stons.add(new Stons(currentResult.getStonCode(),"WQAS",
currentResult.getStonType()));
}
else {
for (Ston ston : MainActivity.Stons) {
if (currentResult.getStonCode().equals(ston.getStonCode()) && ston.getStonSelected().equals("WQAS") &&
ston.getStonType().equals(currentResult.getStonType())){
MainActivity.Stons.remove(ston);
holder.ston2.getBackground().setAlpha(100);
break;
}
}
}
}
});
我看到的一个选项是专门为您的recyclerview适配器创建新类型,该适配器将保存Results对象和用于后台alpha的信息。所以,在您的活动(或片段)中,当触发livedata observer时,您不会直接将其传递给适配器,而是首先创建新类型的对象集合,然后将其传递给适配器。如果可能的话,我强烈建议您使用Kotlin,在那里您可以使用集合映射将集合从db映射到新类型的集合。请在此提供帮助:),我是否要添加更多详细信息?谢谢,现在切换到Kotlin已经太晚了。我设法从适配器内部查询数据库,但仍然不满意it@dxtr怎么会这么晚呢?:)你可以很容易地把它们一起使用(java和kotlin)。哦,我应该读一下,我从没想过它们可以一起使用。谢谢