0

我有 2 个旋转器。我正在从数据库中加载微调器数据。当我在第一个微调器中选择任何项目时,根据该项目,第二个微调器应该加载相应的数据。例如,如果在第一个微调器中选择蔬菜,那么只有蔬菜名称应该排在第二位,如果在第一个选择水果,然后水果名称应该排在第二个。如何将第一个微调器中的选定项目传递给我的自定义微调器类?我试图通过该项目,但它让我在第二个微调器的每一行都空白。这是我的代码

   // Spinner click listener
            spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> paramAdapterView,
                        View paramView, int paramInt, long paramLong) {
                    // TODO Auto-generated method stub
                    label = paramAdapterView.getItemAtPosition(paramInt)
                            .toString();
                    deviceID = paramInt;
                    loadSpinnerData1(label);

                }
       private void loadSpinnerData1(String type) {
        // database handler
        DatabaseHandler db = new DatabaseHandler(getApplicationContext());
                 //getting the spinner item depending on what item selected in 1st spinner
                List<String> manuft = db.getAllManuft(type);
        spinner1.setAdapter(new MyAdapter1(OptionsActivity.this, R.layout.row, manuft,type));
    }

我的自定义微调器类

        public class MyAdapter1 extends ArrayAdapter<String>{

        public MyAdapter1(Context context, int textViewResourceId, List<String> manuft, String type) {
            super(context, textViewResourceId, manuft);
        }

        @Override
        public View getDropDownView(int position, View convertView,ViewGroup parent) {
            return getCustomView(position, convertView, parent, label);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return getCustomView(position, convertView, parent, label);
        }

        public View getCustomView(int position, View convertView, ViewGroup parent, String type) {

            DatabaseHandler db = new DatabaseHandler(getApplicationContext());

            List<String> manuft = db.getAllManuft(type);

            LayoutInflater inflater=getLayoutInflater();
            View row=inflater.inflate(R.layout.row, parent, false);
            TextView label=(TextView)row.findViewById(R.id.device);

            for(int i=0;i<manuft.size();i++){
                label.setText(manuft.get(position));
            }


            return row;
            }
        }
4

2 回答 2

0

//声明全局选中的项目

  String type;

然后使 this.type=typename; 在负载旋转器中

于 2012-10-19T07:04:32.107 回答
0

尝试这样的事情:

  spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, android.view.View v, int position, long id) {
                    // this method will update your second spinner
            updateSecondSpinner(id);


        }

这里更新第二个微调器的方法:

    private void updateSecondSpinner(long id) {

                String[] args = new String[] { String.valueOf(id) };
                cur2 = db.rawQuery("select IID AS _id, TXNAME from TABLE WHERE IID= ?", args);

                SimpleCursorAdapter adapter_2 = new SimpleCursorAdapter(AccidentNewActivity.this, android.R.layout.simple_spinner_item, cur2,
                    new String[] { "TXNAME" }, new int[] { android.R.id.text1 });

                adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                spinner_modelo.setAdapter(adapter2);

...
}
于 2012-10-19T07:25:02.947 回答