I’ve spent all morning on this. Couldn’t find a single useful solution even though I went through plenty of tuts. I’m just going to post my code.
What I want? I have an RSS Reader that grabs news from a site, stores it in a custom class. I want to create a ListView with a custom design that displays the title from every news item. This works FINE if I just use the default simple_list_view_1 from Android, but I want a custom design in the ListView.
Here’s my code:
ArrayList<SingleNewsItem> newsList = new ArrayList<SingleNewsItem>();
This is the SingleNewsItem:
public class SingleNewsItem
{
private String pubDate;
private String title;
private String description;
private String link;
public String getPubDate()
{
return pubDate;
}
public String getTitle()
{
return title;
}
public String getDescription()
{
return description;
}
public String getLink()
{
return link;
}
public SingleNewsItem(String _pubDate, String _title, String _description, String _link)
{
pubDate = _pubDate;
description = _description;
title = _title;
link = _link;
}
@Override
public String toString()
{
return title;
}
}
Here is the Custom Adapter I tried creating:
public class NewsAdapter extends ArrayAdapter<ArrayList<SingleNewsItem>>
{
Context context;
int layoutResourceId;
ArrayList<SingleNewsItem> news;
LayoutInflater inflater;
public NewsAdapter(Context context, int textViewResourceId, ArrayList<SingleNewsItem> objects)
{
super(context, textViewResourceId, objects);
this.context = context;
this.layoutResourceId = textViewResourceId;
this.news = objects;
inflater = LayoutInflater.from(context);
}
@Override
public View getView( int position, View convertView, ViewGroup parent)
{
convertView = (RelativeLayout) inflater.inflate(layoutResourceId, null);
ArrayList item = getItem(position);
TextView title = (TextView) convertView.findViewById(R.id.text1);
title.setText(item.toString());
return parent;
}
}
This is my custom listview. It only has one TextView that I need for the Title:
<View
android:id="@+id/view1"
android:layout_width="15dp"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@android:color/holo_blue_bright" />
<View
android:id="@+id/view2"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/view1"
android:background="#660000" />
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/view1"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/view1"
android:gravity="center_vertical"
android:textSize="16dp"
android:paddingLeft="10dp"
android:background="@android:color/darker_gray"
android:textColor="#ffffff" />
Here are the final lines:
myListView.setAdapter(new NewsAdapter(this, R.layout.listview, newsList));
This doesn’t work. Right now the error is in the NewsAdapter class. It says:
The constructor ArrayAdapter<ArrayList<SingleNewsItem>>(Context, int, ArrayList<SingleNewsItem>) is undefined
EDIT/UPDATE
The following force close error pops up after I run the app now:
FATAL EXCEPTION: main
java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams
at android.widget.ListView.measureScrapChild(ListView.java:1152)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1235)
at android.widget.ListView.onMeasure(ListView.java:1144)
at android.view.View.measure(View.java:8366)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:566)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:381)
at android.view.View.measure(View.java:8366)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
at android.view.View.measure(View.java:8366)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
at android.view.View.measure(View.java:8366)
at android.view.ViewRoot.performTraversals(ViewRoot.java:844)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1865)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
should be
And if you override the
getItem()method to make it return aSingleNewsIteminstead of anObject, you will not even have to cast it.Then just try to save all your source files, refresh + clean your project it could be just that (that happen more often than you can think).
EDIT
Also your code is not optimized, because the
getView()method can be called multiple times even for a View that had already been created, if you scroll the ListView for example.So your should first test if your convertView is null or not: