how to update an Android ListActivity on changing data of the connected SimpleCursorAdapter

Posted by 4485670 on Stack Overflow See other posts from Stack Overflow or by 4485670
Published on 2012-03-29T18:25:16Z Indexed on 2012/03/29 23:28 UTC
Read the original article Hit count: 241

I have the following code. What I want to achieve is to update the shown list when I click an entry so I can traverse through the list. I found the two uncommented ways to do it here on stackoverflow, but neither works. I also got the advice to create a new ListActivity on the data update, but that sounds like wasting resources?

EDIT: I found the solution myself. All you need to do is call "SimpleCursorAdapter.changeCursor(new Cursor);". No notifying, no things in UI-Thread or whatever.

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class MyActivity extends ListActivity {
    private DepartmentDbAdapter mDbHelper;
    private Cursor cursor;
    private String[] from = new String[] { DepartmentDbAdapter.KEY_NAME };
    private int[] to = new int[] { R.id.text1 };
    private SimpleCursorAdapter notes;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.departments_list);
    mDbHelper = new DepartmentDbAdapter(this);
    mDbHelper.open();

    // Get all of the departments from the database and create the item list
    cursor = mDbHelper.fetchSubItemByParentId(1);
    this.startManagingCursor(cursor);

    // Now create an array adapter and set it to display using our row
    notes = new SimpleCursorAdapter(this, R.layout.department_row, cursor, from, to);
    this.setListAdapter(notes);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        // get new data and update the list
        this.updateData(safeLongToInt(id));
    }

    /**
     * update data for the list
     * 
     * @param int departmentId id of the parent department
     */
    private void updateData(int departmentId) {     
        // close the old one, get a new one
        cursor.close();
        cursor = mDbHelper.fetchSubItemByParentId(departmentId);

    // change the cursor of the adapter to the new one
    notes.changeCursor(cursor);
    }

    /**
     * safely convert long to in to save memory
     * 
     * @param long l the long variable
     * 
     * @return integer
     */
    public static int safeLongToInt(long l) {
    if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
        throw new IllegalArgumentException
            (l + " cannot be cast to int without changing its value.");
    }
    return (int) l;
    }

}

© Stack Overflow or respective owner

Related posts about android

Related posts about simplecursoradapter