[英]generate spinner from custom list adapter
我正在构建一个用于从餐厅订购食物的应用程序。 当用户选择一个项目时,我进入我们的服务器以检索一个包含该项目选项的JSON包。 (例如:面包:白,小麦)。
我想要做的是使用自定义列表适配器来生成用户在将商品添加到购物车之前需要选择的所有选项的列表。 这工作得很好,除了当我单击微调器时,我得到:
05-20 15:12:53.602: ERROR/AndroidRuntime(696): FATAL EXCEPTION: main
05-20 15:12:53.602: ERROR/AndroidRuntime(696): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@44e933d8 is not valid; is your activity running?
05-20 15:12:53.602: ERROR/AndroidRuntime(696):at android.view.ViewRoot.setView(ViewRoot.java:505)
05-20 15:12:53.602: ERROR/AndroidRuntime(696):at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
05-20 15:12:53.602: ERROR/AndroidRuntime(696):at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
05-20 15:12:53.602: ERROR/AndroidRuntime(696):at android.view.Window$LocalWindowManager.addView(Window.java:424)
等等...
这与将正确的上下文传递给我用于微调器的阵列适配器有关,但是我觉得我已经尝试了所有可能的选项。 罪魁祸首是:
ArrayAdapter<String> adapter = new ArrayAdapter<String> (parent.getContext(), android.R.layout.simple_spinner_item, options); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); attrSpinner.setAdapter(adapter);
这是整个自定义列表适配器,因此您可以看到我在哪里使用此代码块:
public class ItemListAdapter extends BaseAdapter {
public ItemListAdapter(Context context) {
Context mContext = context;
}
public int getCount() {
return attributes.length();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View attr_item = li.inflate(R.layout.menu_attributes, null);
TextView attrName = (TextView) attr_item.findViewById(R.id.attr_name);
CheckBox attrCheck = (CheckBox) attr_item.findViewById(R.id.attr_checkbox);
EditText attrText = (EditText) attr_item.findViewById(R.id.attr_edittext);
Spinner attrSpinner = (Spinner) attr_item.findViewById(R.id.attr_spinner);
try {
String attrID = attributes.getJSONObject(position).getString("AttrID");
String type = attributes.getJSONObject(position).getString("Type");
String itemLabel = attributes.getJSONObject(position).getString("ItemLabel");
JSONArray values = attributes.getJSONObject(position).getJSONArray("Values");
if (type.equals("select")) {
attrCheck.setVisibility(View.GONE);
attrText.setVisibility(View.GONE);
ArrayList<String> options = new ArrayList<String>();
for (int i=0;i<values.length();i++) {
options.add(values.getJSONObject(i).getString("Name"));
Log.i("value options", values.getJSONObject(i).getString("Name"));
}
//HERE IS THE PROBLEM
ArrayAdapter<String> adapter = new ArrayAdapter<String> (parent.getContext(), android.R.layout.simple_spinner_item, options);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
attrSpinner.setAdapter(adapter);
}
else if (type.equals("checkbox")){
attrSpinner.setVisibility(View.GONE);
attrText.setVisibility(View.GONE);
}
else if (type.equals("textarea")){
attrSpinner.setVisibility(View.GONE);
attrCheck.setVisibility(View.GONE);
}
else if (type.equals("text")){
attrSpinner.setVisibility(View.GONE);
attrCheck.setVisibility(View.GONE);
}
attrName.setText(itemLabel);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return attr_item;
}
任何帮助是极大的赞赏。 谢谢。
您能解释一下如何使用MenuAttributes和LocalActivityRecord吗?
它使用的是Context( android.app.LocalActivityManager$LocalActivityRecord@44e933d8
),而您可能需要提供MenuAttributes对象的Context。
为简化起见,我将使用上层Context context;
MenuAttributes
类中的字段(可以在onCreate()
中将其设置为this.context = this;
),并将该字段用作:
ArrayAdapter<String> adapter = new ArrayAdapter<String> (context, android.R.layout.simple_spinner_item, options);
我无法使它正常工作,所以我将其切换为单选按钮。 然后,我遇到了另一个问题,即滚动列表时,值从edittext字段中消失了。
经过一些研究,我发现将表单控件放入ListView是一个非常糟糕的主意,因为它在android中存在许多错误。 参见上一篇文章: http : //bit.ly/mhvL1D
我切换到了带有嵌套的LinearLayouts并包含各种表单控件的ScrollView。
神奇地,我所有的问题都消失了。 微调器现在可以很好地工作,并且EditText不再在滚动中消失。
我只是将整个过程弄错了。
经验教训:不要将ListView用于表单控件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.