how to refresh the listView using the Cursor Adapter

前端 未结 6 2234
名媛妹妹
名媛妹妹 2021-01-05 07:56

I have created a ListView using CursorAdapter . Now I am Trying to update the ListView and Refresh the value to the ListView .

相关标签:
6条回答
  • 2021-01-05 08:03

    ".java.lang.IllegalArgumentException: column '_id' does not exist ...althouh I have _id field in to my db table" means that the value of "cursor" in your code is wrong. Check the code of getting the value of "cursor" please. A cursor must have a column named '_id'。 This is my suggestion

    0 讨论(0)
  • 2021-01-05 08:04
     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);
                }
    
            }
    

    Put this on the activity where declare the ListView . Just create a new adapter and put it in new cursor then recreate it or swap the cursor. make sure your listview or adapter not constant.

    0 讨论(0)
  • 2021-01-05 08:04

    you could call

    adapter.notifyDataSetChanged();
    
    0 讨论(0)
  • 2021-01-05 08:15

    In your CursorDemo you have to owerwrite changeCursor() method and reset the Cursor if you have indexer you have to set it's cursor too.

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

    public void changeCursor (Cursor cursor)

    Added in API level 1 Change the underlying cursor to a new cursor. If there is an existing cursor it will be closed.

    Parameters cursor The new cursor to be used

    Also try for below method if it's apt for your requirement.

    Set a FilterQueryProviderand pass your key to that 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 : The Cursor must include a column named _id or this class will not work see this.

    0 讨论(0)
  • 2021-01-05 08:17

    If CursorDemo extends CursorAdapter, then you have to use adapter.swapCursor(cursor_update);

    That should swap the old cursor out for the new one and reload the data. With swapCursor, the old cursor is not closed.

    0 讨论(0)
  • 2021-01-05 08:29
    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);
    
    0 讨论(0)
提交回复
热议问题