简体   繁体   中英

how to dynamically add view to a custom cursor adapter..?

how can i dynamically add a view to a custom cursor adapter.. where each row of list view look like..

<?XML version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_width="fill_parent">



<TextView android:layout_height="wrap_content"
android:layout_width="fill_parent" android:layout_weight=".9"
android:id="@+id/book_name" android:text="Default"
android:textStyle="bold" />




<TextView
    android:id="@+id/tick"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="right"
    android:text="NOT ADDED" />

</LinearLayout>

and the adapter looks like..::

package com.himanshu;

import android.content.Context;
import android.database.Cursor;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class ListViewAdapter extends SimpleCursorAdapter 
{ 
    static String[] FROM={DbHelper.BOOK_NAME };
    static int[] TO ={R.id.book_name};

public ListViewAdapter(Context context, Cursor c)
{ 
super(context, R.layout.row, c, FROM, TO);
}
// This is where the actual binding of a cursor to view happens
@Override
public void bindView(View row, Context context, Cursor cursor) 
{ 
super.bindView(row, context, cursor);

int flag = cursor.getInt(cursor.getColumnIndex(DbHelper.FLAG));
if(flag==1)
{
TextView tick = (TextView) row.findViewById(R.id.tick); 
tick.setText("ADDED"); 
}
}
}

i have used SimpleCursorAdapter because i want to access data via database... now while the activity is running there are some insertions in the database which i have to show on the list view..

您应该使用CustomAdapter并重写getView(position)来实现此功能。

When a data change you have to change the cursor using

changeCursor(your_new_cursor);

The cursor you gave before to the adapter will be closed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM