Search code examples
androidlistviewandroid-listviewandroid-cursoradapter

How to update a ListView that use CursorAdapter when click a button in listview's row


I'm getting EditText's text when I click the "Save" button and this text is saved to Database and a cursor returns form Database then displayed in Listview. All saving and getting operations have no problem. When I click the save button, the list have been refreshed dynamically but When I click the "Delete" button in list row,altough the cursor updated the listview doesn't refresh itself.

I dont know what's wrong... Thanks a lot for helps...

MainActivity'S oncreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    DB = new DataBase(this,"CursorDatas",null,1);

    ContextWrapper.setContext(getApplicationContext());

    editText =(EditText) findViewById(R.id.edittext);
    btnSave = (Button)findViewById(R.id.btn_save);
    recordsList = (ListView)findViewById(R.id.list);


    cursor = DB.getRecords();


    listAdapter = new ListAdapter(this,cursor);


    recordsList.setAdapter(listAdapter);


    btnSave.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            String str = editText.getText().toString();

            DB.addRecord(str);

            cursor = DB.getRecords();

            listAdapter = new ListAdapter(getApplicationContext(),cursor);

            recordsList.setAdapter(listAdapter);


        }
    });


}

My ListAdapter class

public class ListAdapter extends CursorAdapter {

LayoutInflater inf;
ViewHolder vHolder;
DataBase DB;
Cursor cursor;
ListView lW;
ListeAdaptor listAdapter;
Context context;


public ListeAdaptor(Context context, Cursor c) {
    super(context, c);
    // TODO Auto-generated constructor stub

    inf = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inf.inflate(R.layout.activity_main, null);
    lW = (ListView)v.findViewById(R.id.list);
    DB = new DataBase(context,"CursorDatas",null,1);
    this.context = context;

}

@Override
public void bindView(View arg0, Context arg1, Cursor arg2) {
    // TODO Auto-generated method stub


    vHolder = (ViewHolder)arg0.getTag();

    vHolder.tV.setText(arg2.getString(arg2.getColumnIndex("Name")));

    vHolder.btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


            String name = vHolder.tV.getText().toString();

            DB.deleteRecords(isim);

            cursor = DB.getRecords();

            listAdapter = new ListAdapter(ContextWrapper.getContext(),cursor);
            cursor.requery();
            listAdapter.notifyDataSetChanged();
            lW.setAdapter(listAdapter);

        }
    });

}

@Override
public View newView(Context arg0, Cursor arg1, ViewGroup arg2) {
    // TODO Auto-generated method stub

    View v = inf.inflate(R.layout.list_item,null);

    ViewHolder viewHolder = new ViewHolder(v);

    v.setTag(viewHolder);



    return v;
}

}

List Item.xml

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

<TextView 
    android:id="@+id/name"
    android:layout_width="100dp"
    android:layout_height="match_parent"
    />

<Button
    android:id="@+id/btn_delete"
    android:layout_width="80dp"
    android:layout_height="50dp"
    android:layout_toRightOf="@id/name"
    android:layout_toEndOf="@id/name"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:text="Delete"></Button>


Solution

  • There is a best solution for this in which you would not have to bother updating and reassigning your adapter and setting it to your listview again and again. You should use 'CursorLoader' along with CursorAdapter and it will automatically manage the updation to your listview.here is link. http://developer.android.com/reference/android/content/CursorLoader.html