【问题标题】:Why is android SimpleCursorAdapter bindView duplicating the first row?为什么android SimpleCursorAdapter bindView 重复第一行?
【发布时间】:2012-06-23 06:31:25
【问题描述】:

我扩展了 SimpleCursorAdapter,但在使用 bindView 时遇到了一个奇怪的问题,它似乎在第一行被调用了两次。

似乎只有第一行重复。我感觉这与光标的定位方式有关,但查看了所有适配器类,似乎无法找到发生这种情况的位置。

这是我的 bindView 代码,我插入了一些日志来显示我所看到的。

@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");
                }


            }
        }
    }


}

这是我的输出,光标中只有一项

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

<?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>

【问题讨论】:

  • 这意味着我喜欢这个故事。 (即没有证据表明存在问题……您也从未暗示存在问题)。
  • 我引用“我遇到了一个奇怪的 bindView 问题,它似乎在第一行被调用了两次。”
  • 好的,那你为什么不设置你的标题“为什么bindview被调用了两次?”或者什么......说真的,你的帖子根本不是很清楚,哈哈。至少在某处使用问号...
  • 你还没有覆盖任何其他SimpleCursorAdapter 方法,是吗?

标签: android simplecursoradapter


【解决方案1】:

如果您看到序列newView/bindView 每个项目被调用两次,您可能正在使用高度设置为 wrap_content 的 ListView,这总是一个坏主意。你能确认你没有这样做吗?

另外,我不能肯定地说,但这可能是完全有效的行为,如果我的意思是你的第一个列表项被调用了两次。听起来Android必须测量第一个列表项(这需要系统调用一次bindView)并然后填充列表项(也可以通过调用bindView。填充第一行后(这需要两次调用bindView),每个后续列表项的宽度是已知的,并且每个项目只需要一次调用bindView。让我知道这是否有意义......

无论哪种方式,我希望这不是您担心的事情……再调用一次bindView 不会杀死您的应用程序。 :)

【讨论】:

  • 好吧,我已经更新了标题并添加了布局 xml。 ListView 设置为双向匹配。它也不是复制所有行,只是第一行。
  • 这确实有道理,但它也破坏了我的应用程序。每个 ListView 项目都有 3 个 EditTexts,我希望在这 3 个中的任何一个上使用 TextWatcher 来检测更改并允许用户保存行。因为在第一个项目上调用了两次 bindView,所以 textwatcher 触发并表现得好像数据一直在变化
猜你喜欢
  • 2017-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多