我需要创建一个适配器扩展ArrayAdapter并使用ArrayList变量。我有带有AnimalName和pictureId的Animal类。程序应在activity_main的ListView中显示ArrayList。当我启动应用程序时,应用程序立即崩溃。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listViewAnimals"/>
</androidx.constraintlayout.widget.ConstraintLayout>
layout_animals.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/animalImage"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_weight="1"/>
<TextView
android:id="@+id/animalName"
android:layout_width="wrap_content"
android:layout_height="83dp"
android:layout_weight="1"
android:text="TextView" />
</LinearLayout>
Animal.java
package com.example.arrayadapter3;
public class Animal {
private String animalName;
private int pictureId;
public Animal(String name, int pictureId) {
this.animalName = name;
this.pictureId = pictureId;
}
public String getAnimalName() {
return animalName;
}
public int getPictureId() {
return pictureId;
}
}
AnimalArrayAdapter
package com.example.arrayadapter3;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class AnimalArrayAdapter extends ArrayAdapter <Animal> {
private List<Animal> animals;
public AnimalArrayAdapter(Context context, List<Animal> animals){
super(context,R.layout.layout_animals,animals);
this.animals = animals;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View row = convertView;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView == null){
row = inflater.inflate(R.layout.layout_animals, null, true);
}
TextView textView = (TextView) row.findViewById(R.id.animalName);
ImageView imageView = (ImageView) row.findViewById(R.id.animalImage);
textView.setText(animals.get(position).getAnimalName());
imageView.setImageResource(animals.get(position).getPictureId());
return row;
}
}
和MainActivity.java
package com.example.arrayadapter3;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView listView;
private AnimalArrayAdapter animalArrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Animal> animals = createAnimalList();
this.animalArrayAdapter = new AnimalArrayAdapter(this, animals);
this.listView = this.findViewById(R.id.listViewAnimals);
this.listView.setAdapter(animalArrayAdapter);
}
private List<Animal> createAnimalList() {
List<Animal> animalList = new ArrayList<>();
int catID = R.drawable.cat;
int dogID = R.drawable.dog;
int foxID = R.drawable.fox;
int sheepID = R.drawable.sheep;
int snakeID = R.drawable.snake;
Animal cat = new Animal("cat", catID);
Animal dog = new Animal("dog", dogID);
Animal fox = new Animal("fox", foxID);
Animal sheep = new Animal("sheep",sheepID);
Animal snake = new Animal("snake",snakeID );
animalList.add(cat);
animalList.add(dog);
animalList.add(fox);
animalList.add(sheep);
animalList.add(snake);
return animalList;
}
}