问题
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>
回答1:
I don't know if I really unerstand you question, but convinient place to make all calculations is overriden method 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);
}
}
回答2:
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.
回答3:
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)
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);
}
}
来源:https://stackoverflow.com/questions/6429005/custom-cursoradapter-listview-is-shifting