2012-03-29 57 views
0

我有以下代码。 我想要实现的是当我单击一个条目时更新显示的列表,以便遍历列表。 我发现了两个未注释的方法来做到这一点在stackoverflow,但都没有工作。 我也收到了关于在数据更新上创建一个新的ListActivity的建议,但这听起来像浪费资源?如何更新Android ListActivity更改连接的SimpleCursorAdapter的数据

编辑:我自己找到了解决方案。所有你需要做的就是调用“SimpleCursorAdapter.changeCursor(new Cursor);”。没有通知,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; 
    } 

} 

回答

0

首先,每次打开一个新的游标时,关闭前一个并管理新的游标。

其次,你改变你的光标,但你的光标适配器如何知道?它没有。您可以创建一个可以更改其游标的适配器,也可以在您的新游标周围创建一个新的SimpleCursorAdapter并将其插入列表中。

但是你是对的,你不应该重新创建列表小部件。

+0

我改变了了updateData功能,这一点没有任何用处,我搜索我的屁股找到如何做到这一点,要找到一个例子,教程...没有。因为我试图让这个工作2天,所以感到沮丧。不能相信没有人有过这个问题! \t private void updateData(int departmentId){\t \t \t this.stopManagingCursor(cursor); \t \t cursor.close(); \t \t cursor = mDbHelper.fetchSubItemByParentId(departmentId); \t \t this.startManagingCursor(cursor); \t \t SimpleCursorAdapter notes = new SimpleCursorAdapter(this,R.layout.department_row,cursor,from,to); \t \t this.setListAdapter(notes); \t} – DarsVaeda 2012-03-29 22:16:53

+0

请更新您的代码。我今天会照顾它。 – Snicolas 2012-03-30 07:12:13

+0

你不远,现在应该接近工作 – Snicolas 2012-03-30 07:12:31

1

当数据发生变化时,您需要通知您的ListVIew。

adapter.notifyDataSetChanged(); 

你的情况。

notes.notifyDataSetChanged(); 

希望这可以帮助你......

相关问题