2013-06-05 45 views
5

Tôi sử dụng bộ chuyển đổi = new SimpleCursorAdapter (điều này, android.R.layout.simple_list_item_multiple_choice, cur, cols, views) để tạo điều khiển trắc nghiệm, nhưng tôi không hài lòng về phong cách của chế độ xem văn bản trong kiểm soát nhiều lựa chọn , vì vậy tôi phải sử dụng đoạn mã sau để tạo bố cục mới của điều khiển trắc nghiệm. Nó hoạt động tốt, nhưng tôi không nghĩ rằng đó là một cách tốt, là có bất kỳ mã tốt? Cảm ơn!Làm cách nào để ghi đè kiểu Android.R.layout.simple_list_item_multiple_choice?

bộ chuyển đổi = new SimpleCursorAdapter (điều này, R.layout.mysimple_list_item_multiple_choice, cur, cols, views);

lv.setAdapter(adapter); 
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 

mysimple_list_item_multiple_choice.xml

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1" 
    android:layout_width="match_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:gravity="center_vertical" 
    android:checkMark="?android:attr/listChoiceIndicatorMultiple" 
    android:paddingLeft="6dip" 
    android:paddingRight="6dip" 
    android:ellipsize="end" 
    android:singleLine="true" 

/> 
+0

gothrough 'bộ điều hợp theo yêu cầu' trong android – Pinki

Trả lời

0

Bạn cần phải mở rộng BaseAdapter để tạo ra một tùy chỉnh Adapter.With Một bộ chuyển đổi tùy chỉnh mỗi hàng của listview sử dụng một bố cục xml và do đó bạn có thể tạo một bố cục tùy chỉnh cho hàng.

Ví dụ mã của adapter tùy chỉnh:

public class ImageAdapter extends BaseAdapter { 
    private Context context; 
    private final String[] StrValues; 

    public ImageAdapter(Context context, String[] StrValues) { 
     this.context = context; 
     this.StrValues = StrValues; 
    } 

    // getView that displays the data at the specified position in the data set. 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // create a new LayoutInflater 
     LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     View view; 
     view = null; 
     convertView = null;// avoids recycling of list view 
     if (convertView == null) { 

      view = new View(context); 
      // inflating grid view item 
      view = inflater.inflate(R.layout.list_view_item, null); 

      // set value into textview 
      TextView textView = (TextView) view 
        .findViewById(R.id.list_item_label); 
      textView.setText(StrValues[position]); 

     } 

     return view; 
    } 

    // Total number of items contained within the adapter 
    @Override 
    public int getCount() { 
     return StrValues.length; 
    } 

    @Override 
    public Object getItem(int position) { 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 

} 

bộ chuyển đổi Thiết:

listView = (ListView) findViewById(R.id.listView1); 
    // setting adapter on listview 
    listView.setAdapter(new ImageAdapter(this, StrValues)); 

Liên kết các ví dụ:

example 1 example 2

R.layout.list_view_item là tùy chỉnh xml fo r xem danh sách của bạn, trong đó bạn có thể thêm các khung nhìn mong muốn.

+0

Cảm ơn! nhưng cách của bạn vẫn cần viết nhiều mã. :( –

+0

nếu bạn phải làm một số điều không được cung cấp theo mặc định, thì không có lựa chọn nào khác .AFAIK nó chỉ có thể được thực hiện bằng bộ điều hợp tùy chỉnh –

+0

Đây là một cách phức tạp để đạt được một nhiệm vụ IMHO đơn giản như vậy. –

3

Đơn giản.

Sử dụng <include>.

Tạo bố cục XML mới.

 <include android:id=”@+android:id/simple_list_item_multiple_choice” 
    android:layout_width=”match_parent” 
    android:layout_height=”match_parent” 
    layout=”@layout/title”/> 

Bạn có thể ghi đè lên tất cả các thông số bố trí (bất kỳ android:layout_* thuộc tính) của xem gốc bố trí bao gồm bằng cách chỉ định chúng trong thẻ.

Các vấn đề liên quan