我尝试使用自定义ArrayAdapter填充微调器。我认为微调器正常填充,但是当应用程序打开它时,它会出错。
Obs:我不会说英语,抱歉。
Obs2:我用ListView做到了,效果非常好。现在有了Spinner ......
我的代码:
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)
答案 0 :(得分:1)
您应该使用setDropDownViewResource(int)来指定适配器用于显示微调器选项列表的布局(simple_spinner_dropdown_item是平台定义的另一种标准布局)。
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
检查文档http://developer.android.com/guide/topics/ui/controls/spinner.html
答案 1 :(得分:0)
你的答案在这里:
How to add Image to spinner in Android
部署&#39; getDropDownView&#39;适配器上的方法。
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
TextView label = (TextView) super.getDropDownView(position, convertView, parent);
PaymentType paymentType = getItem(position);
label.setText(paymentType.getStringResourceId());
label.setCompoundDrawablesWithIntrinsicBounds(0, 0, paymentType.
getImageResourceId(), 0);
return label;
}