我整个上午都在这上面。即使我经历了很多 tuts,也找不到一个有用的解决方案。我只是要发布我的代码。
我想要的是?我有一个 RSS 阅读器,它可以从站点获取新闻,并将其存储在自定义类中。我想创建一个具有自定义设计的 ListView,该设计显示每个新闻项目的标题。如果我只使用 Android 中的默认 simple_list_view_1,这可以正常工作,但我想在 ListView 中进行自定义设计。
这是我的代码:
ArrayList<SingleNewsItem> newsList = new ArrayList<SingleNewsItem>();
这是 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;
}
}
这是我尝试创建的自定义适配器:
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;
}
}
这是我的自定义列表视图。它只有一个标题所需的 TextView:
<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" />
这是最后几行:
myListView.setAdapter(new NewsAdapter(this, R.layout.listview, newsList));
这行不通。现在错误出现在 NewsAdapter 类中。它说:
The constructor ArrayAdapter<ArrayList<SingleNewsItem>>(Context, int, ArrayList<SingleNewsItem>) is undefined
编辑/更新
我现在运行应用程序后弹出以下强制关闭错误:
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)