当我在代码行下面尝试时
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(位置);在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>
类型的列表,因为不知道您要传递给这个适配器的是哪种类型的列表,所以在这种情况下,请确保传递的类型与List<ItemRow>
相同,并且要遵循最佳实践,请将带有适配器的类型提为extends ArrayAdapter<ItemRow>
并在其他位置进行所需的更改。