Мой класс расширяет SimpleCursorAdapter.В этом классе я пытаюсь получить CallLog в ListView (с тремя TextViews (номер, имя и дата) и двумя ImageViews (который представляет входящие и исходящие вызовы).
Мой код в методе 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 смещается. Об этом писал jwei512 из блога ThinkAndroid (http://thinkandroid.wordpress.com/2010/01/11/custom-cursoradapters/), но я до сих пор не понимаю, как я могу вставить в свое представление вычисляемые столбцы и другие столбцы в методе 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>