【问题标题】:Custom CursorAdapter, ListView is shifting自定义 CursorAdapter,ListView 正在转移
【发布时间】:2011-06-21 16:50:56
【问题描述】:

我的课程扩展了 SimpleCursorAdapter。在那个类中,我尝试将 CallLog 放入 ListView(带有三个 TextView(号码、姓名和日期)和两个 ImageView(代表呼入和呼出)。

我在 OnCreate 方法中的代码:

    Cursor c = getContentResolver().query(android.provider.CallLog.Calls.CONTENT_URI, null, "TYPE IN (\"1\",\"2\")", null, "DATE DESC");
    startManagingCursor(c);

    String[] from = new String[] {"number", "name" , "_ID" };
    int[] to = new int[] {R.id.number, R.id.name, R.id.duration};
    listNotImported=(ListView)findViewById(R.id.ListOfNotImportedCalls);
    listNotImported.setOnItemClickListener(this);
    list1Adapter5 = new IconicAdapter5(this, R.layout.rownotimportedcalls, c, from, to);
    listNotImported.setAdapter(list1Adapter5);

我的班级:

class IconicAdapter5 extends SimpleCursorAdapter
{


    public IconicAdapter5(Context context, int layout, Cursor c, String[] from, int[] to) 
    {
        super(context, layout, c, from, to);
    }
    public View newView(Context context, Cursor c, ViewGroup parent )
    {
        rowNotImported = super.newView(CallLog.this, c, parent);
        String duration= c.getString(c.getColumnIndex("DURATION"));
        ((TextView)rowNotImported.findViewById(R.id.duration)).setText(duration);
        return (rowNotImported);
}

列“数字”、“名称”、“_ID”工作正常,一切正常。但是持续时间和我尝试从 newView 方法中的光标(例如“持续时间”)获取的另一列正在发生变化。 来自 ThinkAndroid 博客 (http://thinkandroid.wordpress.com/2010/01/11/custom-cursoradapters/) 的 jwei512 对此进行了介绍,但我仍然不明白如何将计算列和 newView 方法中的其他列放入我的视图中。

我很困惑,因为我在互联网上找不到解决方案。请帮助我。

jwei512: “然后你会注意到一些奇怪的行为——也就是说,当你上下滚动列表时,你会看到名称开始变化。如果你不实例化并将某些东西放入你的 TextView (基本上是为了行动),会发生什么情况作为占位符)然后在您的 bindView 方法中,没有任何内容与某些 TextViews 绑定,因此不会发生变化。所以基本上,如果您看到列表中的内容在移动,那么这是一个很大的标志,可以确保您将事物绑定到所有人newView 和 bindView 方法中的视图。”

XML 代表我的行:

    <TableLayout android:layout_alignParentLeft="true" 
    android:layout_alignBaseline="@+id/myTable" android:id="@+id/myTable"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:stretchColumns="0"
    android:shrinkColumns="yes">


    <TableRow>

    <TextView
    android:id="@+id/name"
    android:textSize="18dp"
    android:textColor="@color/white"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content">
    </TextView>

    <ImageView  
    android:layout_width="20dp" 
    android:layout_height="20dp" 
    android:id="@+id/imageViewType">
    </ImageView>

    </TableRow>




    <TableRow>
    <LinearLayout>
    <TextView
    android:id="@+id/headerNumber"
    android:textColor="@color/white" 
    android:textSize="12sp" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content">
    </TextView>


    <TextView 
    android:id="@+id/number"
    android:textColor="@color/white" 
    android:textSize="12dp" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dip"
    >
    </TextView>
    </LinearLayout>
    </TableRow>

        <TableRow>
    <LinearLayout>
    <TextView
    android:id="@+id/headerDate"
    android:textColor="@color/white" 
    android:textSize="12dp" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content">

    </TextView>
    <TextView 
    android:id="@+id/date"
    android:textColor="@color/white" 
    android:textSize="12dp" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dip" >
    </TextView>

    </LinearLayout>
    </TableRow>


    <TableRow>
    <LinearLayout>
    <TextView
    android:id="@+id/headerDuration"
    android:textColor="@color/white" 
    android:textSize="12dp" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content">

    </TextView>
    <TextView 
    android:id="@+id/duration"
    android:textColor="@color/white" 
    android:textSize="12dp" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp" >
    </TextView>

    </LinearLayout>
    </TableRow>



    </TableLayout>

【问题讨论】:

  • 据我了解,您需要将 something 设置为 newView 和 bindView 中的视图。如果您没有任何数据可以放在 bindView 或 newView 中,请尝试将持续时间 TextView 设置为空字符串(或单个空格)。
  • 感谢 Audrius,我就是这样做的(没有帮助)if (duration == null) duration=getText(R.string.unknown).toString();
  • 好的,你能解释一下你所说的“转移”是什么意思吗?列表视图中的项目是否偶尔更改位置,或者列表视图项目内容视图在滚动时是否重新调整大小?
  • 例如。在第一行中,我有值(姓名、电话、持续时间)JOHN、5455454545、43。如果我向下滚动并返回第一页,我将在第一行看到不同的值。有时当我滚动整个页面时它会改变,有时它会改变得更快。是不是我必须在数组 FROM 中声明所有内容?
  • 您不需要(尽管这是一个好习惯)在查询 from 参数中声明所有内容。现在我不会再浪费时间让 SimpleCursorAdapter 实现工作了,因为这需要你太多时间,只需自己实现 CursorAdapter 或任何其他ListAdapter。顺便说一句,jwei512 示例如何为您工作,您是否仍然遇到同样的转换问题?

标签: android listview adapter simplecursoradapter


【解决方案1】:

我不知道我是否真的理解你的问题,但是进行所有计算的方便的地方是重写方法 setViewText()

private class MyCursorAdapter extends SimpleCursorAdapter {



    private Date d = new Date();

    @Override
    public void setViewText(TextView v, String text) {
        if (v.getId() == R.id.TrackTime) {
            d.setTime(Long.parseLong(text));
            text = d.toLocaleString();
        }
        super.setViewText(v, text);
    }
}

【讨论】:

  • 它适用于日期(现在我可以在 FROM 参数中声明它)并从 long 转换为 date。但是我不能声明的列(例如图像表示的值,例如调用类型?)
  • 查看文档——当数据被传递到 ImageView 时,还有一个 public void setViewImage (ImageView v, String value) 方法被调用
  • 你能看看我的方法吗?我阅读了文档,但仍然无法编写工作代码。我的价值观再次转变(当我放 ImageView 时) *** public void setViewImage (ImageView v, String value) { if (v.getId() == R.id.imageViewType) { if(value.equals("1") == true) value= "R.drawable.sym_call_incoming"; if(value.equals("2") == true) value="R.drawable.sym_call_outgoing"; } super.setViewImage(v, value); }
  • 请为列表项添加布局。
【解决方案2】:

您是否通过 bindView() 方法来实现,因为在更新行时会调用此方法。如果没有,则覆盖它并再次从数据库中获取数据。

【讨论】:

    【解决方案3】:

    恕我直言,做对了:

    1. bindView() 不是必需的。使用新视图

    2. 每个值都必须在 String[] from 中声明。

    3. 如果你想对 TextViews 进行任何操作,你应该使用 setViewText(TextView v, String text) 方法(感谢 piotrpo)

    4.如果您想对 ImageViews 进行操作,请使用该代码(例如,您希望呼入和呼出使用不同的图像)

    class IconicAdapter5 extends SimpleCursorAdapter
    {
        private int source = 0;
    
        @Override
        public void setViewImage (ImageView v, String value) 
        {
             if (v.getId() == R.id.imageViewType) 
                {
                     if(value.equals("1") == true)
                     {
                        source= (R.drawable.sym_call_incoming);
                     }
    
                     if(value.equals("2") == true)
                     {
                        source= (R.drawable.sym_call_outgoing);
                     }
                 }
             v.setImageResource(source);
        }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-13
      相关资源
      最近更新 更多