我想用自定義的ArrayAdapter填充微調框。我認爲微調器是正常填充的,但是當應用程序打開它時,會出現錯誤。當我點擊微調框時出錯
Obs:我不會說英語很好,對不起。
Obs2:我這樣做,用ListView,它工作得很好。現在,隨着微調...
我的代碼:
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;
}
}
錯誤:
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)
它的工作,謝謝。但現在在哪裏顯示文本dropDown,顯示com.jeanjn.guiadeacademia ... –
我不明白你的意思是「但現在在哪裏顯示文本dropDown,顯示com.jeanjn.guiadeacademia ...」 – Libin
當我點擊微調框並顯示列表時,應該顯示textview中的文本,但它是顯示com.jeanjn.guiadeacademia .... –