0

我的活动中有微调器,它从 JSON 数据中设置项目,当我从微调器中选择任何项目并设置到微调器中时,我已经正确地完成了所有这些事情。但是当我想从微调器中删除选定的项目时,我得到了异常:

E/AndroidRuntime(1022): java.lang.UnsupportedOperationException"

E/AndroidRuntime(1022):atandroid.widget.ArrayAdapter.remove(ArrayAdapter.java:212)

在代码行

E/AndroidRuntime(1022):在 com.spinnerdemo.SpinDemo$1.onItemSelected(SpinDemo.java:102)

这是我的代码:

public class SpinDemo extends Activity {

    private static String strUrl = "http://192.168.1.61/jyoti/android_app/all_questions.php";
    private static String TAG_ID = "id";
    private static String TAG_CODE = "q_prefix";
    private static String TAG_CODE_ARR = "Questions";

    JSONArray jsonArray = null;
    Spinner codeSpinner, spinner2;
    EditText edTextSpinnerItem;
    String[] items;

    String strEdtext;
    String strid , strcode ;
    ArrayList<String> codeList;
    public  ArrayAdapter<String> adapter ;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        codeList = new ArrayList<String>();
        codeSpinner = (Spinner) findViewById(R.id.spinner2);

        JSONParser jParser = new JSONParser();
        JSONObject json = jParser.getJSONFromUrl(strUrl);
        try
        {
            jsonArray = json.getJSONArray(TAG_CODE_ARR);

             // looping through All Contacts
             final String[] items = new String[jsonArray.length()];

             for(int i = 0; i < jsonArray.length(); i++)
             {
                 JSONObject c = jsonArray.getJSONObject(i);

                // Storing each json item in variable

                    strid = c.getString(TAG_ID);
                    strcode = c.getString(TAG_CODE);
                    items[i] = c.getString(TAG_CODE);
                    System.out.println("Hello events " + items);        

                    adapter = new ArrayAdapter<String>(this,
                            android.R.layout.simple_spinner_item,items);
                    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    codeSpinner.setAdapter(adapter);

             }
        }

        catch (JSONException e) {
            e.printStackTrace();
        }


        codeSpinner.setOnItemSelectedListener(new OnItemSelectedListener()
        {


            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int pos, long arg3)
            {
                // TODO Auto-generated method stub

                String anyvariable=String.valueOf(codeSpinner.getSelectedItem());
                System.out.println("anyvariable = " + anyvariable);
                edTextSpinnerItem=(EditText)findViewById(R.id.editText_SpinnerItem);
                edTextSpinnerItem.setText(anyvariable);
                System.out.println("edTextSpinnerItem " + edTextSpinnerItem);


                String t = adapter.getItem(pos);
                System.out.println("Get The Item Position From Adapter = " + t);




                adapter.remove(t);
                adapter.notifyDataSetChanged();
                codeSpinner.setAdapter(adapter);
                //mySpinner.setAdapter(m_adapterForSpinner);

                //adapter.remove((String)codeSpinner.getSelectedItem());
                //adapter.notifyDataSetChanged();
                //System.out.println("Item is Removed From The Spinner Drop Dwon List");



            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) 
            {
                // TODO Auto-generated method stub

            }
        });

    }



}
4

2 回答 2

0

我认为问题是由删除实际选择的元素引起的尝试对项目使用列表而不是数组:http: //developer.android.com/reference/android/widget/ArrayAdapter.html#ArrayAdapter (android.content.Context,%20int,%20int,%20java.util.List )

然后在on item中选择使用

items.remove(t);

代替:

adapter.remove(t);

通过此更改,您可以从 ArrayList(List) 而不是适配器中删除该项目。适配器 notifyDataSetChanged 捕获 List 上的更改并刷新微调器

于 2013-11-12T11:48:45.933 回答
0

尝试这个..

1)循环spinner setadapter后给予for

2)如果您需要删除任何项目。你需要使用ArrayList它是简单的方法。

我在一些更改后发布了代码。

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        codeList = new ArrayList<String>();
        codeSpinner = (Spinner) findViewById(R.id.spinner2);

        JSONParser jParser = new JSONParser();
        JSONObject json = jParser.getJSONFromUrl(strUrl);
        try
        {
            jsonArray = json.getJSONArray(TAG_CODE_ARR);

             // looping through All Contacts
             final String[] items = new String[jsonArray.length()];

             for(int i = 0; i < jsonArray.length(); i++)
             {
                 JSONObject c = jsonArray.getJSONObject(i);

                // Storing each json item in variable

                    strid = c.getString(TAG_ID);
                    strcode = c.getString(TAG_CODE);
                    items[i] = c.getString(TAG_CODE);
                    System.out.println("Hello events " + items);  
                    codeList.add(strcode);

             }

adapter = new ArrayAdapter<String>(this,
                            android.R.layout.simple_spinner_item,codeList);
                    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    codeSpinner.setAdapter(adapter);
        }

        catch (JSONException e) {
            e.printStackTrace();
        }


        codeSpinner.setOnItemSelectedListener(new OnItemSelectedListener()
        {


            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int pos, long arg3)
            {
                // TODO Auto-generated method stub

                String anyvariable=String.valueOf(codeSpinner.getSelectedItem());
                System.out.println("anyvariable = " + anyvariable);
                edTextSpinnerItem=(EditText)findViewById(R.id.editText_SpinnerItem);
                edTextSpinnerItem.setText(anyvariable);
                System.out.println("edTextSpinnerItem " + edTextSpinnerItem);


                String t = adapter.getItem(pos);
                System.out.println("Get The Item Position From Adapter = " + t);



                Object t2 = adapter.getItem(pos);
        Log.v("t2", ""+t2);
        codeList.remove(t2);
        adapter.notifyDataSetChanged();


            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) 
            {
                // TODO Auto-generated method stub

            }
        });

    }
于 2013-11-12T12:12:22.943 回答