单击微调器时出错

时间:2021-04-03 21:16:41

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.

我正在尝试使用自定义ArrayAdapter填充微调器。我认为微调器正常填充,但是当应用程序打开它时,它会出错。

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

Obs:我不会说英语,对不起。

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

Obs2:我用ListView做到了,效果很好。现在有了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)

2 个解决方案

#1


1  

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).

您应该使用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);

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

查看文档http://developer.android.com/guide/topics/ui/controls/spinner.html

#2


0  

Your answer is here:

你的答案在这里:

How to add Image to spinner in Android

如何在Android中将图像添加到微调器

Deploy the 'getDropDownView' method on your adapter.

在适配器上部署'getDropDownView'方法。

@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;
}

#1


1  

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).

您应该使用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);

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

查看文档http://developer.android.com/guide/topics/ui/controls/spinner.html

#2


0  

Your answer is here:

你的答案在这里:

How to add Image to spinner in Android

如何在Android中将图像添加到微调器

Deploy the 'getDropDownView' method on your adapter.

在适配器上部署'getDropDownView'方法。

@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;
}