Jean Carlos Jean Carlos - 29 days ago 23
Android Question

Error when I click on spinner

I'm trying to fill the spinner using a custom ArrayAdapter. I think the spinner is filled normal, but when the application opens it, it gets an error.

Obs: I don't speak english very well, sorry.

Obs2: I did that, with a ListView, and it worked very well. Now with Spinner...

My code:

private void carregarSpinner() {
Spinner spinner = (Spinner) findViewById(R.id.CateSpinner);
DataBase db = new DataBase(this);
Cursor cursor = db.getData("SELECT * FROM Categoria");
ArrayList<Categoria> txts = new ArrayList<Categoria>();
while(cursor.moveToNext())
{
Categoria ex = new Categoria();
ex.setNome(cursor.getString(1));
ex.setId(cursor.getInt(0));
txts.add(ex);
}
AdapterCategoria adapter = new AdapterCategoria(this, txts);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
db.closeDB(cursor);
}


public class AdapterCategoria extends ArrayAdapter<Categoria> {

private final Context context;
private final ArrayList<Categoria> itemsArrayList;

public AdapterCategoria(Context context, ArrayList<Categoria> itemsArrayList) {

super(context, R.id.txvCategoria, itemsArrayList);

this.context = context;
this.itemsArrayList = itemsArrayList;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

// 1. Create inflater
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

// 2. Get rowView from inflater
View rowView = inflater.inflate(R.layout.categoria_model, parent, false);

// 3. Get the two text view from the rowView
TextView labelView = (TextView) rowView.findViewById(R.id.txvCategoria);

// 4. Set the text for textView
labelView.setText(itemsArrayList.get(position).getNome());
labelView.setTag(itemsArrayList.get(position).getId());

// 5. retrn rowView
return rowView;
}
}


The error:

04-26 18:52:49.437: E/AndroidRuntime(28710): FATAL EXCEPTION: main
04-26 18:52:49.437: E/AndroidRuntime(28710): android.content.res.Resources$NotFoundException: Resource ID #0x7f0a0007 type #0x12 is not valid
04-26 18:52:49.437: E/AndroidRuntime(28710): at android.content.res.Resources.loadXmlResourceParser(Resources.java:2136)
04-26 18:52:49.437: E/AndroidRuntime(28710): at android.content.res.Resources.getLayout(Resources.java:865)
04-26 18:52:49.437: E/AndroidRuntime(28710): at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
04-26 18:52:49.437: E/AndroidRuntime(28710): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:371)
04-26 18:52:49.437: E/AndroidRuntime(28710): at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:416)
04-26 18:52:49.437: E/AndroidRuntime(28710): at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:741)
04-26 18:52:49.437: E/AndroidRuntime(28710): at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:737)
04-26 18:52:49.437: E/AndroidRuntime(28710): at android.widget.AbsListView.obtainView(AbsListView.java:2465)
04-26 18:52:49.437: E/AndroidRuntime(28710): at android.widget.ListPopupWindow$DropDownListView.obtainView(ListPopupWindow.java:1188)
04-26 18:52:49.437: E/AndroidRuntime(28710): at android.widget.ListView.measureHeightOfChildren(ListView.java:1250)
04-26 18:52:49.437: E/AndroidRuntime(28710): at android.widget.ListPopupWindow.buildDropDown(ListPopupWindow.java:1115)
04-26 18:52:49.437: E/AndroidRuntime(28710): at android.widget.ListPopupWindow.show(ListPopupWindow.java:524)
04-26 18:52:49.437: E/AndroidRuntime(28710): at android.widget.Spinner$DropdownPopup.show(Spinner.java:983)
04-26 18:52:49.437: E/AndroidRuntime(28710): at android.widget.Spinner.performClick(Spinner.java:608)
04-26 18:52:49.437: E/AndroidRuntime(28710): at android.view.View$PerformClick.run(View.java:17082)
04-26 18:52:49.437: E/AndroidRuntime(28710): at android.os.Handler.handleCallback(Handler.java:615)
04-26 18:52:49.437: E/AndroidRuntime(28710): at android.os.Handler.dispatchMessage(Handler.java:92)
04-26 18:52:49.437: E/AndroidRuntime(28710): at android.os.Looper.loop(Looper.java:137)
04-26 18:52:49.437: E/AndroidRuntime(28710): at android.app.ActivityThread.main(ActivityThread.java:4867)
04-26 18:52:49.437: E/AndroidRuntime(28710): at java.lang.reflect.Method.invokeNative(Native Method)
04-26 18:52:49.437: E/AndroidRuntime(28710): at java.lang.reflect.Method.invoke(Method.java:511)
04-26 18:52:49.437: E/AndroidRuntime(28710): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
04-26 18:52:49.437: E/AndroidRuntime(28710): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
04-26 18:52:49.437: E/AndroidRuntime(28710): at dalvik.system.NativeStart.main(Native Method)

Answer

You should use setDropDownViewResource(int) to specify the layout the adapter should use to display the list of spinner choices (simple_spinner_dropdown_item is another standard layout defined by the platform).

  // Specify the layout to use when the list of choices appears
 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

Check the document http://developer.android.com/guide/topics/ui/controls/spinner.html