【问题标题】:How to display the description of the selected animals with names?如何用名称显示所选动物的描述?
【发布时间】:2021-07-02 14:58:21
【问题描述】:

它只显示选定的动物名称,这是代码

public class MainActivity extends AppCompatActivity implements AnimalAdapter.MyClickInterface {

RecyclerView recyclerView;
ArrayList<AnimalEntry> animals;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    recyclerView = findViewById(R.id.recycler_view_adapter);

    animals = new ArrayList<>();

    animals.add(new AnimalEntry("Lion",R.drawable.lion));
    animals.add(new AnimalEntry("Cat", R.drawable.cat));
    animals.add(new AnimalEntry("Horse", R.drawable.hourse));
    animals.add(new AnimalEntry("Wolf", R.drawable.wolf));
    animals.add(new AnimalEntry("Fox", R.drawable.fox));
    animals.add(new AnimalEntry("Elephant", R.drawable.elephant));
    animals.add(new AnimalEntry("Goat", R.drawable.goat));
    animals.add(new AnimalEntry("Lemur", R.drawable.lemur));
    animals.add(new AnimalEntry("Giraffe", R.drawable.giraffe));
    animals.add(new AnimalEntry("Zebra", R.drawable.zebra));

    AnimalAdapter animalAdapter = new AnimalAdapter(animals, this,this);

    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    recyclerView.setAdapter(animalAdapter);
}

@Override
public void onItemClick(int positionOfAnimal) {
    Toast.makeText(this, "Clicked on "+animals.get(positionOfAnimal).getName(), Toast.LENGTH_SHORT).show();
    Intent intent = new Intent(this,AnimalInfo.class);
    //Shows the intently the image of the animals in the next class..
    intent.putExtra("image",animals.get(positionOfAnimal).getImage());
    //This will intently the heading name of the animals in the next class..
    intent.putExtra("hName",animals.get(positionOfAnimal).getName());
    startActivity(intent);
}
}


public class AnimalEntry {

String name;
int image;

public AnimalEntry(String name, int image) {
    this.name = name;
    this.image = image;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getImage() {
    return image;
}

public void setImage(int image) {
    this.image = image;
}
}


public class AnimalAdapter extends RecyclerView.Adapter<AnimalAdapter.AnimalRowHolder> {

ArrayList<AnimalEntry> animals;
Context context;
MyClickInterface myClickInterface;

public AnimalAdapter(ArrayList<AnimalEntry> animals, Context context, MyClickInterface myClickInterface) {
    this.animals = animals;
    this.context = context;
    this.myClickInterface = myClickInterface;
}

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

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

@Override
public void onBindViewHolder(@NonNull AnimalRowHolder holder, int position) {
    holder.textViewAnimals.setText(animals.get(position).getName());
    holder.imageViewAnimals.setImageResource(animals.get(position).getImage());
}

class AnimalRowHolder extends RecyclerView.ViewHolder {
    ImageView imageViewAnimals;
    TextView textViewAnimals;

    public AnimalRowHolder(@NonNull View itemView) {
        super(itemView);

        imageViewAnimals = itemView.findViewById(R.id.image_view_animals);
        textViewAnimals = itemView.findViewById(R.id.txt_animal);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myClickInterface.onItemClick(getAdapterPosition());
            }
        });
    }

    public void bind(AnimalEntry entry, int position) {
        imageViewAnimals.setImageResource(entry.image);
        textViewAnimals.setText(entry.name);
    }
}

interface MyClickInterface {
    void onItemClick(int positionOfAnimal);
}
}

我想针对所选动物显示描述。

【问题讨论】:

  • 我想你不明白,recyclerView 是如何工作的。我会尝试回答,但我建议你阅读 android 指南,它很有帮助。

标签: java android-studio android-recyclerview


【解决方案1】:

在您的 MainActivity.java 中

RecyclerView recyclerView;
ArrayList<AnimalEntry> animals;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    recyclerView = findViewById(R.id.recycler_view_adapter);

    animals = new ArrayList<>();

    animals.add(new AnimalEntry("Lion",R.drawable.lion));
    animals.add(new AnimalEntry("Cat", R.drawable.cat));
    animals.add(new AnimalEntry("Horse", R.drawable.hourse));
    animals.add(new AnimalEntry("Wolf", R.drawable.wolf));
    animals.add(new AnimalEntry("Fox", R.drawable.fox));
    animals.add(new AnimalEntry("Elephant", R.drawable.elephant));
    animals.add(new AnimalEntry("Goat", R.drawable.goat));
    animals.add(new AnimalEntry("Lemur", R.drawable.lemur));
    animals.add(new AnimalEntry("Giraffe", R.drawable.giraffe));
    animals.add(new AnimalEntry("Zebra", R.drawable.zebra));

    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    recyclerView.setAdapter(new AnimalAdapter(animals));
}

在你的 AnimalAdapter.class 中

public class AnimalAdapter extends RecyclerView.Adapter<AnimalAdapter.AnimalRowHolder> {

ArrayList<AnimalEntry> animals;

public AnimalAdapter(ArrayList<AnimalEntry> animals) {
    this.animals = animals;
}

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

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

@Override
public void onBindViewHolder(@NonNull AnimalRowHolder holder, int position) {
    
    //Creates the row with the information of the list
    holder.textViewAnimals.setText(animals.get(position).getName());
    holder.imageViewAnimals.setImageResource(animals.get(position).getImage());

    //Creates a new Object with the information of the row
    AnimalEntry animalEntry = new AnimalEntry();
    animalEntry.setName(animals.get(position).getName());
    animalEntry.setImageResource(animals.get(position).getImage());

    //If the user click on the image it will open another activity
    holder.imageViewAnimals.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                intent = new Intent(v.getContext(), AnimalInfo.class);
                intent.putExtra("animalEntry", animalEntry());
                v.getContext().startActivity(intent);
               
            }
        });
}

class AnimalRowHolder extends RecyclerView.ViewHolder {
    ImageView imageViewAnimals;
    TextView textViewAnimals;

    public AnimalRowHolder(@NonNull View itemView) {
        super(itemView);

        imageViewAnimals = itemView.findViewById(R.id.image_view_animals);
        textViewAnimals = itemView.findViewById(R.id.txt_animal);
    }
}

并且你的动物类应该是可序列化的,以便将对象转换为字节序列。

public class AnimalEntry implements Serializable{
String name;
int image;

public AnimalEntry(String name, int image) {
    this.name = name;
    this.image = image;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getImage() {
    return image;
}

public void setImage(int image) {
    this.image = image;
}
}

在您将接收 Intent 的活动中,您可以创建一个方法,如下所示:

private AnimalEntry animalEntry;

 private void getIntentFromRows() {
      
        if ((getIntent() != null) && (getIntent().hasExtra("animalEntry"))) {
            animalEntry = (AnimalEntry) getIntent().getSerializableExtra("animalEntry");

        }

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-05
    • 1970-01-01
    • 2016-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多