0

我正在尝试更新列表视图,当我在微调器中选择一个项目时,代码有效,但没有生成输出。我无法找出错误。每次单击微调器中的项目时,我都想查看不同的列表。

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

4 回答 4

2

Use OnItemSelectedListener and use a ArrayList<String>. Use the same for your adapter. Add items to arraylist and call notifyDataSetChanged on your adapter to refresh lsitview.

Also there is no need to initialize your adapter in the spinner selection listener.

Modify the below according to your requirement

 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

                    }
                });

            }
于 2013-11-07T10:37:17.150 回答
0

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

// Inside Event callback
 path = Laptops; // This is how you change data set
 lstView.notifyDataSetChanged();
于 2013-11-07T10:16:38.717 回答
-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
    }

});
于 2013-11-07T10:12:59.190 回答
-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();

            }
        });
于 2013-11-07T10:13:26.090 回答