gpt4 book ai didi

android - Livedata正在执行一项 Activity ,但未进行另一项 Activity

转载 作者:行者123 更新时间:2023-12-03 10:56:08 25 4
gpt4 key购买 nike

我正在从SQLite数据库加载动物信息列表。我使用了MVVM体系结构。我在MainActivity中有两个按钮,并开始了两个不同的 Activity 。一个是AnimalAugmented,另一个是AnimalIntroducer。在这两个 Activity 中,我都加载相同的数据。为了在AnimalAugmented中加载数据,我使用了以下代码

        RecyclerView recyclerView = findViewById(R.id.recycler_view_animals);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);

AnimalAdapter animalAdapter = new AnimalAdapter();
recyclerView.setAdapter(animalAdapter);

animalViewModel = ViewModelProviders.of(this).get(AnimalViewModel.class);
animalViewModel.getAllAnimals().observe(this, animals -> {
animalAdapter.setAnimals(animals);
});
它工作正常。但是,当我尝试在 AnimalIntroducer中加载数据时,它没有显示任何项目。对于 AnimalIntroducer,我使用了以前的代码
    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animal_introducer);

RecyclerView recyclerView = findViewById(R.id.recycler_view_animal_introducer);
recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
recyclerView.setHasFixedSize(true);

AnimalAdapter animalAdapter = new AnimalAdapter();
recyclerView.setAdapter(animalAdapter);

animalViewModel = ViewModelProviders.of(this).get(AnimalViewModel.class);
animalViewModel.getAllAnimals().observe(this, animals -> {

animalAdapter.setAnimals(animals);
Log.e(TAG, "onCreate: grid "+animals.size());
for(Animal animal:animals){
Log.e(TAG,animal.getBanglaName());
}
});
}
在这里,它按预期在日志中打印动物列表的大小,但不显示任何项目。但是,如果我从ArrayList而不是ViewModel加载数据,它将起作用。那么,为什么我的 Activity 没有显示SQLite中的任何项目?
        animalAdapter.setAnimals(AnimalDump.insertToList());

这是我的 AnimalAdapter
public class AnimalAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private static final String TAG = "Augmented";
private static final int TYPE_FREE = 1;
private static final int TYPE_LOCKED = 2;
List<Animal> animals= new ArrayList<>();

public OnItemClickListener listener;

public AnimalAdapter() {

Log.d(TAG, "AnimalAdapter: ");
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
if(viewType==TYPE_FREE){
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_animal,parent,false);
return new AnimalViewHolder(view);
}
else {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_animal_locked,parent,false);
return new LockedAnimalViewHolder(view);
}
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {


Animal animal=animals.get(position);
if(animal.isOffline()){
((AnimalViewHolder)holder).setValues(animal);
}
else {
((LockedAnimalViewHolder)holder).setValues(animal);
}
}

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

public void setAnimals(List<Animal> animals) {
this.animals = animals;
Log.d(TAG, "setAnimals: ");
}

@Override
public int getItemViewType(int position) {
if(animals.get(position).isOffline())
return TYPE_FREE;
else
return TYPE_LOCKED;
}

public void setListener(OnItemClickListener listener) {
this.listener = listener;
}

protected class AnimalViewHolder extends RecyclerView.ViewHolder {

private ImageView itemAnimalImageView;
private TextView itemAnimalTextView;
private Context context;
public CardView itemCardView;

public AnimalViewHolder(@NonNull View itemView) {
super(itemView);
itemAnimalImageView=itemView.findViewById(R.id.item_animal_image_view);
itemAnimalTextView=itemView.findViewById(R.id.item_animal_text_view);
itemCardView = itemView.findViewById(R.id.item_card_view);
context = itemView.getContext();

itemView.setOnClickListener(view -> {

int position = getAdapterPosition();
if(listener!=null && position!=RecyclerView.NO_POSITION){
listener.onItemClick(animals.get(position));
}

YoYo.with(Techniques.RollIn)
.duration(700)
.repeat(0)
.playOn(itemAnimalImageView);

});

}

public void setValues(Animal animal){
itemAnimalTextView.setText(animal.getBanglaName());
itemAnimalImageView.setImageResource(context.getResources().getIdentifier(animal.getId(),"drawable",context.getPackageName()));
}
}

protected class LockedAnimalViewHolder extends RecyclerView.ViewHolder {

private ImageView itemAnimalImageView,itemLockIcon;
private TextView itemAnimalTextView;
private Context context;
public CardView itemCardView;

public LockedAnimalViewHolder(@NonNull View itemView) {
super(itemView);
itemAnimalImageView=itemView.findViewById(R.id.item_animal_image_view);
itemAnimalTextView=itemView.findViewById(R.id.item_animal_text_view);
itemCardView = itemView.findViewById(R.id.item_card_view);
itemLockIcon = itemView.findViewById(R.id.icon_lock);
context = itemView.getContext();

itemView.setOnClickListener(view -> {

YoYo.with(Techniques.RubberBand)
.duration(700)
.repeat(0)
.playOn(itemAnimalImageView);

});

}

public void setValues(Animal animal){
itemAnimalTextView.setText(animal.getBanglaName());
itemAnimalImageView.setImageResource(context.getResources().getIdentifier(animal.getId(),"drawable",context.getPackageName()));
}
}

public interface OnItemClickListener{
void onItemClick(Animal animal);
}

最佳答案

notifyDataSetChaged()之后调用setAnimals(..)中的this.animals = animals;列表数据更改后,必须调用notifyDataSetChaged()

关于android - Livedata正在执行一项 Activity ,但未进行另一项 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62714226/

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