如何使用Cursor Adapter刷新listView

时间:2013-12-19 08:28:52

标签: android listview android-listview simplecursoradapter android-cursoradapter

我使用ListView创建了CursorAdapter。现在我正在尝试更新ListView并将值刷新到ListView。

但我无法理解。如何使用LoaderchangeCursor()来刷新我的ListView

以下是我设置CursorAdapter

的代码

// SucessFully在这里完成

SQLDataSore datastore = new SQLDataSore(PrintContent.this);

Cursor cursor                 = datastore.getJSONData();

final CursorDemo cursorDemo = new CursorDemo(PrintContent.this, cursor);

list_View.setAdapter(cursorDemo);

我的Button onClick我正在将值更新到数据库中 // SucessFully完成

btn_check.setOnClickListener( new OnClickListener() {

            @Override
            public void onClick(View view ) {

                String editTextValue = edit_check.getText().toString();

                if (editTextValue!=null) {


                    SQLDataSore sqlDataSore = new SQLDataSore(PrintContent.this);

                    Cursor cursor_update = sqlDataSore.updateData(editTextValue);

//Here How Should I update my ListView ...?
                }

            }

我的UpdateData方法:

public Cursor updateData(String editContent){

        SQLiteDatabase updateContent = getReadableDatabase();


        Cursor cursor_update = updateContent.rawQuery( "update " +TABLE_NAME + " set content = '"+ editContent
                +"' "+" where _id = 357", null);

        return cursor_update;
    }

CursorDemo类

public class CursorDemo extends CursorAdapter{

        public CursorDemo(Context context, Cursor c) {

            super(context, c , false);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void changeCursor(Cursor cursor) {
            // TODO Auto-generated method stub
            super.changeCursor(cursor);
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            // TODO Auto-generated method stub

            TextView txt_content        = (TextView) view.findViewById(R.id.txt_content);
            TextView txt_likes_count    = (TextView) view.findViewById(R.id.txt_likescount);
            TextView txt_name         = (TextView) view.findViewById(R.id.txt_name);
            TextView txt_display_name = (TextView) view.findViewById(R.id.txt_display_name);

            txt_content.setText(cursor.getString(cursor.getColumnIndex("content")));

        }

        @Override
        public View newView(Context context , Cursor cursor, ViewGroup viewGroup) {
            // TODO Auto-generated method stub
            LayoutInflater inflater = LayoutInflater.from(context);
            View view = inflater.inflate(R.layout.message_row_view, viewGroup ,false);

            return view;
        }

    }

感谢任何帮助......             });

6 个答案:

答案 0 :(得分:12)

如果CursorDemo扩展了CursorAdapter,那么你必须使用adapter.swapCursor(cursor_update);

那应该将旧光标换成新光标并重新加载数据。使用swapCursor时,旧光标未关闭。

答案 1 :(得分:4)

在你的CursorDemo中你必须ower写changeCursor()方法并重置Cursor如果你有索引器,你也必须设置它的光标。

@Override
public void changeCursor(Cursor cursor) {
    mIndexer.setCursor(cursor);
    super.changeCursor(cursor);
}
  

public void changeCursor(Cursor cursor)

     

在API级别1中添加将基础游标更改为新游标。如果   有一个现有的光标将被关闭。

     

参数cursor要使用的新光标

如果符合您的要求,请尝试使用以下方法。

  

设置FilterQueryProviderand将密钥传递给filter

  final Cursor oldCursor = adapter.getCursor();
    adapter.setFilterQueryProvider(myQueryProvider);
    adapter.getFilter().filter(editTextValue, new FilterListener() {
        public void onFilterComplete(int count) {
            // assuming your activity manages the Cursor 
            // (which is a recommended way)
            stopManagingCursor(oldCursor);
            final Cursor newCursor = adapter.getCursor();
            startManagingCursor(newCursor);
            // safely close the oldCursor
            if (oldCursor != null && !oldCursor.isClosed()) {
                oldCursor.close();
            }
        }
    });

    private FilterQueryProvider myQueryProvider = new FilterQueryProvider() {
        public Cursor runQuery(CharSequence searchKey) {
            // assuming you have your custom DBHelper instance 
            // ready to execute the DB request
            return sqlDataSore.updateData(searchKey);;
        }
    };
  

PS :Cursor必须包含名为_id的列,否则此类将无效,请参阅this

答案 2 :(得分:1)

 btn_check.setOnClickListener( new OnClickListener() {

        @Override
        public void onClick(View view ) {

            String editTextValue = edit_check.getText().toString();

            if (editTextValue!=null) {


                SQLDataSore sqlDataSore = new SQLDataSore(PrintContent.this);

                Cursor cursor_update = sqlDataSore.updateData(editTextValue);

                 cursorDemo.swapCursor(cursor_update);
                 //or cursorDemo=new CursorDemo(this,cursor_update);

                  list_View.setAdapter(cursorDemo);
            }

        }

将此项放在声明ListView的活动上。只需创建一个新的适配器并将其放入新光标,然后重新创建它或交换光标。确保您的列表视图或适配器不稳定。

答案 3 :(得分:0)

你可以打电话

adapter.notifyDataSetChanged();

答案 4 :(得分:0)

“。java.lang.IllegalArgumentException:列'_id'不存在...我将_id字段输入到我的db表中”意味着代码中“cursor”的值是错误的。 请检查获取“光标”值的代码。游标必须有一个名为“_id”的列。 这是我的建议

答案 5 :(得分:0)

If you want to adapt/replace new cursor value to your list view , you should remove the old cursor from adapter and add new cursor value to the adapter.And finally adapt this adapter to listview using listview.setadapter(CursorAdapter) as follows. 

liveTagListCursor = ctx.getContentResolver().query(
                        LiveTagProvider.TAG_LIST_URI, null, null, null, null);

    tagCursorAdapter = new LiveTagListCursorAdapter(getActivity(),
                        liveTagListCursor);

    tagCursorAdapter.swapCursor(liveTagListCursor);

    listview.setAdapter(tagCursorAdapter);