【问题标题】:Why Custom ArrayAdapter not showing TextView in a ListView为什么自定义 ArrayAdapter 不在 ListView 中显示 TextView
【发布时间】:2019-03-16 03:49:11
【问题描述】:

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

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>

【问题讨论】:

    标签: android android-layout android-listview


    【解决方案1】:

    这很可能是每个项目的线性布局的问题。方向是horizontal,它使每个视图一个接一个地水平放置。问题是动物类型的文本视图的宽度为fill_parent,没有为breed 视图留下空间。如果您将其更改为 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 方法并没有真正做任何事情。你可以删除它。

    【讨论】:

      【解决方案2】:

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

      <?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>
      

      【讨论】:

      • 您使用了 fill_parent 这就是它全屏显示的原因。
      猜你喜欢
      • 2015-08-06
      • 2015-03-13
      • 1970-01-01
      • 1970-01-01
      • 2016-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-03
      相关资源
      最近更新 更多