单击其他一个时,将一个微调器设置为空值

时间:2018-04-15 15:21:10

标签: android onclick spinner onclicklistener

我有两个微调器,第一个显示传输线,第二个显示在第一个微调器上选择的线的工作站,问题是当我点击时,我想将第二个微调器设置为空值第一个,但我不能这样做。

private ArrayAdapter<String> arrayAdapter;
private MaterialBetterSpinner spinner;
private ArrayAdapter<String> arrayAdapter2;
private MaterialBetterSpinner spinner2;

我在第一个微调器的onItemClick()方法上尝试了一些选项,但它不能正常工作:

spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
     @Override
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        @Override
        protected void onPreExecute() {
            // I'm trying all the options in this method
        }

        @Override
        protected String[] doInBackground(String... strings) {

        }

        @Override
        protected void onPostExecute(String[] result) {

        }
     }
});

选项1:

arrayAdapter2.clear();
arrayAdapter2.notifyDataSetChanged();

选项2:

spinner2.setAdapter(null);

选项3:

ArrayList<String> list = new ArrayList<>();
list.add("");
spinner2.setSelection(list.size()-1);

选项4:

arrayAdapter2.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
spinner2.setAdapter(arrayAdapter2); 
spinner2.setSelection(arrayAdapter2.getCount());
spinner2.setOnItemSelectedListener(this);
arrayAdapter2.notifyDataSetChanged();

总而言之,有一种方法可以根据其他值的值来改变微调器的值吗?或者只是一种将微调器的值更改为空值的方法?

任何帮助都会被贬低!

2 个答案:

答案 0 :(得分:0)

使用OnItemClickListener代替 spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { if(pos==0) // you can change condition as per your requirement { arrayAdapter2.clear(); arrayAdapter2.notifyDataSetChanged(); } } public void onNothingSelected(AdapterView<?> parent) { } }); ,如:

Select T2.ID,T1.Name from ShortNames T1
Inner Join LongNames T2
On T2.Name Like Concat('% ', T1.Name)
or T2.Name like Concat(T1.Name,' %')
Group by T2.ID 
Order by Max(Char_Length(T1.Name))

答案 1 :(得分:0)

完整的代码:

public class FillTransportPlaceActivity extends AppCompatActivity  {

    private ArrayAdapter<String> arrayAdapter;
    private MaterialBetterSpinner spinner;
    private ArrayAdapter<String> arrayAdapter2;
    private MaterialBetterSpinner spinner2;
    private ArrayList<String> list = new ArrayList<>();

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

        arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
        spinner = findViewById(R.id.listLines);
        spinner.setAdapter(arrayAdapter);

        arrayAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
        spinner2 = findViewById(R.id.listStations);
        spinner2.setAdapter(arrayAdapter2);

        SharedPreferences sp = getApplicationContext().getSharedPreferences("transportButton", 0);
        boolean metro = sp.getBoolean("metro", false);
        boolean bus = sp.getBoolean("bus", false);

        spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(parent.getItemAtPosition(position).toString() != null) {
                    String choice = parent.getItemAtPosition(position).toString();

                    if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB)
                        new stationsDB().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, choice);
                    else
                        new stationsDB().execute(choice);
                }
            }
        });
    }

    private class stationsDB extends AsyncTask<String, String[], String[]> {
        @Override
        protected String[] doInBackground(String... strings) {
            return DB_transportPlace.getStations(strings[0]);
        }

        @Override
        protected void onPostExecute(String[] result) {
            updateAdapter2(result);
        }
    }

    public void updateAdapter(String[] result) {
        arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, result);
        spinner.setAdapter(arrayAdapter);
    }

    public void updateAdapter2(String[] result) {
        arrayAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, result);
        spinner2.setAdapter(arrayAdapter2);
    }

正如我所说,我尝试使用setOnItemSelectedListener代替OnItemClickListener并且它不起作用,只在onCreate方法中执行一次,我只想更改值当我更改spinner1的值时,spinner2将被清空。