2010-07-09 47 views
0

我正在Eclipse中爲Android開發一個應用程序。目前我的目標是API級別3,但我錯誤地在Android 1.6模擬器(API級別4)上進行了測試。在1.6上它工作正常,但在1.5我的ListView與CHOICE_MODE_SINGLE不會選擇項目,當他們被點擊。ListView不檢查Android上的項目1.5

這裏是我的ListView XML:

<ListView 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:layout_weight="1" 
    android:id="@+id/ListDomains" 
    android:layout_margin="5px" 
    android:choiceMode="singleChoice" 
    android:clickable="false" 
    android:focusable="true" 
    android:focusableInTouchMode="true" 
    android:descendantFocusability="beforeDescendants" 
> 
</ListView> 

下面是在ListView的項目的XML:

<?xml version="1.0" encoding="utf-8"?> 
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="wrap_content" 
    android:id="@+id/domain_list_value" 
    android:checkMark="?android:attr/listChoiceIndicatorSingle" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:layout_width="fill_parent" 
> 
</CheckedTextView> 

我創建一個自定義的ArrayList適配器,讓我定製getView。下面是DomainArrayAdapter代碼:

public class DomainArrayAdapter extends ArrayAdapter<char[]> { 

    private LayoutInflater mInflater; 

    public DomainArrayAdapter(Context context, int textViewResourceId, 
      List<char[]> objects) {  
     super(context, textViewResourceId, objects);  
     mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     if(convertView == null){ 
      convertView = mInflater.inflate(R.layout.domain_list, null); 
     } 

     char[] text = super.getItem(position); 

     ((CheckedTextView)convertView).setText(text, 0, text.length); 
     return convertView; 
    } 

} 

的所有這些代碼工作正常編譯對API級別3和在Android 1.6模擬器中運行。但是,針對1.5模擬器運行時,ListView中的項不會在點擊時進行檢查。

任何想法?

回答

1

它似乎Android 1.5不尊重listview XML中設置的choiceMode。以編程方式設置它會使其正常工作:

ListView listDomains = (ListView) findViewById(R.id.ListDomains); 

    Log.d("app", String.valueOf(listDomains.getChoiceMode())); //prints 0 

    listDomains.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 

    Log.d("app", String.valueOf(listDomains.getChoiceMode())); //prints 1 

是否有其他人看到過這種行爲?

相關問題