简体   繁体   中英

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

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

}

You need to notify your ListVIew when your data get changed.

adapter.notifyDataSetChanged();

in your case.

notes.notifyDataSetChanged();

Hope this may help you...

First, every time you open a new cursor, close the previous one and manage the new one.

Second, you change your cursor, but how does your cursor adapter know that ? It doesn't. Either you create an adapter that can change its cursor or your create a new SimpleCursorAdapter around your new cursor and plug it to the list.

But you were right, you should not recreate the list widget.

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