在列表视图中使用具有更多视图的数组适配器
Posted
技术标签:
【中文标题】在列表视图中使用具有更多视图的数组适配器【英文标题】:Use array adapter with more views in row in listview 【发布时间】:2012-07-25 14:34:04 【问题描述】:我偶然发现了一个我无法完全理解的问题,所以我希望这里的某个人可能遇到过同样的问题或知道解决问题的好方法。
我创建了一个包含 ListView 的视图。这个 ListView 包含两个 TextView。 问题是我不知道我将使用 ArrayAdapter 的第二个文本视图中的值发送到哪里。有没有办法向 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_
android:layout_
android:orientation="vertical" >
<TextView
android:id="@+id/restaurantname"
android:layout_
android:layout_
android:text="@+id/restaurantname"
android:textSize="23dp" >
</TextView>
<TextView
android:id="@+id/textView1"
android:layout_
android:layout_
android:text="@+id/todaysmenu" />
</LinearLayout>
活动布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="vertical" >
<ListView
android:id="@+id/mylist"
android:layout_
android:layout_>
</ListView>
</LinearLayout>
一开始我一切正常,但是当我添加第二个文本字段时,问题就出现了。在此先感谢您的帮助!
【问题讨论】:
【参考方案1】:要实现这一点,您必须构建一个自定义适配器并扩展您的自定义行布局。使用ArrayAdapter 不起作用,因为
默认情况下,此类期望提供的资源 id 引用 单个文本视图。如果您想使用更复杂的布局,请使用也采用字段 id 的构造函数。该字段 id 应引用较大布局资源中的 TextView。
因此,您的自定义适配器类可能类似于:
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;
现在,在您的主要活动中,只需将您的列表与一些数据绑定,例如;
/** 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。
【讨论】:
谢谢。这有帮助!谢谢。【参考方案2】:你可以试试这个https://devtut.wordpress.com/2011/06/09/custom-arrayadapter-for-a-listview-android/。您必须使用您需要的字段创建一个自定义类 Item 并扩展 ArrayAdapter 。
【讨论】:
您建议的链接中的解决方案对我来说效果很好。也许您应该将代码复制并粘贴到您的答案中并引用 URL,以防博客被删除。【参考方案3】:我认为你的问题在这里:
而不是这个:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="vertical" >
<TextView
android:id="@+id/restaurantname"
android:layout_
android:layout_
android:text="@+id/restaurantname"
android:textSize="23dp" >
</TextView>
<TextView
android:id="@+id/textView1"
android:layout_
android:layout_
android:text="@+id/todaysmenu" />
</LinearLayout>
试试这样的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="vertical" >
<TextView
android:id="@+id/restaurantname"
android:layout_
android:layout_
android:text="test text"
android:textSize="23dp" >
</TextView>
<TextView
android:id="@+id/textView1"
android:layout_
android:layout_
android:text="test text" />
</LinearLayout>
如果可行,请将您的文本放在 /res/val/string 文件夹中,如下所示:
<string name="testText">Put your text here...</string>
然后像这样调用:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="vertical" >
<TextView
android:id="@+id/restaurantname"
android:layout_
android:layout_
android:text="@string/testText"
android:textSize="23dp" >
</TextView>
<TextView
android:id="@+id/textView1"
android:layout_
android:layout_
android:text="@string/testText" />
</LinearLayout>
然后您可以像这样设置动态值:
TextView tv = (TextView)findViewById(R.id.restaurantname);
tv.setText(values);
【讨论】:
感谢您的建议,但我最终改用了 ArrayAdapter。但是,更改文本值的方式会被记录下来。谢谢。【参考方案4】:我不得不解决同样的问题,并尝试使用上面回答的 arrayadapter,但是没有用。
后来我用baseadapter成功了——这就是适配器:
public class BusinessAdapter2 extends BaseAdapter
private final ArrayList<Business> myList;
LayoutInflater inflater;
Context context;
public BusinessAdapter2(Context context, ArrayList<Business> myList)
this.myList = myList;
this.context = context;
inflater = LayoutInflater.from(this.context);
@Override
public int getCount()
return myList.size();
@Override
public Object getItem(int position)
return myList.get(position);
@Override
public long getItemId(int position)
return 0;
@Override
public View getView(int position, View convertView, ViewGroup parent)
if (convertView==null) convertView = inflater.inflate(R.layout.business_list_item_2, parent, false);
// assign the view we are converting to a local variable
View v = convertView;
Business b = myList.get(position);
if (b != null)
TextView name = (TextView) v.findViewById(R.id.textview_name);
TextView address = (TextView) v.findViewById(R.id.textview_address);
TextView description = (TextView) v.findViewById(R.id.textview_description);
TextView discount = (TextView) v.findViewById(R.id.textview_discount);
// check to see if each individual textview is null.
// if not, assign some text!
if (name != null)
name.setText(b.name);
if (address != null)
address.setText(b.address);
if (description != null)
description.setText(b.description);
if (discount != null)
discount.setText(b.discountRate);
// the view must be returned to our activity
return v;
这是我使用的 Object 类(Business):
public class Business
String name,address,description,discountRate;
public Business()
public Business(String name,String address,String description,String discountRate)
this.name=name;
this.address=address;
this.description=description;
this.discountRate=discountRate;
这就是我将列表视图填充到适配器中的方式:
ArrayList<Business> businesses2=new ArrayList<Business>(Arrays.asList(for_listview_objects));
adapter_objects =new BusinessAdapter2(
context, // The current context (this activity)
businesses2);
listView.setAdapter(adapter_objects);
【讨论】:
以上是关于在列表视图中使用具有更多视图的数组适配器的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 glide* 使用数组列表中的自定义适配器将图像设置为列表视图