Android:BaseAdapter 怎么做?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/540461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:16:24  来源:igfitidea点击:

Android : BaseAdapter how to?

androidlistviewcursoradapter

提问by Chrispix

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

好的,我一直在搜索,但在实现 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.htmlas per the example above.

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

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

这里有一个非常好的 BaseAdapter 示例: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.

我想使用 BaseAdapter 创建我自己的列表适配器来显示一个列表视图,其中包含来自数据库的多个项目。我知道这可以使用 Simple Cursor Adapter 来完成,但我希望以不同的方式处理行,因此我希望能够通过覆盖 getView 来绘制每一行。数据将从游标中拉出。

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. :

我知道这段代码对于获取游标数据来说很丑陋,但假设我已经填充了一个游标。如果第 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;
}

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

你有使用游标绘制 BaseAdapter 的好例子吗?

回答by Matt

Try calling notifyDataSetChanged()from a method inside the BaseAdapteritself.

尝试notifyDataSetChanged()BaseAdapter自身内部的方法调用。

See the methods in List8 of the API Demosas an example.

以API Demo的List8中的方法为例。