Here's the situation: I've got some lengthy non-UI code that needs to be run in a ListActivity and then have this ListActivity update the UI to contain a the result of this lengthy method (the list). I need a ProgressDialog to be running until it's finished so the user has some feedback.
Here's the code:
public class SolutionListActivity extends ListActivity
{
private String[] solutions;
private String letters;
private ProgressDialog dialog;
private static Solver solver;
/** Called when the activity is first created.
* #param savedInstanceState
*/
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//Get the selected letters from LettersActivity
letters = getIntent().getStringExtra("letters");
dialog = ProgressDialog.show(this, "Please Wait...",
"Searching Words...", true);
new Thread()
{
#Override
public void run()
{
if (solver == null)
{
solver = new Solver(SolutionListActivity.this);
solver.readDictionary(0);
solver.readDictionary(1);
}
solutions = solver.solve(letters);
runOnUiThread(new Runnable()
{
#Override
public void run()
{
dialog.dismiss();
//Set up a UI List
setListAdapter(new ArrayAdapter<String>(SolutionListActivity.this, R.layout.list_item, solutions));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
}
});
}
}.start();
The problem is my ProgressDialog won't dismiss and I can't be sure whether the solutions = solver.solve(letters); will be finished before the UI uses it in setListAdapter(new ArrayAdapter<String>(SolutionListActivity.this, R.layout.list_item, solutions));
Any advice you guys have would be helpful.
Thank You,
Calum
Have you tried AsyncTask? It´s built exactly for having threading AND be able to update things in your UI Thread.
Take a look here: http://developer.android.com/resources/articles/painless-threading.html
Related
What I have :
I have a SearchActivity which receives an ArrayList (NameDesSearch) from a fragment(after a button click) and it updates a simple listview. When I click a list view item a new view is appearing by the corresponding object (orgDesObj) of the clicked list item. This functionality works well.
Currently receiving list (NameDesSearch) consists of names and descriptions. All are strings.
But, I wanted to show lists names only. Hence I tried creating a function (titlefunc()).
Here a new ArrayList ( NameDesTitles ) was crated to add relevent names only.
Issue:
But, seems like Do In background function is not working by the time I call titlefunc().
Attempts:
I put several Log to capture the point.
I'm using the same function (getLocDesOb()) in the list view on item clicked as well.
Surprisingly it works, even the doInBackground function also works.
But when the search activity creates and titlefunc() is called, search list (finalODescriptionArrayList) in doInBackground is empty().
Form the Logs I receive the content of finalODescriptionArrayList as [] and size as 0.
But, when I click list view item finalODescriptionArrayList updates.
I even tried by moving NameDesSearch = getIntent().getStringArrayListExtra("searched"); outside of the function as well.
Seems like my doInBackground method is calling only when the list item clicked but not activity on creates. Every other function works well. I'm not sure by the time when why my titlefunc() is called, why finalODescriptionArrayList does not update.
I would appreciate any suggestions on this. Thank you !
My Code: I have removed Logs for clearness.
public class SearchActivity extends AppCompatActivity {
ListView searchedListView;
String SearchedWord;
private ArrayAdapter<String> orgAdapter;
ArrayList<String> NameDesSearch = new ArrayList<String>();
ArrayList<String> NameDesTitles = new ArrayList<String>();
private OService OService;
ArrayList<ODescription> finalODescriptionArrayList = new ArrayList<ODescription>();
#RequiresApi(api = Build.VERSION_CODES.N)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
searchedListView = (ListView) findViewById(R.id.searched_list_view);
new GetCourse().execute();
titlefunc();
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, NameDesTitles);
searchedListView.setAdapter(arrayAdapter);
searchedListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedItemText = parent.getItemAtPosition(position).toString();
ODescription orgDesObj = getLocDesOb(selectedItemText);
if (orgDesObj != null) {
Intent intent = new Intent(SearchActivity.this, View.class);
intent.putExtra("sOb", orgDesObj);
startActivity(intent);
}
}
});
}
#SuppressLint("StaticFieldLeak")
private class GetCourse extends AsyncTask<Void, Void, Void> {
#TargetApi(Build.VERSION_CODES.N)
#Override
protected Void doInBackground(Void... voids) {
try {
finalODescriptionArrayList = JsontoObject.jsonToObjectData(getResources().openRawResource(R.raw.newdb));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
public ODescription getLocDesOb(String selectedItemText) {
if (finalODescriptionArrayList == null) {
return null;
}
for (ODescription locDescObj : finalODescriptionArrayList) {
if (locDescObj.getName().equals(selectedItemText) || locDescObj.getDescription().equals(selectedItemText)) {
return locDescObj;
}
}
return null;
}
public void titlefunc() {
NameDesSearch = getIntent().getStringArrayListExtra("searched");
for (String searchNameDes : NameDesSearch) {
ODescription orgDesObj2 = getLocDesOb(searchNameDes);
if (orgDesObj2 != null) {
NameDesTitles.add(orgDesObj2.getName());
}
}
}
}
Attempts After Answer Below
AsyncTask update with onPostExecute. Then Since it take a little bit of time a progress bar added with onPreExecute. titlefunc() in oncreate method removed.
This method works now. But, sometimes the same issue exists. Arraylist to adapter is empty so that listview is empty. Seems like still taking lot of time to do the background task.
Updated AsyncTask
#SuppressLint("StaticFieldLeak")
private class GetCourse extends AsyncTask<Void, Void, Void> {
ProgressDialog progressDialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(SearchActivity.this);
progressDialog.setMessage("Searching");
progressDialog.setCancelable(false);
progressDialog.show();
}
#TargetApi(Build.VERSION_CODES.N)
#Override
protected Void doInBackground(Void... voids) {
try {
finalODescriptionArrayList = JsontoObject.jsonToObjectData(getResources().openRawResource(R.raw.newdb));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
titlefunc();
arrayAdapter.notifyDataSetChanged();
if (progressDialog.isShowing())
progressDialog.dismiss();
}
}
Modifed titlefunc() - to remove duplicates
Set<String > set = new HashSet<>( NameDesTitles);
NameDesTitles.clear();
NameDesTitles.addAll(set);
Your AsyncTask runs asynchronously, in the background. It will (most likely) not be finished when you call titleFunc() (which is what you are seeing).
You can fix this in many ways. One way would be to update the content of your adapter after the AsyncTask completes. You can do this in onPostExecute() of your AsyncTask which will be called when the background processing completes. In that method you can run your titleFunc() or something similar to filter the results you want to display. You then need to tell your Adapter to update the view by calling notifyDatasetChanged() on the Adapter.
I searched similar questions like this but sadly I found them really confusing and also I'm still new on using Android and Retrofit.
I have a contact list JSON here
http://api.androidhive.info/contacts/
And already working List but then I wanted to handle the Retrofit process to another class so I can just call it whenever I want. I have the MainActivity calling for the UI and the RetrofitHandler which handles the success and failure method.
Here is my Main Activity
public class MainActivity extends AppCompatActivity{
//note i just simplifiend my code a little
private List<Contacts> contacts;
public String[] itemer;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RetrofitHandler retrofitHandler = new RetrofitHandler();
itemer = retrofitHandler.getContacts(this);
if (itemer != null) {
Toast.makeText(MainActivity.this,itemer[0],Toast.LENGTH_SHORT).show();
}
}
And here is my HandlerClass
ublic class RetrofitHandler {
public String[] item;
public static final String ROOT_URL = "http://api.androidhive.info";
public List<Contacts> contacts;
public String[] getContacts(final Context context) {
final ProgressDialog loading = ProgressDialog.show(context, "Fetching Data", "Please wait...", false, false);
RestAdapter adapter = new RestAdapter.Builder().setEndpoint(ROOT_URL).build();
ContactsAPI api = adapter.create(ContactsAPI.class);
api.getContacts(new Callback<Contacts>() {
#Override
public void success(Contacts contacts, Response response) {
loading.dismiss();
MainActivity update = new MainActivity();
List<Contact> contactList = contacts.getContacts();
item = new String[contactList.size()];
for (int i = 0; i < contactList.size(); i++) {
item[i] = contactList.get(i).getName();
}
}
#Override
public void failure(RetrofitError error) {
Toast.makeText(context, "Error Occured", Toast.LENGTH_LONG).show();
}
});
return item;
}
My problem is it runs smoothly with no error on LogCat. Unfortunately the Toast on the mainactivity won't appear.
Your issue is because of asynchronous nature of the request/response. Success or Failure callbacks fires upon response, which is happen in asynchronous manner. So basically your method returns before that happens, it means your item is null. You have to use synchronous mechanism or you have to check MVP pattern which gives perfect solution for your problem. See below is an example of using synchronous call (I've not tested it, but it should work).
public List<Contacts> getContacts(){
RestAdapter adapter = new RestAdapter.Builder().setEndpoint(ROOT_URL).build();
ContactsAPI api = adapter.create(ContactsAPI.class);
return api.getContacts();
}
See this tutorial to get more idea. I suggest you to move to retrofit 2 which is much more greater than retrofit 1.
I'm trying to make a simple app that's looks for WiFi networks, and connects to them. I'm currently having a problem with updating the UI.
A few pointers would be great. Thank you for your time.
class UiUpdater extends AsyncTask<Void, Void, List<ScanResult>> {
#Override
protected void onPreExecute() {
super.onPreExecute();
TextView searching = (TextView) findViewById(R.id.searching);
searching.setText("Currently searching...");
}
#Override
protected List<ScanResult> doInBackground(Void... params) {
WifiManager manager = (WifiManager) Client.this.getSystemService(Context.WIFI_SERVICE);
if (!manager.isWifiEnabled())
manager.setWifiEnabled(true);
return manager.getScanResults();
}
#Override
protected void onPostExecute(List<ScanResult> items) {
super.onPostExecute(items);
ArrayList<Items> wifi = new ArrayList<>();
for (ScanResult s : items)
wifi.add(new Items(s.SSID, s.capabilities));
///TextView searching = (TextView) findViewById(R.id.searching);
///searching.setText("");
ListView list = (ListView) findViewById(R.id.list);
Explorer adapter = new Explorer(Client.this, R.layout.listview_item_row, wifi);
list.setAdapter(adapter);
clickListener(list, wifi);
}
}
please use Toast message or logcat or breakpoint to check onPost execute is being called or not
I managed to get it working, but by taking a different approach.
I used the BroadcastReceiver and registerReceiver to get the onReceive event to update the list.
Here's the code:
mWifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
if(!mWifiManager.isWifiEnabled())
mWifiManager.setWifiEnabled(true);
mWifiManager.startScan();
wifiReceiver = new BroadcastReceiver(){
#Override
public void onReceive(Context c, Intent intent)
{
if(mWifiManager != null) {
List<ScanResult> networks = mWifiManager.getScanResults();
showWifi(networks);
}
}
};
registerReceiver(wifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
Although I haven't managed to understand how AsyncTask works, I did get some nice results with loading a simple progress bar.
I think the problem I encountered was as such:
I started an AsyncTask on another thread.
In the doInBackground, getScanResults started another background thread, leading doInBackground to think the job's done.
onPostExecute was called because doInBackground finished its job.
Bottom line, it wasn't the AsyncTask's fault, it was mine for not knowing that getScanResults starts another background thread.
So I'm just trying to create an Alert Dialog that is just a message (no buttons or titles). I want to display an alert dialog when a background task is running. The alert dialog will run on the UI thread.
Here's what I have done so far:
protected void onPreExecute() {
super.onPreExecute();
AlertDialog altDlg;
altDlg = new AlertDialog.Builder(AlertDialogActivity.this).create();
altDlg.setMessage("Retrieving Information. Please Wait");
altDlg.show();
}
I also tried doing this:
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setMessage("Retrieve Info. Please Wait").show();
The error I am getting with the first one is:
cannot find symbol 'AlertDialogActivity'
symbol: class AlertDialogActivity
location: class com.example.Device.Activity
The second attempt error says:
incompatible types: com.example.Device.Activity cannot be converted to android.content.Context
I'm not sure what I am doing wrong in either scenario. I just want to display a basic message when a background task is running and I was hoping the closest thing I can use is AlertDialog.
EDIT for how to set up AsyncTask properly:
Small background of what I want to do. I just want to read in a file, deserialize it and save it's contents to a db.
Right now I'm assuming I only need two activities.
One is my main activity:
public class MainActivity extends Activity {
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setup);
final Button setup_button = (Button) findViewById(R.id.setup_button);
setup_button.setOnClickListener (new View.OnClickListener() {
public void onClick(View view){
setContentView(R.layout.retrieve_info);
}
});
}
}
Now the onClick event just moves to the new view that is supposed to display the message or alert dialog that says retrieving information. Please Wait. It displays the message while reading a file and saving to db. Once the file is read and saved, The message should disappear and say something like setup complete.
My second activity so far is:
public class RetrieveInfoActivity extends AsyncTask<Void,Void,Void> {
private ProgressDialog progressBar;
private void retrieveInfo(String fileName) {
try {
File file = new File(fileName);
Scanner scanner = new Scanner(file);
//Read all the lines until there are no more lines
while (scanner.hasNextLine()) {
scanner.nextLine();
//TODO: deserialize and save to db
}
scanner.close();
}
catch (FileNotFoundException e) { e.printStackTrace(); }
}
#Override
protected Void doInBackground(Void... params) {
retrieveInfo("test.txt");
return null;
}
protected void onPreExecute() {
super.onPreExecute();
progressBar.setIndeterminate(true);
progressBar.setCancelable(false);
progressBar.setMessage("Retrieve Information.Please wait");
progressBar.show();
}
#Override
protected void onPostExecute() {
progressBar.dismiss();
}
}
That's all I really have so far. I just need to understand how to set up this in Android conceptually.
Hope this makes sense.
Try this:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
Instead of using an AlertDialog use a ProgressBar, it will do the trick for you.
private ProgressDialog progressBar;
#Override
protected void onPreExecute() {
super.onPreExecute();
progressBar.setIndeterminate(true);
progressBar.setCancelable(false);
progressBar.setMessage("Your message");
progressBar.show();
}
#Override
protected void onPostExecute(final String error_code) {
progressBar.dismiss();
}
Looks like you are extending AsyncTask and trying to use it as a context. That won't work as AsyncTask itself is nothing but an abstract class.
You need to create a custom constructor for your AsyncTask to fetch the Context:
public class MyTask extends AsyncTask<Void, Void, Void> {
private Context mCtx;
public MyTask(Context context) {
mCtx = context;
}
...
Then when starting your AsyncTask, pass the context:
new MyTask(this).execute();
Another way would be to make the AsyncTask an inner class and use YourActivity.this when creating the dialog. Example:
public class YourActivity extends Activity {
...
private class MyTask extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute();
AlertDialog dialog = new AlertDialog.Builder(YourActivity.this).create();
}
#Override
protected Void doInBackground(Void... params) {
...
}
}
}
I have a little problem, I hope U can help me;)
Trouble is, that ProgressDialog show only after loading run(), but I need to show it on start and showing it while loading some data. I put: "dialog = ProgressDialog.show(CategoriesListActivity.this,"Working...","Loading data", true);" in method run(), but the same. I print in Log.i() some info (int i++) and put title of ProgressDialog. Method work correctly, but don't show ProgressDialog. I have read some info that some thread block another thread (my created), that's why doesn't show progressDialog, but can't do anything. Thx.
public void run() {
/** getting there long execution **/
handler.sendEmptyMessage(0);
}
private Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
// stop and hide dialog
dialog.dismiss();
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_
dialog = ProgressDialog.show(CategoriesListActivity.this, "Working...",
"Loading data", true);
// start new thread where get long time execution
Thread thread = new Thread(this);
thread.start();
//wait while data is loading, 'cause I need use variable from calculation
// in "EfficientAdapter" later
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
ListView l1 = (ListView) findViewById(R.id.list);
l1.setAdapter(new EfficientAdapter(this));
}
That's done with the help of AsyncTask (an intelligent backround thread) and ProgressDialog
When the AsyncTask starts we raise a progressdialog with indeterminate state, once the task is finished we dismiss the dialog.
Example code
What the adapter does in this example is not important, more important to understand that you need to use AsyncTask to display a dialog for the progress.
private class PrepareAdapter1 extends AsyncTask<Void,Void,ContactsListCursorAdapter > {
ProgressDialog dialog;
#Override
protected void onPreExecute() {
dialog = new ProgressDialog(viewContacts.this);
dialog.setMessage(getString(R.string.please_wait_while_loading));
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
/* (non-Javadoc)
* #see android.os.AsyncTask#doInBackground(Params[])
*/
#Override
protected ContactsListCursorAdapter doInBackground(Void... params) {
cur1 = objItem.getContacts();
startManagingCursor(cur1);
adapter1 = new ContactsListCursorAdapter (viewContacts.this,
R.layout.contact_for_listitem, cur1, new String[] {}, new int[] {});
return adapter1;
}
protected void onPostExecute(ContactsListCursorAdapter result) {
list.setAdapter(result);
dialog.dismiss();
}
}