2012-07-26 28 views
11

Tôi đã vấp phải một vấn đề Tôi không thể có được đầu của tôi xung quanh, vì vậy tôi hy vọng có lẽ ai đó ở đây đã có cùng một vấn đề hoặc biết cách giải quyết vấn đề .Sử dụng bộ điều hợp mảng có nhiều lượt xem hơn trong hàng trong listview

Tôi đã tạo chế độ xem có chứa một ListView. ListView này chứa hai TextView. Vấn đề là tôi không biết nơi tôi gửi các giá trị có nghĩa là để đi trong xem văn bản thứ hai bằng cách sử dụng ArrayAdapter. Có cách nào để gửi thêm thông tin cho ArrayAdapter để tôi có thể nạp TextView "todaysmenu" không?

Phương pháp 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); 
} 

Các đánh dấu hàng:

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

Cách bố trí hoạt động:

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

Vào đầu tôi có tất cả mọi thứ để làm việc, nhưng khi tôi thêm các vấn đề textfield thứ hai khơi dậy. Trước, cảm ơn sự giúp đỡ của bạn!

Trả lời

21

Để đạt được điều này, bạn phải tạo bộ điều hợp tùy chỉnh và tăng bố cục hàng tùy chỉnh của mình. Sử dụng ArrayAdapter sẽ không hoạt động bởi vì

Theo mặc định, lớp này yêu cầu id tài nguyên được cung cấp tham khảo một TextView duy nhất. Nếu bạn muốn sử dụng bố cục phức tạp hơn, hãy sử dụng các hàm tạo cũng có một id trường. Id trường đó sẽ tham chiếu một TextView trong tài nguyên bố cục lớn hơn.

Vì vậy, lớp bộ chuyển đổi tùy chỉnh của bạn có thể somthing như:

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 lớp học của bạn có thể đơn giản như tôi mô tả dưới đây:

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

Bây giờ, trong bạn hoạt động chính chỉ ràng buộc bạn danh sách với một số dữ liệu, như;

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

Tại thời điểm này bạn có thể thiết lập adapter tùy chỉnh vào danh sách

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

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

Đây là tất cả và nó cũng làm việc nicelly, nhưng tôi khuyên bạn google đối với một số lựa chọn thay thế tốt hơn để thực hiện những người khác Adapters.

+0

Cảm ơn bạn. Điều đó đã giúp! Cảm ơn bạn. – olovholm

2

Bạn có thể thử điều này https://devtut.wordpress.com/2011/06/09/custom-arrayadapter-for-a-listview-android/. Bạn phải tạo một mục lớp tùy chỉnh với các trường bạn cần và mở rộng ArrayAdapter.

+0

Cảm ơn đề xuất của bạn. – olovholm

+0

Giải pháp từ liên kết bạn đề xuất hoạt động tốt cho tôi. Có lẽ bạn nên sao chép và dán mã vào câu trả lời của bạn và tham khảo URL trong trường hợp blog sẽ bị xóa. – Peter

0

Tôi nghĩ rằng vấn đề của bạn là ở đây:

Thay vì điều này:

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

Hãy thử một cái gì đó như thế này:

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

Nếu thành công, sau đó đặt văn bản của bạn trong thư mục/thư mục res/val/string như vậy:

<string name="testText">Put your text here...</string> 

và sau đó gọi như thế này:

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

Sau đó bạn sẽ thiết lập các giá trị động như thế này:

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

Cảm ơn đề xuất của bạn, nhưng tôi đã kết thúc bằng cách sử dụng một ArrayAdapter thay thế. Tuy nhiên, cách thay đổi giá trị văn bản được ghi nhận. Cảm ơn. – olovholm

0

tôi phải giải quyết cùng một vấn đề và cố gắng sử dụng arrayadapter như đã trả lời ở đây trên, nhưng nó không hoạt động.

Sau đó tôi đã thành công để làm điều đó với baseadapter - đây là bộ chuyển đổi:

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

này là lớp Object tôi đã sử dụng (Kinh doanh):

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

và đây là cách tôi cư chế độ xem danh sách vào bộ điều hợp:

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); 
Các vấn đề liên quan