Android 错误原因:不兼容类型:AdapterView<;第1章>;无法转换?
我试图强制转换OnItemSelected方法的父对象以检索选定项的对象,但出现错误Android 错误原因:不兼容类型:AdapterView<;第1章>;无法转换?,android,android-arrayadapter,Android,Android Arrayadapter,我试图强制转换OnItemSelected方法的父对象以检索选定项的对象,但出现错误 TextView output = null; CustomAdapter adapter; MainActivity activity = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.a
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();
}
TextView输出=null;
自定义适配器;
MainActivity=null;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
活动=此;
微调器微调器示例=(微调器)findViewById(R.id.Spinner);
输出=(TextView)findViewById(R.id.company);
//在arraylist中设置数据
setListData();
//传递给适配器以获取映像的资源
Resources res=getResources();
//创建自定义适配器对象(请参见下面的CustomAdapter.java)
适配器=新的CustomAdapter(活动,R.layout.spinner\u行,CustomListViewValuesArr,res);
//将适配器设置为微调器
喷丝头示例。设置适配器(适配器);
//选择微调器项时调用侦听器
SpinnerExample.setOnItemSelectedListener(新的OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图v、整型位置、长id){
//你的代码在这里
SpinnerModel obj=(SpinnerModel)父视图;
//获取要在屏幕上显示的选定行数据
字符串Company=((TextView)v.findviewbyd(R.id.Company)).getText().toString();
字符串CompanyUrl=((TextView)v.findViewById(R.id.sub)).getText().toString();
String OutputMsg=“所选公司:\n\n”+公司+“\n”+公司URL;
Toast.makeText(
getApplicationContext(),OutputMsg,Toast.LENGTH_LONG).show();
Toast.makeText(
getApplicationContext(),xxx.getCompanyName(),Toast.LENGTH\u LONG.show();
}
公共类CustomAdapter扩展了ArrayAdapter{
私人活动;
私有数组列表数据;
公共资源;
SpinnerModel tempValues=null;
充气机;
/*************自定义适配器构造函数*****************/
公共自定义适配器(
主活动活动微调器,
int textViewResourceId,
ArrayList对象,
本地资源
)
{
超级(activitySpinner、textViewResourceId、对象);
/**********接受传递的值**********/
活动=活动微调器;
数据=对象;
res=resLocal;
/***********布局充气器调用外部xml布局()**********************/
充气器=(LayoutInflater)activity.getSystemService(Context.LAYOUT\u充气器\u SERVICE);
}
编译时出现错误:(48,50)错误:不兼容的类型:AdapterView无法转换为SpinnerModel
其中CAP#1是一个新类型变量:
CAP#1从捕获?扩展适配器如果您想要与单击的项目关联的
SpinnerModel
对象,您应该从适配器获取它:
SpinnerModel obj = adapter.getItem(position);
parentView
是SpinnerExample
对象
SpinnerModel obj = adapter.getItem(position);