THIS code WORKS, the spinner displays the field correctly, however I should say maybe it is not 100% perfect, for some reason they cannot leave the initial field value empty, it defaults to item 0. package com.cccheck ;
public class OneCheckActivity extends Activity { LayoutInflater factory; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.spinner_view); ArrayList tdata = new ArrayList<MyData>(); MyData mdata =new MyData(); mdata.setName(""); mdata.setData("-1"); MyData ndata =new MyData(); ndata.setName("ciao belluzzo"); ndata.setData("1"); tdata.add(mdata); tdata.add(ndata); mdata= new MyData(); mdata.setName("vai alla fnac"); mdata.setData("2"); tdata.add(mdata); mSpinner = (Spinner) findViewById(R.id.my_spinner); factory = LayoutInflater.from(this); populateSpinner(tdata); setUpAdapter(); mSpinner.setSelected(false); try { mAdapter.notify(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } //mAdapter.registerDataSetObserver(new MyObserver()); } ArrayList<MyData> mData; MyAdapter mAdapter = new MyAdapter(null); Spinner mSpinner; // This sets up the adapter and the arraylist that contains the data private void setUpAdapter() { mSpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { MyData g = (MyData) parent.getItemAtPosition(pos); // TODO Toast.makeText(OneCheckActivity.this , "selected item : " + pos + ", value: " + g.getData(),Toast.LENGTH_LONG).show(); } @Override public void onNothingSelected(AdapterView parent) { // Do nothing. } }); } // this populates the arraylist that is attached to the spinner adapter //it is called once an AsyncTask finishes pulling data from a local database private void populateSpinner(ArrayList<MyData> result) { if (result != null) { if (mData == null) { mData = new ArrayList<MyData>(); } else { mData.clear(); } for (int index = 0; index < result.size(); index++) { mData.add(result.get(index)); } mAdapter = new MyAdapter(mData); mSpinner.setAdapter(mAdapter); } } // this is the adapter for the spinner private class MyAdapter implements SpinnerAdapter { ArrayList<MyData> data; public MyAdapter(ArrayList<MyData> data){ this.data = data; } public void updateData(ArrayList<MyData> data){ this.data = data; } @Override public int getCount() { return data.size(); } @Override public Object getItem(int position) { return data.get(position); } @Override public long getItemId(int position) { return position; } @Override public int getItemViewType(int position) { return android.R.layout.simple_spinner_dropdown_item; } @Override public LinearLayout getView(int position, View convertView, ViewGroup parent) { LinearLayout pv = (LinearLayout)(factory.inflate(R.layout.spinner_item, null)); TextView tv = (TextView) pv.findViewById(R.id.textviewid); tv.setTextColor(Color.BLACK); MyData item = data.get(position); tv.setText( item.getName() + " - " + item.getData() + " "); tv.setPadding(0, 20, 0, 20); return pv; } @Override public int getViewTypeCount() { return 1; } @Override public boolean hasStableIds() { return false; } @Override public boolean isEmpty() { return data.isEmpty(); } @Override public void registerDataSetObserver(DataSetObserver observer) { // TODO Auto-generated method stub } @Override public void unregisterDataSetObserver(DataSetObserver observer) { // TODO Auto-generated method stub } @Override public LinearLayout getDropDownView(int position, View convertView, ViewGroup parent) { if (convertView instanceof LinearLayout) System.out.println("%%%%%%%%%%%%%%55555 hai ragione"); return this.getView(position, convertView, parent); } } }
use this as a layout for spinner_item.xml
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:textSize="28px" android:id="@+id/textviewid" /> </LinearLayout>
user931560
source share