Android Retry / Cancel Dialog

I have the following problem. My main activity is a ListView populated with data received from a web service. When the main action is first downloaded, if the data cannot be obtained from the Internet, I want to display a dialog with two buttons "Repeat" and "Cancel". If the user clicks Reply, the reload data method is called, and if there is any exception, the dialog should appear again.

I have not yet found a way to realize my desired behavior.

@Override
protected void onResume() {
   super.onResume();
   Log.i("ItemListActivity", "onResume()");
   if(isNewInstance) {
      reloadItems();
      isNewInstance = false;
   }
}

private void reloadItems() {

   try {
       itemService.reloadItems();
       items = itemService.getItemList();
       listAdapter.notifyDataSetChanged();  
   } catch (Exception e) {
       showDialog(RETRY_DIALOG);                
   }

}

protected Dialog onCreateDialog(int id) {
   switch(id) {
      case RETRY_DIALOG:
         return new AlertDialog.Builder(this).setTitle(R.string.retryDialog_title)
         .setPositiveButton(R.string.retryDialog_retry, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    reloadItems();
                }


            })
            .setNegativeButton(R.string.retryDialog_quit, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
            })
            .create();
    }

    return null;
}

reloadItems() onResume() , . "", reloadItems() onclicklistener, , , onclicklistener , reloadItems() .

reloadItems() onClick(), . reloadItems() onResume() , .

, , , showDialog (RETRY_DIALOG), reloadItems().

, , . , "" . Android, .

+3
1

reloadItems ASyncTask.

- UI THREAD.

, , . , , .

, , , NEW INSTANCE (, onClick Retry. .

+5

Source: https://habr.com/ru/post/1753525/


All Articles