@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
switch (requestCode)
{
case REQUEST_VOICE:
if (resultCode != RESULT_OK) break;
ArrayList<String> raw = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
dialogAdapter.add(raw.get(0));
dialogAdapter.notifyDataSetChanged(); // Doesn't work?!
String res = "http://192.168.0.197/xyz.php?v=1";
res = getFromUrl(res); // Get content from internet
if (!res.startsWith("{")) res = "Error.";
else {
try {
JSONObject json = new JSONObject(res);
dialog.set(dialog.lastIndexOf(raw.get(0)), json.getString("in")); // Should update the row that was added before the call of getFromUrl()
res = json.getString("out");
} catch (Exception e) {
res = "Error";
}
}
dialogAdapter.add(res);
dialogAdapter.notifyDataSetChanged(); // Works?
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
(I’ve shortened the code above.)
The ListView (dialogAdapter is the adapter) will refresh after the function (onActivityResult) is fully executed,
but I want to append a row to it before I call getFromUrl().
Please help.
The code is working fine as it expected
The problem which occurs here is you application execution continues till here..
actually it works but you are calling another task which hangs the UI thread, here it is:
It hangs the UI thread to update itself, and after the data has been fetched
this line executed again.
and ListView updated.
Solutino: Never perform URL request on UI thread. Use
AsyncTask