如何在Android中的微调器中加载数据?

时间:2017-09-08 10:04:22

标签: java android

我试图将数据加载到我的微调器中。但它并不好用。我不知道原因。我尝试过只使用菱形运算符(<>)和Stack Overflow中的其他方法。

Spinner spinner = (Spinner) findViewById(R.id.spinner3);

            spinner.setOnItemSelectedListener((AdapterView.OnItemSelectedListener) this);
                    List<String> view = new ArrayList<String>();
            view.add("Grid view");
            view.add("chart view");



            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,View);


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


            spinner.setAdapter(dataAdapter);

5 个答案:

答案 0 :(得分:2)

更改此代码

 ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,view);//insted of View use view

答案 1 :(得分:1)

使用view代替View

  ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,view);

答案 2 :(得分:0)

代码的另一个可行示例

Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    }

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

    }
});
spinner.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item,
        Arrays.asList("Grid View", "chart view")));

答案 3 :(得分:0)

喷丝=(微调)findViewById(R.id.spinner);

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, R.layout.spinner_items,yourArrayList);
spinner.setAdapter(dataAdapter);
spinner.setOnItemSelectedListener(this);

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    switch (parent.getId()){
        case R.id.spinner:
            String str_type=parent.getItemAtPosition(position).toString();
            TypeSelected = position;
            break;
    }
}

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

答案 4 :(得分:0)

String[] data= {"Grid view","chart view"};
Spinner spinner = (Spinner) findViewById(R.id.spinner3);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item,data);

spinner .setAdapter(adapter);
adapter.notifyDataSetChanged();

然后你可以在

上获得选定的项目位置
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {    }