为什么自定义 ArrayAdapter 不在 ListView 中显示 TextView

Why Custom ArrayAdapter not showing TextView in a ListView

我制作了一个自定义 ArrayAdapter 并尝试添加动物的另一个 TextView "Breed",但是当我执行该程序时,它没有显示品种。我哪里做错了?我挠头很久了。请帮忙看看错在哪里?

MainActivity.Java

public class MainActivity extends AppCompatActivity {

    ListView simpleList;
    ArrayList<Item> animalList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        simpleList = (ListView) findViewById(R.id.simpleListView);


        animalList.add(new Item("Lion",R.drawable.lion,"Khatarnak"));
        animalList.add(new Item("Tiger",R.drawable.tiger,"Fudu"));
        animalList.add(new Item("Monkey",R.drawable.monkey,"Lallu"));
        animalList.add(new Item("Elephant",R.drawable.elephant,"Jabardast"));
        animalList.add(new Item("Dog",R.drawable.dog,"ItemDog"));
        animalList.add(new Item("Cat",R.drawable.cat,"MeeMee"));

        MyAdapter myAdapter=new MyAdapter(this,R.layout.list_view_items,animalList);
        simpleList.setAdapter(myAdapter);
    }
}

MyAdapter.Java

public class MyAdapter extends ArrayAdapter<Item> {

    ArrayList<Item> animalList = new ArrayList<>();

    public MyAdapter(Context context, int textViewResourceId, ArrayList<Item> objects) {
        super(context, textViewResourceId, objects);
        animalList = objects;
    }

    @Override
    public int getCount() {
        return super.getCount();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View v = convertView;
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.list_view_items, null);

        TextView textView = (TextView) v.findViewById(R.id.textView);
        ImageView imageView = (ImageView) v.findViewById(R.id.imageView);
        TextView breedView = (TextView)v.findViewById(R.id.breed);

        textView.setText(animalList.get(position).getAnimalName());
        imageView.setImageResource(animalList.get(position).getAnimalImage());
        breedView.setText(animalList.get(position).getBreed());

        return v;

    }

}

Item.Java

public class Item {

    String animalName;
    int animalImage;
    String breedName;


    public String getBreed() {
        return breedName;
    }

    public void setBreed(String breed) {
        this.breedName = breedName;
    }

    public Item(String animalName,int animalImage,String breedName)
    {
        this.animalImage=animalImage;
        this.animalName=animalName;
        this.breedName = breedName;
    }
    public String getAnimalName()
    {
        return animalName;
    }
    public int getAnimalImage()
    {
        return animalImage;
    }
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/simpleListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="#000"
        android:dividerHeight="2dp"/>

</RelativeLayout>

list_view_items.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:padding="5dp" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Demo"
        android:textColor="#000" />

    <TextView
        android:id="@+id/breed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Breed"
        android:textColor="#000" />


</LinearLayout>

这很可能是每个项目的线性布局问题。方向是 horizontal,每个视图水平放置一个接一个。问题是动物类型的文本视图的宽度为 fill_parentbreed 视图没有 space。如果将其更改为 wrap_content,它可能适用于某些情况,但可能不适用于所有情况。

无论如何我认为这是视图位置和大小的问题。尝试移动它们。也许甚至一个简单的改变就是将它们垂直放置:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal">

   <ImageView
    android:id="@+id/imageView"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:padding="5dp" />

  <LinearLayout
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:orientation="vertical">

      <TextView
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Demo"
        android:textColor="#000" />

     <TextView
        android:id="@+id/breed"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Breed"
        android:textColor="#000" />
   </LinearLayout>
</LinearLayout>

也许有更有效的方法,但这只是一个例子。内部线性布局将一个文本视图置于另一个文本视图之上。

PS:您的 getCount 方法实际上没有做任何事情。你可以删除它。

用我的代码替换您的布局,您的问题将得到解决。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:padding="5dp" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:text="Demo"
        android:textColor="#000" />

    <TextView
        android:id="@+id/breed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Breed"
        android:textColor="#000" />


</LinearLayout>