2011-05-20 154 views
0

我正在构建一个应用程序来从餐厅订购食物。当用户选择一个项目时,我点击我们的服务器以检索包含该项目选项的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 [email protected] 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; 
    } 

任何帮助是极大的赞赏。谢谢。

+0

LocalActivityRecord是你的Activity类吗? – djg 2011-05-20 16:02:39

+0

其实我扩展ListActivity:MenuAttributes扩展ListActivity 我应该使用LocalActivityManager? – 2011-05-20 16:09:54

回答

0

我不能得到这个工作,所以我把它切换到单选按钮。然后我遇到了另一个问题,当滚动列表时,值从edittext字段中消失。

在做了一些研究之后,我发现把ListView放在ListView中是一个非常糟糕的主意,因为它在android中有很多bug。看到这个前一篇文章:http://bit.ly/mhvL1D

我切换到一个ScrollView嵌套LinearLayouts包含各种表单控件。

神奇的是,我所有的问题都消失了。旋转工具现在工作得很好,EditText不再在滚动中消失。

我只是想了解整个过程是错误的。

经验教训:不要使用ListView作为窗体控件。

0

您能解释一下如何使用MenuAttributesLocalActivityRecord

它使用后者的上下文([email protected]),而可能需要提供MenuAttributes对象的上下文。

为了简化,我会在MenuAttributes类的上层Context context;场(你可以将其设置为在onCreate()this.context = this;),并使用该字段为:

ArrayAdapter<String> adapter = new ArrayAdapter<String> (context, android.R.layout.simple_spinner_item, options);