1. Download our Official Android App: Forums for Android!

Apps Custom ArrayAdapter Spinner. Cant select item

Discussion in 'Android Development' started by a123send, Jan 3, 2012.

  1. a123send

    a123send Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    2
    Joined:
    Jan 3, 2012

    Jan 3, 2012
    2
    0
    5
    Android 1.6
    There's a collection of such objects
    Code (Text):
    1.  
    2. public class MyItem {
    3.  public String name = "";
    4.  public String brief = "";
    5.          public int availiableweight = 0;
    6.  public Node xmlpoint = null;
    7.  @Override
    8.  public String toString()
    9.  {
    10.   return name;
    11.  }
    12.  public MyItem(String _brief, String _name, Node _xmlpoint)
    13.  {
    14.   name = _name;
    15.   brief = _brief;
    16.   xmlpoint = _xmlpoint;
    17.  }
    18. }
    19.  
    Objects were stored in array: ArrayList<MyItem> itemsList
    Items are visible in dropdown list of spinner, but i cant select any item. Event OnItemSelectedListener is not generated. Spinner control is empty. Where is my fault?

    Code of application

    Code (Text):
    1.  
    2. public class MyActivity extends Activity {
    3.     /** Called when the activity is first created. */
    4.  
    5.  private ArrayList<MyItem> itemsList;
    6.  private Spinner mySpinner;
    7.  
    8.     @Override
    9.     public void onCreate(Bundle savedInstanceState) {
    10.         super.onCreate(savedInstanceState);
    11.         setContentView(R.layout.main);
    12.  
    13.      mySpinner = (Spinner) findViewById(R.id.mySpinner);
    14.  
    15.      itemsList = new ArrayList<MyItem>();
    16.      ArrayAdapter<MyItem> myAdapter = new ArrayAdapter<MyItem>(this, android.R.layout.simple_spinner_item, itemsList);
    17.      myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    18.      mySpinner.setAdapter(myAdapter);
    19.  
    20.      mySpinner.setOnItemSelectedListener(new OnItemSelectedListener()
    21.      {
    22.    @Override
    23.    public void onItemSelected(AdapterView<?> arg0, View arg1,
    24.      int arg2, long arg3)
    25.    {
    26.     // TODO Auto-generated method stub
    27.        AlertDialog("Pos: " + arg2);
    28.    }
    29.    @Override
    30.    public void onNothingSelected(AdapterView<?> arg0)
    31.    {
    32.     // TODO Auto-generated method stub
    33.    }
    34.      });
    35.         itemsList.add(new MyItem("1","one",null));
    36.         itemsList.add(new MyItem("2","two",null));
    37.         itemsList.add(new MyItem("3","three",null));
    38.     }  
    39. }
    40.  
    layout main.xml

    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3.     <LinearLayout xmlns:android="[URL]http://schemas.android.com/apk/res/android[/URL]"
    4.     android:layout_width="fill_parent"
    5.     android:layout_height="fill_parent"
    6.     android:orientation="vertical" >
    7.     <TextView
    8.         android:id="@+id/textView1"
    9.         android:layout_width="wrap_content"
    10.         android:layout_height="wrap_content"
    11.         android:text="TextView" />
    12.     <LinearLayout
    13.         android:id="@+id/linearLayout1"
    14.         android:layout_width="fill_parent"
    15.         android:layout_height="wrap_content" >
    16.         <TextView
    17.             android:id="@+id/languageText"
    18.             android:layout_width="wrap_content"
    19.             android:layout_height="fill_parent"
    20.             android:text="Language" android:gravity="center_vertical"/>
    21.         <Spinner
    22.             android:id="@+id/languageSpinner"
    23.             android:layout_width="wrap_content"
    24.             android:layout_height="wrap_content"
    25.             android:layout_weight="1" android:prompt="@string/chooseitem"/>
    26.     </LinearLayout>
    27. </LinearLayout>
    28.  
     

    Advertisement

Share This Page

Loading...