Android Spinner: Custom Adapter Display

柔情痞子 提交于 2019-12-11 07:17:30

问题


I am using a SimpleCursorAdapter on my spinner because I want to create a custom dropDownList that contains multiple rows and populate it from my database. I have accomplished this task quite nicely, but on my layout activity, the spinner shows the selected rows and I would like it to have a separate layout so it shows only the first line of the selected row. How can I accomplish this?

String fields[] = {"name", "lovibond", "gravity"};
    nameAdapter = new GrainSpinnerAdapter(this, R.layout.grain_spinner_row, data, fields, new int[] { R.id.GrainSpinnerName, R.id.GrainSpinnerLovibond, R.id.GrainSpinnerGravity });
    nameSpinner.setAdapter(nameAdapter);

Here is my SimpleCursorAdapter code:

public class GrainSpinnerAdapter extends SimpleCursorAdapter {

    private Context myContext;

    public GrainSpinnerAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);
        myContext = context;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        super.bindView(view, context, cursor);

        int nameColumn = cursor.getColumnIndex("name");
        String getName = cursor.getString(nameColumn);
        TextView name = (TextView)view.findViewById(R.id.GrainSpinnerName);
        name.setText(getName);

    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        super.newView(context, cursor, parent);
        View view = View.inflate(context, R.layout.grain_spinner, null);
        return view;
    }

    @Override
    public View newDropDownView(Context context, Cursor cursor, ViewGroup parent) {
        super.newDropDownView(context, cursor, parent);

        View view = View.inflate(context, R.layout.grain_spinner_row, null);
        int nameColumn = cursor.getColumnIndex("name");
        String getName = cursor.getString(nameColumn);
        TextView name = (TextView)view.findViewById(R.id.GrainSpinnerName);
        name.setText(getName);

        int loviColumn = cursor.getColumnIndex("lovibond");
        String getLovi = cursor.getString(loviColumn);
        TextView lovi = (TextView)view.findViewById(R.id.GrainSpinnerLovibond);
        lovi.setText(getLovi);

        int gravityColumn = cursor.getColumnIndex("gravity");
        String getGravity = cursor.getString(gravityColumn);
        TextView gravity = (TextView)view.findViewById(R.id.GrainSpinnerGravity);
        gravity.setText(getGravity);

        return view;
    }

回答1:


I think what you are asking is the getDropDownView() method of BaseAdapter class.

In your newView method do what you've done and provide a layout for single row displays.

Then implement newDropDownView to inflate another layout that provides for multiple lines. The drop down views are used to create rows in the popup menu that is provided when a user selects the dropdown.



来源:https://stackoverflow.com/questions/6835032/android-spinner-custom-adapter-display

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!