根据不同微调器的位置更改微调器列表

时间:2015-12-21 11:53:01

标签: java android drop-down-menu spinner adapter

在我的项目中,我有一个微调器来选择单位类型(长度,质量等),然后根据用户选择的内容选择两个旋转器。因此,如果用户从第一个微调器中选择Mass作为它们的单位,则第二个和第三个微调器应该更改它们显示的列表,显示克数,磅数等。

我正在尝试使用conversionAdapter的onItemSelectedListener()来切换其他两个微调器显示的列表。我的尝试都没有奏效,因为这个内部类无法访问主要活动中的微调器。

public class MainActivity extends AppCompatActivity {

private ImageView equalSign;
private Spinner conversionSpinner, unitOneSpinner, unitTwoSpinner;
private EditText unitOneEditText, unitTwoEditText;
private List selectedList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    equalSign = (ImageView)findViewById(R.id.equal_sign);
    equalSign.setImageResource(R.drawable.equal_sign_black_2);

    unitOneEditText = (EditText)findViewById(R.id.unit_one);
    unitTwoEditText = (EditText)findViewById(R.id.unit_two);

    conversionSpinner = (Spinner)findViewById(R.id.conversion_spinner);

    String[] conversion_list = {"Length", "Mass", "Time", "Temperature", "Volume"};
    String[] length_list = {"Kilometer", "Meter", "Centimeter", "Millimeter", "Micrometer", "Nanometer", "Mile", "Yard", "Inch"};
    String[] mass_list = {"Metric Ton", "Kilogram", "Gram", "Milligram", "Microgram", "Pound", "Ounce"};
    String[] time_list = {"Nanosecond", "Microsecond", "Millisecond", "Second", "Minute", "Hour", "Day", "Year", "Decade", "Century"};
    String[] temp_list = {"Fahrenheit", "Celsius", "Kelvin"};
    String[] volume_list = {"US Liquid Gallon", "US Liquid Quart", "US Liquid Pint", "US Legal Cup", "US Fluid Ounce", "US Tablespoon", "US Teaspoon", "Liter", "Milliliter"};

    List<String>conversion_types = new ArrayList<String>();
    List<String>length = new ArrayList<String>();
    List<String>mass = new ArrayList<String>();
    List<String>time = new ArrayList<String>();
    List<String>temp = new ArrayList<String>();
    List<String>volume = new ArrayList<String>();

    conversion_types = updateList(conversion_types, conversion_list);
    length = updateList(length, length_list);
    mass = updateList(mass, mass_list);
    time = updateList(time, time_list);
    temp = updateList(temp, temp_list);
    volume = updateList(volume, volume_list);

    selectedList = length;

    ArrayAdapter<String> conversionAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_dropdown_item, conversion_types);
    conversionSpinner.setAdapter(conversionAdapter);
    conversionSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String unitType = (String)parent.getItemAtPosition(position);
            switch(unitType){
                case "length":
                    Toast.makeText(getApplicationContext(), "Length", Toast.LENGTH_SHORT).show();
                    break;
                case "Mass":
                    Toast.makeText(getApplicationContext(), "Mass", Toast.LENGTH_SHORT).show();
                    break;
                case "Time":
                    Toast.makeText(getApplicationContext(), "Time", Toast.LENGTH_SHORT).show();
                    break;
                case "Temperature":
                    Toast.makeText(getApplicationContext(), "Temperature", Toast.LENGTH_SHORT).show();
                    break;
                case "Volume":
                    Toast.makeText(getApplicationContext(), "Volume", Toast.LENGTH_SHORT).show();
                    break;
            }

        }

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

        }
    });

    unitOneSpinner = (Spinner)findViewById(R.id.unit_one_spinner);
    ArrayAdapter<String> unitOneAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_dropdown_item, selectedList);
    unitOneSpinner.setAdapter(unitOneAdapter);
    unitOneSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        }

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

        }
    });

    unitTwoSpinner = (Spinner)findViewById(R.id.unit_two_spinner);
    ArrayAdapter<String> unitTwoAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_dropdown_item, selectedList);
    unitTwoSpinner.setAdapter(unitTwoAdapter);
    unitTwoSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        }

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

        }
    });

    unitOneAdapter.clear();
    unitOneAdapter = updateList(unitOneAdapter, mass_list);
    unitOneAdapter.notifyDataSetChanged();
}

public List<String> updateList(List<String> myArray, String[] list){
    for(int i = 0; i < list.length; i++){
        myArray.add(list[i]);
    }
    return myArray;
}

//Overloaded method used for ArrayAdapters
public ArrayAdapter<String> updateList(ArrayAdapter<String> myArray, String[] list){
    for(int i = 0; i < list.length; i++){
        myArray.add(list[i]);
    }
    return myArray;
}

0 个答案:

没有答案