自定义CursorAdapter,ListView正在移位
我的类扩展了 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);
}
列“number”、“name” ,“_ID”工作正常,一切正常,但持续时间和我尝试从 newView 方法中的光标获取的其他列(例如“持续时间”)正在移动。 jwei512 来自 ThinkAndroid 博客 ( http://thinkandroid.wordpress.com/2010/01 /11/custom-cursoradapters/ )写了相关内容,但我仍然不明白如何将计算列放入视图中,并在 newView 方法中放入另一列。
我很困惑,因为我在互联网上找不到解决方案。请帮助我。
jwei512: “然后你会注意到一些奇怪的行为 - 即,当你上下滚动列表时,你会看到名称开始变化。如果你不实例化并将某些内容放入 TextView 中(基本上是为了执行操作),会发生什么情况?作为占位符)然后在你的bindView方法中没有任何东西绑定到某些TextView,因此基本上,如果你看到列表中的东西在移动,那么这是一个重要的标志,以确保你将东西绑定到所有东西。您的观点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>
My class extends SimpleCursorAdapter. In that class I try to get CallLog into ListView (with three TextViews (number, name and date) and two ImageViews (which represents incoming and outgoing calls).
My code in OnCreate method:
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);
My class:
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);
}
Columns "number", "name" , "_ID" works fine, everything is ok. but duration, and another columns, which I try to get from cursor (eg "duration") in newView method are shifting.
jwei512 from ThinkAndroid blog ( http://thinkandroid.wordpress.com/2010/01/11/custom-cursoradapters/ ) wrote about that, but I still dont understand how I can put into my view calculated columns, and another columns in newView method.
I'm confused, because I can not find solution on the internet.Please help me.
jwei512:
"Then you’ll notice some weird behavior – namely, that you’ll see the names start shifting as you scroll up and down the list. What happens is that if you don’t instantiate and place something into your TextView (basically to act as a place holder) then in your bindView method nothing gets bound to some of the TextViews and thus the shifting. So basically, if you see stuff shifting around in your lists, then that’s a big flag for make sure you are binding things to all of your views in both your newView and bindView methods."
XML representing my row:
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道我是否真的理解你的问题,但是进行所有计算的方便地方是重写方法 setViewText()
I don't know if I really unerstand you question, but convinient place to make all calculations is overriden method setViewText()
您是否通过 bindView() 方法实现了,因为在更新行时会调用此方法。如果没有,则覆盖它并再次从数据库中获取数据。
Have you implemented through bindView() method since on updating the rows this method is called. If not then override that and again fetch data from database.
恕我直言,正确做法是:
bindView() 不是必需的。使用 newView
每个值都必须在 String[] 中声明。
如果你想对 TextView 进行任何操作,你应该使用 setViewText(TextView v, String text) 方法(感谢 piotrpo)
4.如果您想对 ImageView 进行操作,请使用该代码(例如,您希望传入和传出的呼叫使用不同的图像)
}
IMHO to do it right:
bindView() is not necessary. use newView
Every value must be declared in String[] from.
if you want to make any operations on TextViews you should use setViewText(TextView v, String text) method (thanks piotrpo)
4.If you want do operations on ImageViews use that code (eg you want different images for incoming and outgoing calls)
}