我绝对是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?
请使用onCreate方法的public void onCreate(Bundle savedInstanceState)
版本而不是public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState)
后者仅在Lollipop及以上版本中可用。
并且还创建MyCustomAdapter
的实例而不是Generic 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