本文介绍了自定义的CursorAdapter,ListView控件正在发生变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的类扩展SimpleCursorAdapter。在该类我试图让CallLog成的ListView(三TextViews(号码,姓名和日期)和两个ImageViews(其中重presents呼入和呼出)。

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).

我的code在onCreate方法:

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

我的类:

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博客jwei512()写的,但我仍然不明白我怎么可以把我的观点计算列,并在NewView的方法的另一列。

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.

我很困惑,因为我无法找到关于internet.Please解决方案帮助我。

I'm confused, because I can not find solution on the internet.Please help me.

jwei512:
那么,你会发现一些奇怪的行为 - 那就是你会看到的名字开始为你上下滚动列表中移动会发生什么事是,如果你不实例化,并把东西放到你的TextView(基本上采取行动。作为占位符),然后在您的bindView方法没有被绑定到一些TextViews的,因而换档。所以基本上,如果你看到的东西转移你的列表周围,那么这就是一个很大的标志确保您绑定的东西给所有在你的NewView的和bindView方法既你的意见。

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重新presenting我行:

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>

推荐答案

我不知道如果我真的unerstand你的问题,但方便易的地方,让所有的计算是覆盖方法setViewText()

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

这篇关于自定义的CursorAdapter,ListView控件正在发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 21:25