为什么带有自定义适配器的 ListView 什么都不显示?

Why ListView with custom adapter showing nothing?

我绝对是 Android Studio 的新手。我现在正在学习列表视图。当我将列表视图与简单的数组适配器一起使用时。它显示正常。现在我正在尝试创建具有自定义行布局的自定义适配器。

但是当我 运行 应用程序时,它没有抛出错误。但是 activity 在屏幕上没有显示任何内容。我刚看到空白屏幕,没有数据绑定到列表视图。我的代码有什么问题?

这是我的activityclass

public class CustomAdapterActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        setContentView(R.layout.custom_adapter);

        ListItem item_1 = new ListItem();
        item_1.Name = "Name 1";
        item_1.Email = "Email 1";
        item_1.Phone = "Phone 1";

        ListItem item_2 = new ListItem();
        item_1.Name = "Name 2";
        item_1.Email = "Email 2";
        item_1.Phone = "Phone 2";

        ListItem item_3 = new ListItem();
        item_1.Name = "Name 3";
        item_1.Email = "Email 3";
        item_1.Phone = "Phone 3";

        ListItem item_4 = new ListItem();
        item_1.Name = "Name 4";
        item_1.Email = "Email 4";
        item_1.Phone = "Phone 4";

        ListItem[] items = { item_1 , item_2,item_3 , item_4 };

        ArrayAdapter adapter = new ArrayAdapter<ListItem>(this,R.layout.custom_row,items);
        ListView listView = (ListView)findViewById(R.id.custom_listview);
        listView.setAdapter(adapter);
    }
}

这是我的名为 ListItem

的行模型 class
public class ListItem {

    public String Name;
    public String Email;
    public String Phone;

}

这是主要布局

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

    <ListView
        android:id="@+id/custom_listview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></ListView>
</LinearLayout>

这是行的布局。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:text="name"
        android:id="@+id/row_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"/>

    <TextView
        android:layout_below="@+id/row_name"
        android:id="@+id/row_email"
        android:text="email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"/>

    <TextView
        android:layout_below="@+id/row_email"
        android:id="@+id/row_phone"
        android:text="phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"/>
</RelativeLayout>

这是我的自定义适配器 class。

public class MyCustomAdapter extends ArrayAdapter<ListItem> {
    private final Context context;
    private final ListItem[] values;

    public MyCustomAdapter(Context context,ListItem[] values)
    {
        super(context,-1,values);
        this.context = context;
        this.values = values;
    }

    @Override
    public View getView(int position,View convertView,ViewGroup parent)
    {
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.custom_row, parent, false);
        TextView name_tf = (TextView)rowView.findViewById(R.id.row_name);
        TextView email_tf = (TextView)rowView.findViewById(R.id.row_email);
        TextView phone_tf = (TextView)rowView.findViewById(R.id.row_phone);
        name_tf.setText(values[position].Name);
        email_tf.setText(values[position].Email);
        phone_tf.setText(values[position].Phone);
        return rowView;
    }
}

当我 运行 应用程序时,没有数据绑定到 ListView。

然后我跟着Sankar V的回答。有效。但它仍然有一个小问题,如图

为什么只有 item_4 绑定到列表视图?

请使用 public void onCreate(Bundle savedInstanceState) 版本的 onCreate 方法,而不是 public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState)

后者仅适用于 Lollipop 及以上版本。

并且还创建 MyCustomAdapter 实例而不是通用 ArrayAdapter

ArrayAdapter adapter = new MyCustomAdapter<ListItem>(this,R.layout.custom_row,items);

正确设置数据。您正在创建 4 个不同的对象,但仅为第一个对象设置值。像下面这样更改

        ListItem item_1 = new ListItem();
        item_1.Name = "Name 1";
        item_1.Email = "Email 1";
        item_1.Phone = "Phone 1";

        ListItem item_2 = new ListItem();
        item_2.Name = "Name 2"; // This need to be changed from item_1 to item_2
        item_2.Email = "Email 2"; // This need to be changed from item_1 to item_2
        item_2.Phone = "Phone 2"; // This need to be changed from item_1 to item_2

        ListItem item_3 = new ListItem();
        item_3.Name = "Name 3"; // This need to be changed from item_1 to item_3
        item_3.Email = "Email 3"; // This need to be changed from item_1 to item_3
        item_3.Phone = "Phone 3"; // This need to be changed from item_1 to item_3

        ListItem item_4 = new ListItem();
        item_4.Name = "Name 4"; // This need to be changed from item_1 to item_4
        item_4.Email = "Email 4"; // This need to be changed from item_1 to item_4
        item_4.Phone = "Phone 4"; // This need to be changed from item_1 to item_4