• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Custom ArrayAdapter Spinner. Cant select item

a123send

Lurker
Android 1.6
There's a collection of such objects
Code:
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;
 }
}

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:
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));
    }  
}

layout main.xml

Code:
<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="[URL]http://schemas.android.com/apk/res/android[/URL]"
    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>
 
Back
Top Bottom