how to refresh the listView using the Cursor Adapter
Asked Answered
T

6

7

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

But I am not able to figure out . How to work with Loader or changeCursor() to refresh my ListView

Below is My code of setting the CursorAdapter :

//SucessFully done here

SQLDataSore datastore = new SQLDataSore(PrintContent.this);

Cursor cursor                 = datastore.getJSONData();

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

list_View.setAdapter(cursorDemo);

My Button onClick I am updating the Value into the Database //SucessFully Done

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 ...?
                }

            }

My UpdateData Method:

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 Class

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

    }

Any Help is Appreciated... });

Trigonometry answered 19/12, 2013 at 8:28 Comment(2)
call adapter.notifyDataSetChanged()Oech
adapter.notifyDataSetChanged() worked only when adapter value changed either new value added or previously removed.In your case your cursor value is updated but adapter is not changed its value.Schaller
I
14

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.

Intersect answered 19/12, 2013 at 8:32 Comment(5)
java.lang.IllegalArgumentException: column '_id' does not exist ...althouh I have _id field in to my db tableTrigonometry
@nitesh. '_id' is case sensitive. Make sure the column name is exactly the same.Bearce
@Bearce Thks. I solved it long time before .It was not due to _id although.Trigonometry
@Intersect Swap in a new Cursor, returning the old Cursor. Unlike changeCursor(Cursor), the returned old Cursor is NOT CLOSED.Bearce
That's correct answer! I faced this issue when I replace SimpleCurcorAdapter with CurcorAddapter and this solution is working.Meilen
L
4

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.

Larios answered 19/12, 2013 at 9:30 Comment(6)
how this gonna refresh my listView and load my content from changed database the one I have UPdated...Trigonometry
any luck with adapter.notifyDataSetChanged()Larios
let me assume So you changed cursor then called notifyDataSetChanged() still your listview not get refreshed right?Larios
I tried this way: Cursor cursor_update = datastore.updateData(editTextValue); final CursorDemo cursorDemo = new CursorDemo(PrintContent.this, cursor_update); cursorDemo.notifyDataSetChanged();Trigonometry
you may find this helpful please check this linkLarios
according to above code you your listview does know about the new CusrorDemo that you have created try to set it with list view and try again. I recommend plz follow the link that I providedLarios
S
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);
            }

        }

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.

Shaniqua answered 12/10, 2015 at 3:53 Comment(0)
I
0

you could call

adapter.notifyDataSetChanged();
Impatiens answered 19/12, 2013 at 8:30 Comment(2)
sth evil would be super.onCreate(null);Impatiens
Nope...java.lang.IllegalArgumentException: column '_id' does not exist ...althouh I have _id field in to my db table –Trigonometry
S
0

".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

Selfdetermination answered 19/12, 2013 at 9:26 Comment(1)
thks I figured it out ...the Value was Correct but the need to move my cursor to postion first...My question is how would I refresh my ListView once I updated my Database from the CursorTrigonometry
R
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);
Rinee answered 21/4, 2014 at 7:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.