Android:BaseAdapter怎么样? [英] Android : BaseAdapter how to?

查看:157
本文介绍了Android:BaseAdapter怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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



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



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



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

  @Override 
public View getView(int position,View convertView,ViewGroup parent){
/ / TODO自动生成方法存根
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的好例子吗?

解决方案

尝试从 notifyDataSetChanged() c $ c> BaseAdapter 本身。



请参阅 API演示的List8 为例。


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?

解决方案

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

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

这篇关于Android:BaseAdapter怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆