簡體   English   中英

從自定義列表適配器生成微調器

[英]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;
    }

任何幫助是極大的贊賞。 謝謝。

您能解釋一下如何使用MenuAttributesLocalActivityRecord嗎?

它使用的是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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM