Java - 通过微调器更改微调器的值

时间:2016-08-18 10:04:12

标签: java android

我试图让一个微调器(2)从另一个微调器(1)中改变它的内容。

我目前在第二个微调器的数组适配器中出现错误 “错误:(28,57)错误:无法推断ArrayAdapter的类型参数<>”

这是我的代码:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class DetailsForm extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.details_form);

        final Spinner carBrand = (Spinner) findViewById(R.id.carBrand);
        String[] items = new String[]{"Honda", "Nissan", "Toyota"};
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, items);
        carBrand.setAdapter(adapter);
        carBrand.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
            String carBrand = parent.getSelectedItem().toString();
                if (carBrand.equals("Nissan")) {
                    final Spinner model = (Spinner) findViewById(R.id.model);
                    String[] models = new String[]{"S14", "R34", "240SX"};
                    ArrayAdapter<String> modeladapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, models);
                    model.setAdapter(modeladapter);

                } else if (carBrand.equals("Honda")) {
                    Spinner model = (Spinner) findViewById(R.id.model);
                    String[] models = new String[]{"Civic", "NSX", "Accord"};
                    ArrayAdapter<String> modeladapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, models);
                    model.setAdapter(modeladapter);
                } else if (carBrand.equals("Toyota")) {
                    Spinner model = (Spinner) findViewById(R.id.model);
                    String[] models = new String[]{"Supra", "AE86", "GT86"};
                    ArrayAdapter<String> modeladapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, models);
                    model.setAdapter(modeladapter);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

更改您的代码

发件人:

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
            String carBrand = parent.getSelectedItem().toString();
            if (carBrand.equals("Nissan")) {
                final Spinner model = (Spinner) findViewById(R.id.model);
                String[] models = new String[]{"S14", "R34", "240SX"};
                ArrayAdapter<String> modeladapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, models);
                model.setAdapter(modeladapter);

            } else if (carBrand.equals("Honda")) {
                Spinner model = (Spinner) findViewById(R.id.model);
                String[] models = new String[]{"Civic", "NSX", "Accord"};
                ArrayAdapter<String> modeladapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, models);
                model.setAdapter(modeladapter);
            } else if (carBrand.equals("Toyota")) {
                Spinner model = (Spinner) findViewById(R.id.model);
                String[] models = new String[]{"Supra", "AE86", "GT86"};
                ArrayAdapter<String> modeladapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, models);
                model.setAdapter(modeladapter);
            }
        }

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
            String carBrand = parent.getSelectedItem().toString();
            if (carBrand.equals("Nissan")) {
                final Spinner model = (Spinner) findViewById(R.id.model);
                String[] models = new String[]{"S14", "R34", "240SX"};
                ArrayAdapter<String> modeladapter = new ArrayAdapter<>(YourActivity.this, android.R.layout.simple_spinner_dropdown_item, models);
                model.setAdapter(modeladapter);

            } else if (carBrand.equals("Honda")) {
                Spinner model = (Spinner) findViewById(R.id.model);
                String[] models = new String[]{"Civic", "NSX", "Accord"};
                ArrayAdapter<String> modeladapter = new ArrayAdapter<>(YourActivity.this, android.R.layout.simple_spinner_dropdown_item, models);
                model.setAdapter(modeladapter);
            } else if (carBrand.equals("Toyota")) {
                Spinner model = (Spinner) findViewById(R.id.model);
                String[] models = new String[]{"Supra", "AE86", "GT86"};
                ArrayAdapter<String> modeladapter = new ArrayAdapter<>(YourActivity.this, android.R.layout.simple_spinner_dropdown_item, models);
                model.setAdapter(modeladapter);
            }
        }

YourActivity是您的activity班级名称。此更改是必要的,因为在OnItemSelectedListenerthis内表示与context类型不同的侦听器对象,您需要contextactivity的对象引用调用ArrayAdapter构造函数。这个解决方案应该适合你。

答案 1 :(得分:0)

您必须传递当前活动的上下文。在此行中将this更改为DetailsForm.this:

ArrayAdapter<String> modeladapter = new ArrayAdapter<>(DetailsForm.this, android.R.layout.simple_spinner_dropdown_item, models);