Android - 更新微调器选择的列表视图

时间:2013-11-07 10:09:44

标签: android listview android-spinner

我正在尝试更新列表视图,当我在spinner中选择一个项目时,代码可以工作,但是没有生成输出。我无法弄清楚这个bug。每次点击旋转器中的项目我想查看不同的列表。

Spinner spinner;
String path[] = {"Laptops","DesktopPC","Tablets","Add-Ons","Gaming"};
String Laptops[] = {"Dell","Hp","Apple"};
ListView lstView;


lstView = (ListView) findViewById(R.id.listView1);

ArrayAdapter<String> adapter = new ArrayAdapter<String> (Category.this, android.R.layout.simple_spinner_item, path );
spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setAdapter(adapter);
spinner.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1,
            int arg2, long arg3) {
        // TODO Auto-generated method stub
        ArrayAdapter<String> lstAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, Laptops);
        lstView.setAdapter(lstAdapter);
        //lstView.refreshDrawableState();

    }
});

4 个答案:

答案 0 :(得分:2)

使用OnItemSelectedListener并使用ArrayList<String>。对适配器使用相同的。将项添加到arraylist并在适配器上调用notifyDataSetChanged以刷新lsitview。

此外,无需在微调器选择侦听器中初始化适配器。

根据您的要求修改以下内容

 Spinner spinner;
        String path[] = {"Laptops","DesktopPC","Tablets","Add-Ons","Gaming"};
        ArrayList<String> list = new ArrayList<String>();
        String Laptops[] = {"Dell","Hp","Apple"};
        ListView lstView;
        ArrayAdapter<String> adapter,listadapter;
        public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.activity_main);
                lstView = (ListView) findViewById(R.id.listView1);
                for(int i=0;i<Laptops.length;i++)
                {
                    list.add(Laptops[i]);
                }
                adapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item,path);
                listadapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item,list);
                lstView.setAdapter(listadapter);
                spinner = (Spinner) findViewById(R.id.spinner1);
                spinner.setAdapter(adapter);
                spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

                    @Override
                    public void onItemSelected(AdapterView<?> arg0, View arg1,
                            int arg2, long arg3) {
                        // TODO Auto-generated method stub
                        String s = spinner.getItemAtPosition(arg2).toString();
                        list.add(s);
                        listadapter.notifyDataSetChanged();
                    }

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

                    }
                });

            }

答案 1 :(得分:0)

使用通知已更改数据集而不是设置新适配器。

// Inside Event callback
 path = Laptops; // This is how you change data set
 lstView.notifyDataSetChanged();

答案 2 :(得分:-1)

使用微调器onitemSelectedListener代替onItemClickListener

尝试以下方法:

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
        // your code here


  ArrayAdapter<String> lstAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, Laptops);
                    lstView.setAdapter(lstAdapter);
                    lstView.notifyDataSetChanged();
        }

    @Override
    public void onNothingSelected(AdapterView<?> parentView) {
        // your code here
    }

});

答案 3 :(得分:-1)

将notifyDataSetChanged()添加到适配器,如下所示....

spinner.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                ArrayAdapter<String> lstAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, Laptops);
                lstView.setAdapter(lstAdapter);
                lstAdapter.notifyDataSetChanged();
                //lstView.refreshDrawableState();

            }
        });