当我尝试下面的代码行
ItemRow itemdata = data.get(position);
在 ItemAdapter 类中我收到错误
所需的不兼容类型:com.fragment.ItemRow 发现:java.lang.object
完整的ItemRow.java类如下:
Package com.fragments;
import android.graphics.drawable.Drawable;
public class ItemRow {
String itemName;
Drawable icon;
public ItemRow(String itemName, Drawable icon) {
super();
this.itemName = itemName;
this.icon = icon;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public Drawable getIcon() {
return icon;
}
public void setIcon(Drawable icon) {
this.icon = icon;
}
}
以及完整的 Itemadapeter 类,当我尝试时,我在其中得到了不兼容的类型
-ItemRow itemdata = data.get(position);在getView中
package com.fragments;
public class ItemAdapter extends ArrayAdapter {
List data;
Context context;
int layoutResID;
public ItemAdapter(Context context, int layoutResourceId,List data) {
super(context, layoutResourceId, data);
this.data=data;
this.context=context;
this.layoutResID=layoutResourceId;
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
NewsHolder holder = null;
View row = convertView;
holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResID, parent, false);
holder = new NewsHolder();
holder.itemName = (TextView)row.findViewById(R.id.example_itemname);
holder.icon=(ImageView)row.findViewById(R.id.example_image);
holder.button1=(Button)row.findViewById(R.id.swipe_button1);
holder.button2=(Button)row.findViewById(R.id.swipe_button2);
holder.button3=(Button)row.findViewById(R.id.swipe_button3);
row.setTag(holder);
}
else
{
holder = (NewsHolder)row.getTag();
}
ItemRow itemdata = data.get(position);
holder.itemName.setText(itemdata.getItemName());
holder.icon.setImageDrawable(itemdata.getIcon());
holder.button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "Button 1 Clicked",Toast.LENGTH_SHORT).show();
}
});
holder.button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "Button 2 Clicked",Toast.LENGTH_SHORT).show();
}
});
holder.button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "Button 3 Clicked",Toast.LENGTH_SHORT);
}
});
return row;
}
static class NewsHolder{
TextView itemName;
ImageView icon;
Button button1;
Button button2;
Button button3;
}
}
最佳答案
您的列表是raw type
意思是Object
所以你必须向下转换 Object
到你的 POJO 即 ItemRow
ItemRow itemdata = (ItemRow )data.get(position);
或者你应该有一个 List<ItemRow>
的列表type ,因为不知道您要传递给此适配器的列表类型,因此请确保传递与 List<ItemRow>
相同的类型在这种情况下,请遵循最佳实践,将带有适配器的类型称为 extends ArrayAdapter<ItemRow>
并在其他地方进行所需的更改。
关于java - 发现不兼容的类型 java.lang : ArrayAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40129444/