Question

I have a question. I am programming a application to add records to a Content Provider. The content provider is working perfectly. My problem is when I tried to insert some data into the listview. It inserts, but I'm not able to see the data(Except that I restart the app). I was calling notifydatasetchanged from the adapter but it didn't work.

public class MainActivity extends Activity implements

    LoaderManager.LoaderCallbacks<Cursor> {
        ListView listContacts;
        ListView listView;
        Button addContactButton;
        MyAdapter adapter;
        ContentValues values;
        ContentResolver contentResolver;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            contentResolver = getContentResolver();
            listView = (ListView) findViewById(R.id.listView1);
            values = new ContentValues();
            getLoaderManager().initLoader(0, null, this);
            addContactButton = (Button) findViewById(R.id.button1);
            addContactButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    final Dialog dialogForm = new Dialog(v.getContext());
                    dialogForm.setTitle("Add a new Address");
                    dialogForm.setContentView(R.layout.dialog);
                    dialogForm.show();
                    Button saveButton = (Button) dialogForm
                            .findViewById(R.id.saveButton);
                    final EditText addressEditText = (EditText) dialogForm
                            .findViewById(R.id.addressEditText);
                    saveButton.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            try {
                                values.put(Contract.DATA, addressEditText.getText()
                                        .toString());
                                contentResolver.insert(
                                        Contract.CONTENT_URI, values);
                                values.clear();
                                dialogForm.dismiss();

                            } catch (Exception ex) {
                                ex.printStackTrace();
                            }
                        }
                    });
                    ((MyAdapter)listView.getAdapter()).notifyDataSetChanged() ;
                }

            });
        }

        @Override
        public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
            return new CursorLoader(this, Contract.CONTENT_URI, null, null, null,
                    null);
        }

        @Override
        public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
            adapter = new MyAdapter(this, R.layout.list_item, cursor, 0);
            listView.setAdapter(adapter);
            adapter.swapCursor(cursor);

        }

        @Override
        public void onLoaderReset(Loader<Cursor> loader) {
            adapter.swapCursor(null);

        }

    }

Can someone give me a clue about this problem? Also, I have another question. Is efficient the implementation of onLoadFinished method?

Greetings

Was it helpful?

Solution

You need to call restartLoader to refresh the data inside the listview.

http://developer.android.com/reference/android/app/LoaderManager.html

when you make changes to ContentProvider data, you must call getLoaderManager().restartLoader().It will discard any old data and onCreateLoader will be called again which will in turn fetch updated records and ListView will be updated.

Below is nice tutorial on Loaders.

http://www.grokkingandroid.com/using-loaders-in-android/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top