0

我整个上午都在这上面。即使我经历了很多 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)
4

1 回答 1

1
ArrayList item = getItem(position);

应该

SingleNewsItem item = (SingleNewsItem) getItem(position);

而且,如果您重写该getItem()方法以使其返回 aSingleNewsItem而不是 a Object,您甚至不必强制转换它。

然后试着保存你所有的源文件,刷新+清理你的项目,它可能就是这样(发生的频率比你想象的要多)。

编辑 您的代码也没有优化,因为getView()即使对于已经创建的视图,也可以多次调用该方法,例如,如果您滚动 ListView。所以你应该首先测试你的 convertView 是否为空:

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());
于 2012-11-03T07:38:33.093 回答