在listview中使用包含更多视图的数组适配器

时间:2012-07-26 22:22:38

标签: android android-listview android-arrayadapter

我偶然发现了一个我无法理解的问题,所以我希望也许有人遇到同样的问题,或者知道解决问题的好方法。

我创建了一个包含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_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>

活动布局:

<?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>

一开始我把一切都搞定了,但是当我添加了第二个文本字段问题时。提前谢谢你的帮助!

4 个答案:

答案 0 :(得分:21)

要实现此目的,您必须构建自定义适配器并为自定义行布局充气。使用ArrayAdapter将无效,因为

  

默认情况下,此类需要提供的资源ID引用   一个TextView。如果要使用更复杂的布局,请使用也带有字段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

答案 1 :(得分:2)

你可以尝试这个https://devtut.wordpress.com/2011/06/09/custom-arrayadapter-for-a-listview-android/。您必须使用所需的字段创建自定义类Item并扩展ArrayAdapter

答案 2 :(得分:0)

我认为你的问题就在这里:

而不是:

<?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>

尝试这样的事情:

<?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="test text"
        android:textSize="23dp" >

    </TextView>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        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_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="@string/testText"
        android:textSize="23dp" >

    </TextView>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/testText" />

</LinearLayout>

然后,您将设置动态值,如下所示:

TextView tv = (TextView)findViewById(R.id.restaurantname);
tv.setText(values);

答案 3 :(得分:0)

我必须解决同样的问题并尝试使用如上所述的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;
    }
}

这就是我将listview填充到适配器中的方式:

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);