【问题标题】:Why ListView with custom adapter showing nothing?为什么带有自定义适配器的 ListView 什么也没显示?
【发布时间】:2016-01-19 06:24:55
【问题描述】:

我是 Android Studio 的绝对初学者。我现在正在学习列表视图。当我使用带有简单数组适配器的列表视图时。它显示得很好。现在我正在尝试使用自定义行布局创建一个自定义适配器。

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

这是我的活动课

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 的行模型类

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>

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

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 绑定到 listview?

【问题讨论】:

  • 您正在创建 4 个不同的对象但仅为 item_1 设置值的问题,该值每次都会被替换并显示最后一个项目数据。
  • 应该如何修改?我想我正在为每一行设定价值。例如像 name_tf.setText(values[position].Name) 。我怎样才能让它正确?
  • 检查我的更新答案

标签: android listview android-listview


【解决方案1】:

请使用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

【讨论】:

  • 使用 MyCustomAdapter 创建实例后是否正常工作?你能添加logcat吗?
  • 是的。有用。但是有问题。我将发布屏幕截图。只有 item_4 数据绑定到 listview。
  • 非常感谢。有效。抱歉,第二期的复制粘贴工作非常紧迫。非常感谢。它奏效了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多