2012-07-26 62 views
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> 

在我得到的一切工作的开始,但是当我加入第二个textfield问题引起了。提前,谢谢你的帮助!

回答

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

这一切,它应该nicelly工作,但我强烈建议你到谷歌的一些更好的替代品执行其他Adapters

+0

谢谢。这有帮助!谢谢。 – olovholm 2012-07-31 16:42:51

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

感谢您的建议,但我最终改用了ArrayAdapter。虽然改变文本值的方式。谢谢。 – olovholm 2012-07-31 16:41:53

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类(商务):

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); 
相关问题