Hadi Samadbin Hadi Samadbin - 20 days ago 7
Android Question

Incompatible types found java.lang : ArrayAdapter

when I try below line of code

ItemRow itemdata = data.get(position);


in ItemAdapter class I got the error

Incompatible types required: com.fragment.ItemRow found: java.lang.object

the complete ItemRow.java class is as below:

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;
}

}


and the complete Itemadapeter class in which I got the incompatible type when I try

-ItemRow itemdata = data.get(position); in 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;
}

}

Answer

Your list is raw type mean Object so you have to down-cast the Object to your POJO i.e ItemRow

ItemRow itemdata = (ItemRow )data.get(position);

or you should have a list of List<ItemRow> type , since there is no idea which kind of list you are passing to this adapter so make sure you pass the same type as List<ItemRow> in this case plus to follow best practice , mention the type with adapter as extends ArrayAdapter<ItemRow> and make the required changes at other places.