0

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

4

3 回答 3

0

恕我直言,做对了:

  1. bindView() 不是必需的。使用新视图

  2. 每个值都必须在 String[] from 中声明。

  3. 如果您想对 TextViews 进行任何操作,您应该使用 setViewText(TextView v, String text) 方法(感谢 piotrpo)

4.如果您想对 ImageViews 进行操作,请使用该代码(例如,您需要不同的图像用于来电和去电)

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);
    }

}

于 2011-06-24T12:55:23.560 回答
0

我不知道我是否真的理解你的问题,但是进行所有计算的方便地方是重写方法 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);
    }
}
于 2011-06-22T20:02:20.750 回答
0

您是否通过bindView()方法实现了,因为在更新行时会调用此方法。如果不是,则覆盖它并再次从数据库中获取数据。

于 2011-06-21T18:00:59.100 回答