使用数组适配器行更多的意见列表视图 [英] Use array adapter with more views in row in listview

查看:110
本文介绍了使用数组适配器行更多的意见列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶然发现了一个问题,我不能完全得到我的头左右,所以我希望也许这里有人有过同样的问题或知道解决问题的好办法。

I have stumbled upon a problem I can't quite get my head around, so I was hoping perhaps someone here have had the same problem or knew a good way of solving the problem.

我已经创建了一个包含一个ListView的视图。该ListView控件包含两个TextView中。 问题是,我不知道在哪里我送这是指使用一个ArrayAdapter在第二个文本视图去的值。有没有一种方法,以提供更多的信息发送到一个ArrayAdapter,这样我可以养活todaysmenu的TextView?

I have created a view containing a ListView. This ListView contains two TextView. The problem is that I don't know where I send the values which are meant to go in the second text view using the ArrayAdapter. Is there a way to send with more information to the ArrayAdapter so that I can feed the "todaysmenu" TextView?

一个ArrayAdapter方法​​:

The ArrayAdapter method:

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

该行的标记:

The row markup:

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

活动的布局:

The activity layout:

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

在开始的时候我得到的一切工作,但是当我添加第二个文本框的问题引起。在前进,感谢您的帮助!

At the beginning I got everything to work, but when I added the second textfield problems arouse. In advance, thank you for your help!

推荐答案

要做到这一点,你必须建立一个自定义的适配器和膨胀您的自定义行布局。使用 ArrayAdapter 将无法工作,因为

To achieve this you have to build a custom adapter and inflate your custom row layout. Using ArrayAdapter won't work because

在默认情况下此类预期所提供的资源ID引用   单一的TextView。如果你想使用更复杂的布局,使用构造函数,还需要一个字段id。那场ID应该引用一个TextView在较大的布局资源。

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.

那么,您的自定义适配器类可能是财产以后这样的:

So, your custom adapter class could be somthing like:

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 类可以简单,只要我描述如下:

And your Restaurant.java class could as simple as I describe below:

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

现在,只需绑定您列出了一些数据,你的主要活动,等等;

Now, in you main activity just bind you list with some data, like;

/** 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"));

在这一点上你可以自定义适配器设置为你的清单

At this point you're able to set the custom adapter to your list

ListView lv = (ListView) findViewById(R.id.mylist);

CustomAdapter adapter = new CustomAdapter(YourMainActivityName.this, list);
lv.setAdapter(adapter);

这是所有的,它应该工作nicelly,但我强烈建议你在google上寻找更好的替代品,以实现人的适配器的。

This is all and it should work nicelly, but I strongly recommend you to google for some better alternatives to implement others Adapters.

这篇关于使用数组适配器行更多的意见列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆