android中自定义Spinner错误

时间:2012-10-19 06:01:20

标签: android

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

   // 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;
            }
        }

2 个答案:

答案 0 :(得分:0)

//声明选中的项目全球

  String type;

然后make this.type = typename;在loadspinner中

答案 1 :(得分: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);

...
}