2012-01-03 88 views
3

的Android 1.6 有自定義ArrayAdapter和微調器。沒有項目選擇

public class MyItem { 
public String name = ""; 
public String brief = ""; 
    public int availiableweight = 0; 
public Node xmlpoint = null; 

@Override 
public String toString() 
{ 
    return name; 
} 
public MyItem(String _brief, String _name, Node _xmlpoint) 
{ 
    name = _name; 
    brief = _brief; 
    xmlpoint = _xmlpoint; 
} 
} 

對象被存儲在陣列,例如對象的集合:ArrayList的itemsList

項目是在轉的下拉列表中可見,但我不能選擇的任何項目。未生成事件OnItemSelectedListener。微調控制是空的。我的錯在哪裏?應用

代碼

public class MyActivity extends Activity { 
/** Called when the activity is first created. */ 

private ArrayList<MyItem> itemsList; 
private Spinner mySpinner; 

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

    mySpinner = (Spinner) findViewById(R.id.mySpinner); 

    itemsList = new ArrayList<MyItem>(); 
    ArrayAdapter<MyItem> myAdapter = new ArrayAdapter<MyItem>(this, android.R.layout.simple_spinner_item, itemsList); 
    myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    mySpinner.setAdapter(myAdapter); 

    mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() 
    { 

     @Override 
     public void onItemSelected(AdapterView<?> arg0, View arg1, 
       int arg2, long arg3) 
     { 
      // TODO Auto-generated method stub 
      AlertDialog("Pos: " + arg2); 
     } 

     @Override 
     public void onNothingSelected(AdapterView<?> arg0) 
     { 
      // TODO Auto-generated method stub 
     } 
    }); 
    itemsList.add(new MyItem("1","one",null)); 
    itemsList.add(new MyItem("2","two",null)); 
    itemsList.add(new MyItem("3","three",null)); 
} 
} 

佈局main.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="fill_parent" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="TextView" /> 

<LinearLayout 
    android:id="@+id/linearLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 

    <TextView 
     android:id="@+id/languageText" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:text="Language" android:gravity="center_vertical"/> 

    <Spinner 
     android:id="@+id/languageSpinner" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" android:prompt="@string/chooseitem"/> 

</LinearLayout> 

</LinearLayout> 

回答

4

你的問題是在這裏:

ArrayAdapter<MyItem> myAdapter = new ArrayAdapter<MyItem>(this, android.R.layout.simple_spinner_item, itemsList); 

你不能因爲你必須使用通用的一個ArrayAdapter類自定義對象數組。在ArrayAdapter的構造函數中,使用android.R.layout.simple_spinner_item佈局文件。但適配器不知道如何顯示數據。

您必須使用自定義適配器並創建自己的XML佈局文件。 閱讀有關如何實現此目的的一些示例。最好了解ListViews和適配器的工作方式。

請參見以下鏈接:

http://android.vexedlogic.com/2011/04/02/android-lists-listactivity-and-listview-i-basic-usage/

http://android.vexedlogic.com/2011/04/02/android-lists-listactivity-and-listview-ii-%E2%80%93-custom-adapter-and-list-item-view/

他們提供了一個很好的參考。您可以繼續閱讀「Android列表」系列教程的下一部分,以擴展您的知識。

希望這會有所幫助。如果您需要進一步解釋,請務必發表評論。

1

感謝Bandreid的回答,但決定是另一回事。 不要直接使用ArrayList,只能通過適配器。 此代碼無誤

myAdapter.add(new MyItem("1","one",null));  
myAdapter.add(new MyItem("2","two",null));  
myAdapter.add(new MyItem("3","three",null));