我整个上午都在这上面。即使我经历了大量的尝试,也找不到一个有用的解决方案。我只想发布我的代码。
我想要什么?我有一个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)
答案 0 :(得分:1)
ArrayList item = getItem(position);
应该是
SingleNewsItem item = (SingleNewsItem) getItem(position);
如果您覆盖getItem()
方法以使其返回SingleNewsItem
而不是Object
,则您甚至不必投出它。
然后只是尝试保存所有源文件,刷新+清理你的项目就可能就是这样(这种情况比你想象的更频繁)。
修改强>
此外,您的代码未进行优化,因为如果您滚动ListView,即使对于已创建的视图,也可以多次调用getView()
方法。
所以你应首先测试你的convertView是否为null:
SingleNewsItem item = getItem(position); //You should override this method
TextView tv;
if (convertView != null) { //View already created
tv = (TextView) convertView.getTag();
}
else { //View already created
convertView = this.inflater.inflate(layoutResourceId, null);
tv = (TextView) convertView.findViewById(R.id.text1);
convertView.setTag(tv);
}
tv.setText(item.toString());