为什么安卓SimpleCursorAdapter bindView复制的第一行? [英] Why is android SimpleCursorAdapter bindView duplicating the first row?

查看:129
本文介绍了为什么安卓SimpleCursorAdapter bindView复制的第一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经扩展SimpleCursorAdapter和正在运行与bindView,一个奇怪的问题,它似乎是在第一行上两次调用。

I have extended SimpleCursorAdapter and am running into a weird issue with bindView, which appears to be called twice on the first row.

仅在第一行出现重复。我有什么做的光标定位方式的感觉,但在所有的适配器类都看了看,似乎无法找到这种情况出现。

Only the first row appears to be duplicated. I have a feeling it has something to do with the way the cursor is positioned, but have looked through all the adapter classes and can't seem to locate where this occurs.

下面是我的code为bindView,我已经插入了一些日志,以显示我所看到的。

Here is my code for bindView, I have inserted some Logs to show what I'm seeing.

@Override
public void bindView(View view, Context context, Cursor cursor) {
    final ViewBinder binder = getViewBinder();
    final int count = mTo.length;
    final int[] from = mFrom;
    final int[] to = mTo;

    Log.v("builder", "cursor count"+cursor.getCount() );

    Log.v("builder", "mTo.length"+count);
    //Bind all Views
    for (int i = 0; i < count; i++) {
        final View v = view.findViewById(to[i]);
        if (v != null) {
            boolean bound = false;
            if (binder != null) {
                bound = binder.setViewValue(v, cursor, from[i]);
            }

            if (!bound) {
                String text = cursor.getString(from[i]);
                v.setVisibility(View.VISIBLE);
                if (text == null && !(v instanceof ImageView)) {
                    text = "";
                    v.setVisibility(View.GONE);
                } 

                if (v instanceof TextView) {
                    setViewText((TextView) v, text);
                    if (v instanceof EditText){
                        EditText et = (EditText) v;
                        Log.v("builder", "setting up edittext"+cursor.getPosition()+i);
                    //  setUpEditors(view, et);
                    }
                } else if (v instanceof ImageView) {
                    setViewImage((ImageView) v, text);
                } else {
                    throw new IllegalStateException(v.getClass().getName() + " is not a " +
                            " view that can be bound by this SimpleCursorAdapter");
                }


            }
        }
    }


}

下面是我的只有一个项目输出光标

Here is my output with only one item in the cursor

06-22 15:15:03.797: V/builder(27573): cursor count1
06-22 15:15:03.797: V/builder(27573): mTo.length5
06-22 15:15:03.807: V/builder(27573): setting up edittext02
06-22 15:15:03.807: V/builder(27573): setting up edittext03
06-22 15:15:03.807: V/builder(27573): setting up edittext04
06-22 15:15:03.887: V/builder(27573): cursor count1
06-22 15:15:03.887: V/builder(27573): mTo.length5
06-22 15:15:03.897: V/builder(27573): setting up edittext02
06-22 15:15:03.897: V/builder(27573): setting up edittext03
06-22 15:15:03.907: V/builder(27573): setting up edittext04

这是2项

06-22 15:17:28.337: V/builder(27573): cursor count2
06-22 15:17:28.337: V/builder(27573): mTo.length5
06-22 15:17:28.337: V/builder(27573): setting up edittext02
06-22 15:17:28.337: V/builder(27573): setting up edittext03
06-22 15:17:28.337: V/builder(27573): setting up edittext04
06-22 15:17:28.417: V/builder(27573): cursor count2
06-22 15:17:28.417: V/builder(27573): mTo.length5
06-22 15:17:28.417: V/builder(27573): setting up edittext02
06-22 15:17:28.427: V/builder(27573): setting up edittext03
06-22 15:17:28.427: V/builder(27573): setting up edittext04
06-22 15:17:28.517: V/builder(27573): cursor count2
06-22 15:17:28.517: V/builder(27573): mTo.length5
06-22 15:17:28.527: V/builder(27573): setting up edittext12
06-22 15:17:28.527: V/builder(27573): setting up edittext13
06-22 15:17:28.527: V/builder(27573): setting up edittext14

下面是其中的ListView位于XML

Here's the xml where the ListView is located

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent" android:orientation="vertical">

<TextView
    android:id="@+id/textView6"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/labels_background"
    android:text="@string/lb_item_type"
    android:textSize="@dimen/dm_maint_tv" />

<CheckBox
    android:id="@+id/mt_base"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/lb_base" />

<TextView
    android:id="@+id/textView10"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:background="@color/labels_background"
    android:text="@string/lb_build"
    android:textSize="@dimen/dm_maint_tv" />

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="0dp" android:layout_weight="1" android:id="@+id/builder">

    <ListView
        android:id="@+id/mt_build_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>

    <ImageView
        android:id="@+id/mt_additem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="3dp"
        android:layout_marginTop="3dp"
        android:scaleType="centerInside"
        android:src="@drawable/ic_input_add" android:layout_gravity="top|right"/>

</FrameLayout>

推荐答案

如果你看到序列 NewView的 / bindView 每个项目两次打电话,你可能正在使用的ListView其高度设置为WRAP_CONTENT,这始终是一个坏主意。你能否确认你是不是这样?

If you see the sequence newView/bindView called twice per item, you are probably using ListView with its height set to wrap_content, which is always a bad idea. Can you confirm that you are not doing this?

另外,我不能肯定地说,但是这可能是完全合法的行为,如果你的第一个列表项被调用两次我的意思。这听起来像Android的必须测量第一个列表项(这就要求系统调用 bindView 一次)和然后的填充列表项(也被调用 bindView 之后的第一行填充(这需要两次调用 bindView ),以后每次名单的宽度项目是已知的,要求每个每个项目只有一个调用 bindView 。让我知道,如果这是有道理...

Also, I can't say for sure but this might be perfectly valid behavior, if your first list item is being called twice I mean. It sounds like Android must measure the first list item (which requires the system to call bindView one time) and then populate the list item (also by calling bindView. After the first row is populated (which requires two calls to bindView), the width of each subsequent list item is known and requires only one call to bindView per each item. Let me know if that makes sense...

无论哪种方式,我希望这不是你担心......一个额外调用 bindView 不会杀你的应用程序。 :)

Either way, I hope this isn't something you are worried about... one additional call to bindView won't kill your app. :)

这篇关于为什么安卓SimpleCursorAdapter bindView复制的第一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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