简体   繁体   English

如何在更改连接的SimpleCursorAdapter的数据时更新Android ListActivity

[英]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. 我在stackoverflow上找到了两种未注释的方法来执行此操作,但是都没有用。 I also got the advice to create a new ListActivity on the data update, but that sounds like wasting resources? 我还得到了在数据更新上创建新的ListActivity的建议,但这听起来像浪费资源吗?

EDIT: I found the solution myself. 编辑:我自己找到了解决方案。 All you need to do is call "SimpleCursorAdapter.changeCursor(new Cursor);". 您需要做的就是调用“ SimpleCursorAdapter.changeCursor(new Cursor);”。 No notifying, no things in UI-Thread or whatever. 没有通知,UI-Thread中没有任何东西。

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. 数据更改时,您需要通知ListVIew。

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. 您可以创建可以更改其光标的适配器,也可以在新光标周围创建一个新的SimpleCursorAdapter并将其插入列表。

But you were right, you should not recreate the list widget. 但是您说对了,您不应该重新创建列表小部件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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