Android Spinner:自定义适配器显示
我在微调器上使用 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您要问的是 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.