I understand its because my list_item.xml but what is the reason
I am getting this error
java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.ImageView
E/AndroidRuntime(27089): at com.example.jsn.ImageListAdapter.getView(ImageListAdapter.java:44)
this is my code
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (null == convertView) {
convertView = inflater.inflate(R.layout.list_item, parent, false);
}
Picasso
.with(context)
.load(imageUrls[position])
.placeholder(R.drawable.ic_launcher) // can also be a drawable
.fit() // will explain later
.noFade()
.into((ImageView) convertView);
return convertView; // I guess here is my problem
}
}
this is my xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f0f0f0">
<ImageView
android:id="@+id/ImageView"
android:layout_width="match_parent"
android:layout_height="200dp"/>
</RelativeLayout>
it works when I my list_item.xml like this :
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ImageView"
android:layout_width="match_parent"
android:layout_height="200dp"/>
but why ? how to make it work with relativelayout
Your convertView
is the result of inflater.inflate(R.layout.list_item, parent, false);
which means it is an instance of the root element of list_item.xml
, i.e. a RelativeLayout
.
To fix this, you need to do the following:
Picasso
.with(context)
.load(imageUrls[position])
.placeholder(R.drawable.ic_launcher) // can also be a drawable
.fit() // will explain later
.noFade()
.into((ImageView) convertView.findViewbyId(R.id.ImageView));