【问题标题】:Android : BaseAdapter how to?Android:BaseAdapter 怎么用?
【发布时间】:2010-10-07 03:05:59
【问题描述】:

好的,我一直在搜索,但在实现 BaseAdapter 时遇到了一些问题。

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

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

我想使用 BaseAdapter 创建我自己的列表适配器来显示一个列表视图,其中包含来自数据库的多个项目。我知道这可以使用简单光标适配器来完成,但我希望以不同的方式处理行,所以我希望能够通过覆盖 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 的好例子吗?

【问题讨论】:

  • 您能否提供更多关于您想要实现的目标/未按预期工作的细节。您显示的代码在运行时会做什么?这与您尝试做的有什么不同?
  • 实际上,经过几个小时的挖掘,我想我让它以某种方式工作。我最大的问题是,如果我从列表视图不更新的行中删除一个项目,即使使用 mAdapter.notifiyDataSetChanged();

标签: android listview adapter android-cursor


【解决方案1】:

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

List8 of the API Demos中的方法为例。

【讨论】:

    猜你喜欢
    • 2012-01-12
    • 1970-01-01
    • 1970-01-01
    • 2013-10-21
    • 2014-12-17
    • 2016-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多