我的课程扩展了 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>