我正在尝试转换OnItemSelected方法的父对象来检索所选项目的对象但是有错误
TextView output = null;
CustomAdapter adapter;
MainActivity activity = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activity = this;
Spinner SpinnerExample = (Spinner)findViewById(R.id.spinner);
output = (TextView)findViewById(R.id.company);
// Set data in arraylist
setListData();
// Resources passed to adapter to get image
Resources res = getResources();
// Create custom adapter object ( see below CustomAdapter.java )
adapter = new CustomAdapter(activity, R.layout.spinner_rows, CustomListViewValuesArr,res);
// Set adapter to spinner
SpinnerExample.setAdapter(adapter);
// Listener called when spinner item selected
SpinnerExample.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View v, int position, long id) {
// your code here
SpinnerModel obj = (SpinnerModel) parentView;
// Get selected row data to show on screen
String Company = ((TextView) v.findViewById(R.id.company)).getText().toString();
String CompanyUrl = ((TextView) v.findViewById(R.id.sub)).getText().toString();
String OutputMsg = "Selected Company : \n\n"+Company+"\n"+CompanyUrl;
Toast.makeText(
getApplicationContext(),OutputMsg, Toast.LENGTH_LONG).show();
Toast.makeText(
getApplicationContext(),xxx.getCompanyName(), Toast.LENGTH_LONG).show();
}
public class CustomAdapter extends ArrayAdapter<SpinnerModel>{
private Activity activity;
private ArrayList data;
public Resources res;
SpinnerModel tempValues=null;
LayoutInflater inflater;
/************* CustomAdapter Constructor *****************/
public CustomAdapter(
MainActivity activitySpinner,
int textViewResourceId,
ArrayList objects,
Resources resLocal
)
{
super(activitySpinner, textViewResourceId, objects);
/********** Take passed values **********/
activity = activitySpinner;
data = objects;
res = resLocal;
/*********** Layout inflator to call external xml layout () **********************/
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
编译时我得到错误:(48,50)错误:不兼容的类型:AdapterView无法转换为SpinnerModel 其中CAP#1是一个新的类型变量: CAP#1扩展了适配器以捕获?
答案 0 :(得分:0)
如果您希望SpinnerModel
对象与所点击的项目相关联,您应该从适配器获取它:
SpinnerModel obj = adapter.getItem(position);
parentView
是SpinnerExample
对象。