2010-01-21 4 views
17

Có thể bất kỳ ai cung cấp cho tôi ví dụ về hai_line_list_item không?có thể cung cấp cho tôi ví dụ về Two_line_list_item trong Android không?

+0

[Đây] (http: //mylifewithandroid.blogspot. com/2008/03/my-first-meeting-with-simpleadapter.html) bạn có thể tìm một ví dụ. Kiểu này cung cấp cho bạn một mục có hai hàng văn bản, có thể với định dạng khác nhau. –

+0

Đây không phải là chế độ xem TwoLineListItem, hướng dẫn này đang sử dụng ListView đơn giản. – mudit

Trả lời

37

Tôi chưa tìm thấy ví dụ thực sự sử dụng bố cục được tích hợp sẵn, android.R.layout.two_line_list_itemListView được insted của ListActivity. Vì vậy, ở đây đi.

Nếu bạn đang vội vàng, ghi đè TwoLineArrayAdapter.getView() bên dưới là phần quan trọng trong việc sử dụng bố cục mặc định two_line_list_item.

Dữ liệu của bạn

Bạn có một lớp xác định danh sách của bạn. Tôi sẽ giả sử bạn có một mảng này.

public class Employee { 
    public String name; 
    public String title; 
} 

Một TwoLineArrayAdapter trừu tượng

lớp trừu tượng này có thể được tái sử dụng, và làm cho việc xác định một hai dòng ListView dễ dàng hơn nhiều sau đó. Bạn có thể cung cấp bố cục của riêng bạn, nhưng hàm tạo hai đối số sử dụng bố cục two_line_list_item được cài sẵn. Yêu cầu duy nhất cho bố cục mục danh sách tùy chỉnh là chúng phải sử dụng @android:id/text1@android:id/text2 để xác định số trẻ TextView của chúng, giống như two_line_list_item.

public abstract class TwoLineArrayAdapter<T> extends ArrayAdapter<T> { 
     private int mListItemLayoutResId; 

     public TwoLineArrayAdapter(Context context, T[] ts) { 
      this(context, android.R.layout.two_line_list_item, ts); 
     } 

     public TwoLineArrayAdapter(
       Context context, 
       int listItemLayoutResourceId, 
       T[] ts) { 
      super(context, listItemLayoutResourceId, ts); 
      mListItemLayoutResId = listItemLayoutResourceId; 
     } 

     @Override 
     public android.view.View getView(
       int position, 
       View convertView, 
       ViewGroup parent) { 


      LayoutInflater inflater = (LayoutInflater)getContext() 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      View listItemView = convertView; 
      if (null == convertView) { 
       listItemView = inflater.inflate(
        mListItemLayoutResId, 
        parent, 
        false); 
      } 

      // The ListItemLayout must use the standard text item IDs. 
      TextView lineOneView = (TextView)listItemView.findViewById(
       android.R.id.text1); 
      TextView lineTwoView = (TextView)listItemView.findViewById(
       android.R.id.text2); 

      T t = (T)getItem(position); 
      lineOneView.setText(lineOneText(t)); 
      lineTwoView.setText(lineTwoText(t)); 

      return listItemView; 
     } 

     public abstract String lineOneText(T t); 

     public abstract String lineTwoText(T t); 
} 

Một TwoLineArrayAdapter bê tông

Cuối cùng, đây là đoạn code bạn viết cụ thể để class Employee của bạn để nó sẽ render trong ListView của bạn.

public class EmployeeArrayAdapter extends TwoLineArrayAdapter<Employee> { 
    public EmployeeArrayAdapter(Context context, Employee[] employees) { 
     super(context, employees); 
    } 

    @Override 
    public String lineOneText(Employee e) { 
     return e.name; 
    } 

    @Override 
    public String lineTwoText(Employee e) { 
     return e.title; 
    } 
} 

Activity.onCreate()

Trong phương pháp của hoạt động của bạn onCreate(), bạn sẽ có mã trông như thế này:

employees = new Employee[...]; 
    //...populate the employee array... 

    employeeLV = (ListView)findViewById(R.id.employee_list); 
    employeeLV.setAdapter(new EmployeeArrayAdapter(this, employees); 
+0

câu trả lời tuyệt vời, cảm ơn. tôi có thể đề nghị bổ sung thực hiện getDropDownView (vị trí int, Xem convertView, ViewGroup cha mẹ) để nó xuất hiện một cách chính xác trong bối cảnh đó. Việc triển khai của bạn có thể đơn giản là: return getView (...) – Keith