开发者

Hide an element of ListView's item depending on Cursor's column value

I have a child of CursorAdapter class, and a ListView which every item has two TextViews. One holding a text, and another holding a number.

Text is from one Cursor column, and number from another. I want to hide number if it is equals to 0.

My View for list item is:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dp">

    <TextView
        android:background="@drawable/task_count_indicator"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentRight="true"
        android:layout_gravity="center_vertical"
        android:textColor="@color/white"
        android:id="@+id/txtTaskCount" />

    <TextView
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:ellipsize="marquee"
        android:textSize="18sp"
        android:layout_alignParentLeft="true"
        android:layout_gravity="center_vertical"
        android:id="@+id/placeItemName"/>

</RelativeLayout>

My adapter code is:

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
        return LayoutInflater.from(context).inflate(R.layout.places_list_item, viewGroup, false);
    }


    @Override
    public void bindView(View view, Context context, Cursor cursor) {

        int taskCount = cursor.get...// get to know it...

        TextView name = (TextView) view.findViewById(R.id.placeItemName);
        name.setText(
                cursor.getString(
                        cursor.getColumnIndexOrThrow(...)));

        TextView count = (TextView)view.findViewById(R.id.txtTaskCount);
        if(taskCount > 0)
            count.setText(Integer.toString(taskCount));
        else
            count.setVisibility(View.GONE);
    }

The problem is what sometimes every view that holds a number is drawn hidden, or with incorrect value (text content is always correct). I log the taskCount value - it is always correct.

开发者_运维技巧Also i noticed, if i comment an if statement and assign text to count always, like this:

    //if(taskCount > 0)
        count.setText(Integer.toString(taskCount));
    //else
        //count.setVisibility(View.GONE);

the problem isn't reproducing.


I think your problem is related to the case where taskCound>0 You should need to specify count.setVisibility(View.Visible) since the view is being reused. So be sure to define your values for each case.

Hope that helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜