2012-04-02 58 views
11

Tôi muốn hiển thị hai giá trị trong chế độ xem thả xuống của xoay tròn.Ví dụ về mục spinner setDropDownViewResource tùy chỉnh

Hiện tại, tên này chỉ có tên thành phố, nhưng tôi cũng muốn thêm một trường khoảng cách nhỏ vào đó.

MyCity<MyCityDistance> dataAdapter; 

    dataAdapter = new MyCity(this, R.layout.mycityrow, list); 
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

tôi có tất cả các mã cho bộ chuyển đổi dữ liệu tùy chỉnh, exapanding xem và giữ của tôi, vv

Tuy nhiên, mục mà được chương trình không hiển thị cả các thành phố và khoảng cách của nó từ vị trí hiện tại của tôi.

Nó chỉ hiển thị những gì được ghi đè trong phương thức toString() của lớp MyCityDistance.

Tôi thậm chí còn cố gắng thiết lập

dataAdapter.setDropDownViewResource(R.layout.mycityrow);

nhưng không thành công. Nó ném một lỗi.

04-02 11:05:22.600: E/AndroidRuntime(367): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView 
04-02 11:05:22.600: E/AndroidRuntime(367): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:347) 
04-02 11:05:22.600: E/AndroidRuntime(367): at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:376) 
04-02 11:05:22.600: E/AndroidRuntime(367): at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:332) 

Ví dụ về cách tạo tùy chỉnh của riêng bạn setDropDownViewResource() là gì?

Ngay cả khi tôi nhận xét dòng setDropDownViewResource(), tôi cũng gặp lỗi tương tự.

Lưu ý: Hiệu ứng duy nhất mycityrow hiện tại là phần tử đầu tiên của Spinner được hiển thị theo bố cục của mycityrow. Tuy nhiên, khi tôi nhấp vào mở trình đơn thả xuống, bố cục đó sẽ bị mất. Tôi muốn bố trí tương tự trong quá trình lựa chọn thả xuống quá.

Trả lời

20

Lưu ý ví dụ dưới đây sử dụng chế độ sẵn có android.R.layout.simple_list_item_2, Rất tiếc, màu văn bản có thể sẽ giống với nền. Bạn chỉ có thể giải quyết vấn đề này bằng cách tạo chế độ xem tùy chỉnh của riêng mình và sử dụng chế độ xem đó trong bộ điều hợp thay thế.

Hãy cho tôi biết nếu tôi nên giải thích bất kỳ phần nào của nó.

public class MainActivity extends Activity { 

    class City { 
     public City(String city, int d) { 
      this.city = city; 
      this.distance = String.valueOf(d); 
     } 

     String city; 
     String distance; 
    } 

    class CityAdapter extends ArrayAdapter<City> { 

     public CityAdapter(Context context, List<City> objects) { 
      super(context, android.R.layout.simple_list_item_2, objects); 
     } 

     @Override //don't override if you don't want the default spinner to be a two line view 
     public View getView(int position, View convertView, ViewGroup parent) { 
      return initView(position, convertView); 
     } 

     @Override 
     public View getDropDownView(int position, View convertView, 
            ViewGroup parent) { 
      return initView(position, convertView); 
     } 

     private View initView(int position, View convertView) { 
      if(convertView == null) 
       convertView = View.inflate(getContext(), 
              android.R.layout.simple_list_item_2, 
              null); 
      TextView tvText1 = (TextView)convertView.findViewById(android.R.id.text1); 
      TextView tvText2 = (TextView)convertView.findViewById(android.R.id.text2); 
      tvText1.setText(getItem(position).city); 
      tvText2.setText(getItem(position).distance); 
      return convertView; 
     } 
    } 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Spinner spinner = (Spinner)findViewById(R.id.spinner1); 
     List<City> list = new ArrayList<MainActivity.City>(); 
     for(int i = 0; i < 10; i++) 
      list.add(new City(String.format("City %d", i + 1), (i + 1) * 1000)); 
     spinner.setAdapter(new CityAdapter(this, list)); 

    } 

} 
+0

Tôi tự hỏi làm thế nào nó hoạt động với một phụ huynh null? –

+0

Truyền phụ huynh là null tương đương với việc chuyển 'attachToRoot' thành false khi được tăng cường bằng cách sử dụng LayoutInflater. – st0le

+0

Có giải pháp mã này thực hiện :) Cảm ơn –

1

Hãy thử nhận xét dữ liệu dòngAdapter.setDropDownViewResource() và bộ điều hợp sẽ cố gắng sử dụng tệp bố cục mycityow cho trình đơn thả xuống. Hoạt động trong các trường hợp đơn giản.

+0

Cảm ơn, tôi đã thử phương pháp đó trước đó, cập nhật câu hỏi của tôi ngay bây giờ. –