Android:BaseAdapter 如何?

发布于 2024-07-14 01:50:42 字数 1201 浏览 5 评论 0原文

好吧,我一直在仔细搜索,并且在实现 BaseAdapter 时遇到了一些问题。

我已经能够实现一个简单的光标适配器 http://开发人员。 android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List7.html 按照上面的示例。

这里有一个非常好的 BaseAdapter 示例: List14 google 示例

我想使用 BaseAdapter 创建自己的列表适配器来显示 listView,其中包含数据库中的多个项目。 我知道这可以使用简单游标适配器来完成,但我希望以不同的方式处理行,因此我希望能够通过重写 getView 来绘制每一行。 数据将从游标中提取。

我知道这段代码对于获取光标数据来说很难看,但假设我已经填充了光标。 如果第 8 列包含图像资源 id,您对此有何建议。 :

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    cursor.moveToPosition(position);
    ImageView i = new ImageView(mContext);
    i.setImageResource(cursor.getShort(8));
    i.setAdjustViewBounds(true);
    i.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    return i;
}

您有任何使用光标绘制 BaseAdapter 的好例子吗?

Ok, I have been searching thick and thin, and I am having some issues implementing a BaseAdapter.

I have been able to implement a Simple Cursor Adapter
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List7.html as per the example above.

There is a pretty good BaseAdapter example here : List14 google example

I am wanting to create my own List Adapter using BaseAdapter to show a listView, with multiple items from a Database. I know this can be done using the Simple Cursor Adapter, but I am looking to handle rows differently, so I want to be able to draw each row by overriding getView. The data would be pulled from a cursor.

I know this code is ugly for getting to the cursor data, but assuming I have populated a cursor. What suggestions do you have on this if column 8 contains the image resource id. :

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    cursor.moveToPosition(position);
    ImageView i = new ImageView(mContext);
    i.setImageResource(cursor.getShort(8));
    i.setAdjustViewBounds(true);
    i.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    return i;
}

Do you have any good examples of a BaseAdapter being drawn using a cursor?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Hello爱情风 2024-07-21 01:50:42

尝试从 BaseAdapter 本身内部的方法调用 notifyDataSetChanged()

请参阅 List8 中的方法以API演示为例。

Try calling notifyDataSetChanged() from a method inside the BaseAdapter itself.

See the methods in List8 of the API Demos as an example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文