2012-07-26 94 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); 
相關問題