首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >游标getPosition始终为0

游标getPosition始终为0
EN

Stack Overflow用户
提问于 2016-09-23 07:57:47
回答 2查看 271关注 0票数 1

我有一个getCount返回50的游标。由于需要在ListView中显示内容,我使用了一个光标适配器,如下所示。出于某种奇怪的原因,列表总是包含一个元素。在日志记录中,我看到cursor.getPosition()始终为0。为什么会这样呢?我使用CursorAdapters已经有一段时间了,以前从来没有遇到过这个问题。所以我很困惑,看不出我做错了什么。

代码语言:javascript
代码运行次数:0
运行
复制
Cursor cursor = getContext().getContentResolver().query(uri, PROJECTION,null,null,null);
    MyLog.d(TAG,"cursor count is %s",cursor.getCount());
    cursor.moveToFirst();
    CursorAdapter cursorAdapter = new CursorAdapter(getContext(),cursor,true){
        @Override
        public View newView(Context context,Cursor cursor,ViewGroup parent){
            MyLog.d(TAG,"newView cursor count %s position %s",cursor.getCount(),cursor.getPosition());
            return LayoutInflater.from(context).inflate(R.layout.my_row,parent,false);
        }

        @Override
        public void bindView(View view,Context context,Cursor cursor){
            MyLog.d(TAG,"bindView at index %s",cursor.getPosition());
            TextView name = (TextView) view.findViewById(R.id.name);
            TextView address = (TextView) view.findViewById(R.id.address);
            String naTxt = cursor.getString(COL_NAME);
            String addTxt = cursor.getString(COL_ADDR)
                    +" "
                    +cursor.getString(COL_STR);
            name.setText(naTxt);
            address.setText(addTxt);
        }
    };

    root.setAdapter(cursorAdapter);
EN

回答 2

Stack Overflow用户

发布于 2016-09-23 09:58:22

我发现了这个问题,这确实是最新的android中的一个新东西,因为我以前从未见过它。对于行布局,我使用了android:layout_height="match_parent"。我应该用的是android:layout_height="wrap_content"。难倒我了。

票数 1
EN

Stack Overflow用户

发布于 2016-09-23 09:14:31

请尝试删除

cursor.moveToFirst();

并使用

代码语言:javascript
代码运行次数:0
运行
复制
CursorAdapter cursorAdapter = new CursorAdapter(getContext(),cursor,0)

代替CursorAdapter cursorAdapter = new CursorAdapter(getContext(),cursor,true)

并且您应该使用android.widget.CursorAdapter

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39650581

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档