【问题标题】:bindView being called endlessly in custom CursorAdapterbindView 在自定义 CursorAdapter 中被无休止地调用
【发布时间】:2013-05-06 00:10:25
【问题描述】:

我有一个由自定义 CursorAdapter 提供服务的 ListView。 ListView 行非常简单 - 左侧的 CheckBox 和两个 TextView。当“单击”复选框时,将启动操作模式,以便用户可以从列表中删除多个项目。

在努力处理复选框和 OnItemClickedListeners 并跟踪检查了哪些框时,我注意到 bindView 被无休止地调用。而且,我的意思是无休止:如果 ListView 有一个项目或 100 个项目,则在片段的生命周期内,bindView 每秒被调用多次。 ListView 现在的行为完全符合我的要求,但我的代码或我的 XML 布局显然有问题。

在这个网站上搜索答案没有得到结果,所以我在这里发布我的第一个问题。

ListView 每一行的 XML 布局如下所示:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal"
     android:paddingBottom="@dimen/activity_vertical_margin" 
     android:paddingTop="@dimen/activity_vertical_margin"
     android:descendantFocusability="blocksDescendants"    
     >

    <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content"
      android:focusable="false"
      android:clickable="false"
      android:id ="@+id/chkBox" />
    <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" 
      android:text="Some example text"
      android:layout_weight=".5"
      android:id="@+id/txtExercise" />
    <TextView 
      android:id="@+id/txtDuration"
      android:layout_width="fill_parent" android:layout_height="wrap_content"
      android:text="0:00" android:gravity="right"
      android:layout_weight=".5" >

    </TextView>


     </LinearLayout>

我不确定这里要显示什么其他代码,所以我将把我的 newView 和 bindView 方法放入其中。希望这足以让有人帮助我找出问题。

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {     
    final LayoutInflater inflater = LayoutInflater.from(context);
    View v = inflater.inflate(layout, parent, false);
    Log.i(TAG, "newView=" + parent.getId());

    final ViewHolder holder = new ViewHolder();
    holder.txtExercise = (TextView) v.findViewById(R.id.txtExercise);
    holder.txtDuration = (TextView) v.findViewById(R.id.txtDuration);
    holder.check = (CheckBox) v.findViewById(R.id.chkBox);                  
    holder.check.setOnCheckedChangeListener(new OnCheckedChangeListener(){

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean checked) {
            checkedList.put((Integer) buttonView.getTag(),checked);             
        }

    });
    v.setTag(holder);       
    return v;
}


@Override
public void bindView(View v, Context arg1, Cursor c) {
    String exercise = c.getString(c.getColumnIndex("_ExcerciseName"));
    int sec = c.getInt(c.getColumnIndex(WorkoutLog.DURATION));
    String duration = returnDurationString(sec);
    Log.i(TAG, "bindview called");
    ViewHolder holder = (ViewHolder) v.getTag();

    if(holder.txtExercise !=null)
        holder.txtExercise.setText(exercise);
    if(holder.txtDuration != null)
        holder.txtDuration.setText(duration);
    if(holder.check !=null) {   
        holder.check.setTag(c.getInt(c.getColumnIndex(WorkoutLog._ID)));
        int x = (Integer) holder.check.getTag();            
        holder.check.setChecked(checkedList.get(x));            
    }
}

任何建议将不胜感激!我对 Android 开发还很陌生,所以我可能会犯一些完全愚蠢的错误,但我只是没有经验和/或知识来尝试解决这个问题。

[编辑] - 从我的 ListView 周围删除标签解决了问题!

【问题讨论】:

  • 你能把你有listview的布局贴出来吗?

标签: android simplecursoradapter


【解决方案1】:

我假设您将 android:layout_heightlistview 设置为 wrap_content。尝试切换到fill_parent

检查这个答案:Why does Wrap_Content fire BindView more than once

类似:

<ListView
        android:id="@+id/my_listView"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        //...
</ListView>

希望对您有所帮助!

【讨论】:

  • amp,您的 cmets 没有解决问题....但他们确实让我找到了问题的原因。在确认我使用了 android:layout_height 设置的 fill_parent 之后,我去调查包含 ListView 本身的布局。我发现列表视图被包装在 中。删除它可以解决问题。
  • 请注意,我必须更改两者以包装内容,因为在我的项目中,我们有一个带有网格的平板电脑布局。
猜你喜欢
  • 1970-01-01
  • 2011-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-26
  • 2013-08-19
  • 1970-01-01
相关资源
最近更新 更多