Android Spinner:自定义适配器显示

发布于 2024-11-26 14:22:48 字数 2329 浏览 1 评论 0 原文

我在微调器上使用 SimpleCursorAdapter,因为我想创建一个包含多行的自定义 dropDownList 并从我的数据库填充它。我已经很好地完成了这项任务,但在我的布局活动中,微调器显示所选行,我希望它有一个单独的布局,因此它仅显示所选行的第一行。我怎样才能做到这一点?

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

这是我的 SimpleCursorAdapter 代码:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好菇凉咱不稀罕他 2024-12-03 14:22:48

我认为您要问的是 getDropDownView() 方法>BaseAdapter 类。

在您的 newView 方法中执行您已完成的操作并提供单行显示的布局。

然后实现 newDropDownView 来扩充另一个提供多行的布局。
下拉视图用于在用户选择下拉菜单时提供的弹出菜单中创建行。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文