0

i have a MainActivity with a list view. in this activity is a private void showList() which query all of my database and show it in the list view.

i have another public class "Sync" which send an http post to my MYSQL Database, get the result and write this into the android database.

i would like to realize, that on each resume of the main activity, the public class Sync will call and on finish the list view in the main activity should be refresh.

in the MainActivity, i call it like this:

@Override
public void onResume() {
   super.onResume();
   new Sync.SyncMYSQL(context).execute();
}

this is the Sync Class:

public class Sync {


        // MYSQL => Database
        static class SyncMYSQL extends AsyncTask<String, String, String> {

            Context context;

            public SyncMYSQL(Context c) {
                this.context = c;
            }


            @Override
            protected String doInBackground(String... params) {
                try {

                       // DO SOMETHING


                } catch (MalformedURLException e) {
                    Log.e("-->", Log.getStackTraceString(e));
                } catch (IOException e) {
                    Log.e("-->", Log.getStackTraceString(e));
                }
                return null;
            }


            protected void onPostExecute(String result) {

                // DO SOMETHING

            }
        }

MainActivity

public class OverviewData extends Fragment {

    private View FragementView;
    DatabaseHandler dbHandler;
    ListView ListView;
    DatabaseListAdapter databaseListAdapter;
    Context context;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        context = getContext();

        FragementView = inflater.inflate(R.layout.overview, container, false);
        ListView = (ListView) FragementView.findViewById(R.id.listView);

        dbHandler = new DatabaseHandler(getActivity());

        showList();

        return FragementView;
    }


    @Override
    public void onResume() {
        super.onResume();
        new Sync.SyncMYSQL(context, 0).execute();
    }




    private void showList() {

        ArrayList<DatabaseListItems> databaseList = new ArrayList<>();
        databaseList.clear();

        String query = "SELECT * FROM " + DatabaseHelper.DATABASE_TABLE;

        Cursor c1 = dbHandler.selectQuery(query);
        if (c1 != null && c1.getCount() != 0) {
            if (c1.moveToFirst()) {
                do {
                    DatabaseListItems databaseListItems = new DatabaseListItems();

                    databaseListItems.setIdentifier(c1.getString(c1.getColumnIndex(DatabaseHelper.COLUMN_ID)));

                    databaseList.add(databaseListItems);

                } while (c1.moveToNext()) ;
            }
            c1.close();

        }


        databaseListAdapter = new DatabaseListAdapter(getActivity(), databaseList);
        ListView.setAdapter(databaseListAdapter);
    }
}
Stack108
  • 915
  • 2
  • 14
  • 35

3 Answers3

0

For the listView adapter -

firstly set the data like

adapter=new Adapter(context,newDataList);

adapter.notifyDatasetChanged();

  • mmmh, maybe i have to say that i use a custom list view adapter. i will post the showList Function in my first post now – Stack108 May 27 '16 at 08:42
  • I am talking about custom list view adapter only! make a constructor in adapter class to receive dataList. – Meghal Agrawal May 27 '16 at 08:45
  • sry, but i dont know what i have to do, where should i put your code? int the post onPostExecute of the Sync class? – Stack108 May 27 '16 at 08:51
0

its bad idea to refresh list data every time in onResume(because, its load data whenever you open and close activity, or come back from last activity) instead of that call sync method in onStart/onCreate(at end of onCreate).


To refresh listview use interface. implement interface in activity and add below code in interface method after that call interface method from onPostExecute

if(databaseListAdapter != null)
databaseListAdapter.notifyDataSetChanged();
Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
0

Use a cursor loader. As soon as you database changes the cursor loader will call onLoadFinished() in which you can update your list view.

Initialize a loader:

getLoaderManager().initLoader(1, null, this);

In your onResume() restart it:

getLoaderManager().restartLoader(1, null, this);

In onDetach()/onDestroy() destroy it:

getLoaderManager().destroyLoader(1);

Make your class implement: LoaderManager.LoaderCallbacks And you will have to override its three main methods. onLoadFinished() will help you yo update the UI/list view.

Kaveesh Kanwal
  • 1,753
  • 17
  • 16