2013-08-29 51 views
1

我在頁面中包含動態微調。 單擊微調器時,它將下拉圖像和文本的選擇。 問題是,當我想選擇下面的值之一,如「類別B」,它不能點擊。 爲什麼?我的代碼有任何問題?動態微調 - 無法點擊下拉列表 - Android

真的需要某人的幫助。

感謝

* Category.java ***

private Spinner iconSpinner; 
String[] iconArrays = {"Category A", "Category B"}; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_categorydetails); 
    addListenerOnSpinnerItemSelection(); 
} 

public void addListenerOnSpinnerItemSelection(){ 

    iconSpinner = (Spinner) findViewById(R.id.iconSpinner); 
    iconSpinner.setAdapter(new MyCustomAdapter(this, R.layout.icon_row, iconArrays)); 
    iconSpinner.setOnItemSelectedListener(this); 
} 



public class MyCustomAdapter extends ArrayAdapter<String>{ 


    public MyCustomAdapter(Context context, int textViewResourceId, 
    String[] objects) { 
     super(context, textViewResourceId, objects); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public View getDropDownView(int position, View convertView, 
    ViewGroup parent) { 
     // TODO Auto-generated method stub 
     return getCustomView(position, convertView, parent); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     return getCustomView(position, convertView, parent); 
    } 

    public View getCustomView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     //return super.getView(position, convertView, parent); 

     //LayoutInflater inflater = (LayoutInflater) CategoryDetailsActivity.this.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     LayoutInflater inflater = getLayoutInflater(); 
     View row=inflater.inflate(R.layout.icon_row, parent, false); 
     TextView label=(TextView)row.findViewById(R.id.iconLabel); 
     label.setText(iconArrays[position]); 

     ImageView icon=(ImageView)row.findViewById(R.id.icon); 

     if (iconArrays[position]=="Category A"){ 
      icon.setImageResource(R.drawable.ic_catA); 
     } 
     else{ 
      icon.setImageResource(R.drawable.ic_catB); 
     } 

     return row; 
    } 
} 



@Override 
public void onItemSelected(AdapterView<?> arg0, View arg1, int pos, 
     long arg3) { 
    // TODO Auto-generated method stub 
    Toast.makeText(arg0.getContext(), 
      "OnItemSelectedListener : " + arg0.getItemAtPosition(pos).toString(), 
      Toast.LENGTH_SHORT).show(); 
} 

@Override 
public void onNothingSelected(AdapterView<?> arg0) { 
    // TODO Auto-generated method stub 

} 

* icon_row.xml ***

<?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="50dip" 
    android:orientation="horizontal"> 
    <ImageView 
    android:id="@+id/icon" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:contentDescription="@string/stringIcon" 
    android:layout_margin="10dip"/> 
    <TextView 
    android:id="@+id/iconLabel" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textIsSelectable="true" 
    android:layout_margin="10dip"/> 
</LinearLayout> 

回答

0

icon_row.xml

刪除機器人:textIsSelectable =TextView的 「真」

1
iconSpinner.setOnItemSelectedListener(new OnItemSelectedListener() { 
    @Override 
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) { 
     // your code here 
    } 

    @Override 
    public void onNothingSelected(AdapterView<?> parentView) { 
     // your code here 
    } 

}); 

難道üTR你呢?

EDIT

嘗試此方法,

activity_main.xml中

<Spinner 
      android:id="@+id/spinner" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 

      android:entries="@array/list" 
      android:prompt="@string/select_menu" /> 

的strings.xml

<string name="select_menu">Select Category</string> 

    <string-array name="list"> 
     <item >Category A</item> 
     <item >Category B</item> 

    </string-array> 

的Java文件

iconspinner = (Spinner) findViewById(R.id.spinner); 

    ArrayAdapter<CharSequence> cat_list = ArrayAdapter.createFromResource(
      this, R.array.list, android.R.layout.simple_spinner_item); 

    cat_list.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    iconspinner.setAdapter(cat_list); 

    iconspinner.setOnItemSelectedListener(new OnItemSelectedListener() { 

     @Override 
     public void onItemSelected(AdapterView<?> arg0, View arg1, 
       int arg2, long arg3) { 
      // TODO Auto-generated method stub 
      Toast.makeText(getApplicationContext(), 
        iconspinner.getSelectedItem().toString(), 
        Toast.LENGTH_LONG).show(); 
     } 

     @Override 
     public void onNothingSelected(AdapterView<?> arg0) { 
      // TODO Auto-generated method stub 

     } 

    }); 

我試圖這樣做,它是工作的罰款。只是檢查

+0

是的,它仍然無法點擊。 – user831098

+0

<微調機器人:layout_height = 「WRAP_CONTENT」 機器人:ID = 「@ + ID/iconSpinner」 機器人:layout_width = 「match_parent」 機器人:提示= 「@串/ icon_prompt」 機器人:layout_marginBottom = 「10dip」 android:layout_marginLeft =「10dip」 android:layout_marginRight =「10dip」 > – user831098

+0

以上是我添加微調的XML – user831098