11

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

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

4 回答 4

22

要实现这一点,您必须构建一个自定义适配器并扩展您的自定义行布局。使用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

于 2012-07-26T22:57:38.953 回答
2

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

于 2012-07-26T22:54:29.620 回答
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);
于 2012-07-26T22:59:41.420 回答
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;
    }
}

这是我使用的对象类(业务):

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);
于 2016-05-05T01:50:31.157 回答