无法解析setAdapter和setOnItemSelectedListener

时间:2018-07-20 14:16:12

标签: android

我无法解析setAdapter和setOnItemSelectedListener。谁能解决这个问题。

无法解析setAdapter和setOnItemSelectedListener

public class Spinner extends AppCompatActivity{

private ArrayList<CountryItem> mCountryList;
private CountryAdapter mAdapter;

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

    initList();

    Spinner spinnerCountries = (Spinner)findViewById(R.id.spinner_countries);

    mAdapter = new CountryAdapter(this, mCountryList);
    spinnerCountries.setAdapter(mAdapter);

    spinnerCountries.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            CountryItem clickedItem = (CountryItem) parent.getItemAtPosition(position);
            String clickedCountryName = clickedItem.getCountryName();
            Toast.makeText(Spinner.this, clickedCountryName + " selected", Toast.LENGTH_SHORT).show();
        }

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

        }
    });
}

private void initList() {
    mCountryList = new ArrayList<>();
    mCountryList.add(new CountryItem("India", R.drawable.app_logo));
    mCountryList.add(new CountryItem("China", R.drawable.app_logo));
    mCountryList.add(new CountryItem("USA", R.drawable.app_logo));
    mCountryList.add(new CountryItem("Germany", R.drawable.app_logo));
}

2 个答案:

答案 0 :(得分:0)

它与窗口小部件具有相同的类名'Spinner'-Spinner可能是造成此问题的原因,请确保您已正确导入窗口小部件Spinner,例如-:

import android.widget.Spinner;

答案 1 :(得分:0)

只需将您自己的班级名称从Spinner更改为SpinnerActivity。您的班级和android.widget.Spinner

之间存在一个简单的冲突