【发布时间】: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