我偶然发现了一个我无法完全理解的问题,所以我希望这里可能有人遇到过同样的问题或者知道解决问题的好方法。
我创建了一个包含 ListView 的 View 。这个ListView包含两个TextView。 问题是我不知道我将要使用 ArrayAdapter 进入第二个 TextView 的值发送到哪里。有没有办法将更多信息发送到 ArrayAdapter,以便我可以提供“todaysmenu”TextView?
ArrayAdapter 方法:
private void createList() {
ListView lv = (ListView) findViewById(R.id.mylist);
String[] values = new String[] { "Android", "Linux", "OSX",
"WebOS", "Windows7", "Ubuntu", "OS/2"
};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.rowlayout, R.id.restaurantname, values);
lv.setAdapter(adapter);
}
行标记:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/restaurantname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/restaurantname"
android:textSize="23dp" >
</TextView>
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@+id/todaysmenu" />
</LinearLayout>
Activity 布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/mylist"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
一开始我让一切正常,但是当我添加第二个文本字段时出现问题。预先感谢您的帮助!
最佳答案
要实现这一点,您必须构建一个自定义适配器并扩充您的自定义行布局。使用ArrayAdapter不会工作,因为
By default this class expects that the provided resource id references a single TextView. If you want to use a more complex layout, use the constructors that also takes a field id. That field id should reference a TextView in the larger layout resource.
因此,您的自定义适配器类可能类似于:
public class CustomAdapter extends ArrayAdapter {
private final Activity activity;
private final List list;
public CustomAdapter(Activity activity, ArrayList<Restaurants> list) {
this.activity = activity;
this.list = list;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
ViewHolder view;
if(rowView == null)
{
// Get a new instance of the row layout view
LayoutInflater inflater = activity.getLayoutInflater();
rowView = inflater.inflate(R.layout.rowlayout, null);
// Hold the view objects in an object, that way the don't need to be "re- finded"
view = new ViewHolder();
view.retaurant_name= (TextView) rowView.findViewById(R.id.restaurantname);
view.restaurant_address= (TextView) rowView.findViewById(R.id.textView1);
rowView.setTag(view);
} else {
view = (ViewHolder) rowView.getTag();
}
/** Set data to your Views. */
Restaurants item = list.get(position);
view.retaurant_name.setText(item.getTickerSymbol());
view.restaurant_address.setText(item.getQuote().toString());
return rowView;
}
protected static class ViewHolder{
protected TextView retaurant_name;
protected TextView restaurant_address;
}
}
您的 Restaurant.java 类可以像我下面描述的那样简单:
public class Restaurants {
private String name;
private String address;
public Restaurants(String name, String address) {
this.name = name;
this.address = address;
}
public void setName(String name) {
this.name= name;
}
public String getName() {
return name;
}
public void setAddress(String address) {
this.address= address;
}
public String getAddress() {
return address;
}
}
现在,在您的主要 Activity 中,只需将您的列表与一些数据绑定(bind),例如;
/** Declare and initialize list of Restaurants. */
ArrayList<Restaurants> list = new ArrayList<Restaurants>();
/** Add some restaurants to the list. */
list.add(new Restaurant("name1", "address1"));
list.add(new Restaurant("name2", "address2"));
list.add(new Restaurant("name3", "address3"));
list.add(new Restaurant("name4", "address4"));
list.add(new Restaurant("name5", "address5"));
list.add(new Restaurant("name6", "address6"));
此时您可以将自定义适配器设置到您的列表中
ListView lv = (ListView) findViewById(R.id.mylist);
CustomAdapter adapter = new CustomAdapter(YourMainActivityName.this, list);
lv.setAdapter(adapter);
这就是全部,它应该可以很好地工作,但我强烈建议您在谷歌上搜索一些更好的替代方案来实现其他方案 Adapters .
关于android - 在 ListView 中的行中使用具有更多 View 的数组适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11678909/