I've been stuck on this one for a while, so I'm finally looking for some hints. The Main Activity of my app performs a Volley GET request onCreate and my ListView items display fine. Upon clicking an item in the list, I pass some data to another activity where I then perform a Volley POST request which updates my DB. I am then taken back to my Main Activity where the previously selected ListView item should immediately update (disappear from the list or change TextViews, etc.), but it seems to be one cycle behind if I select another item and repeat the process.
I've been testing various things with onResume but I can't seem to get it to work. I even put the entire volley request in a method and tried to call it onResume but that causes my progress dialog to hang and duplicates everything in the list. However, a manual refresh of the list called by a method from a toolbar works fine, as well as a request from a timed runnable.
UserListActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_list);
//Setting adapter to the listView
listView =(ListView) findViewById(R.id.list);
adapter =new CustomListAdapter(this,userList);
listView.setAdapter(adapter);
// Showing progress dialog before making http request
pDialog =new ProgressDialog(this);
pDialog.setMessage("Loading...");
pDialog.show();
if(userList!=null) {
userList.clear();
}
// Creating volley request obj
JsonArrayRequest userReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
User user = new User();
user.setUserId(obj.getString("user_id"));
user.setFirstName(obj.getString("firstname"));
user.setLastName(obj.getString("lastname"));
// adding user to users array
userList.add(user);
} catch (JSONException e) {
e.printStackTrace();
}
}
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
//Passing Data to UserDetailActivity
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
int UserDetailActivity = 1;
Intent i = new Intent(UserListActivity.this, UserDetailActivity.class);
i.putExtra("user_id", userList.get(position));
i.putExtra("firstname", userList.get(position));
i.putExtra("lastname", userList.get(position));
startActivityForResult(i, UserDetailActivity);
}
});
}
// Adding request to request queue
AppController.getInstance().addToRequestQueue(userReq);
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 1 && resultCode == RESULT_OK) {
//Calling the volley method below
volleyRequest();
}
}
//Collapsed volley method, same as onCreate
public void volleyRequest() {...}
UserDetailActivity
//Update Status
private void updateStatus() {
SharedPreferences sharedPreferences = getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);
final String user_id = sharedPreferences.getString(Config.user_id,"Not Available");
final String user_status = "ONLINE";
StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.USERSTATUS_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(UserDetailActivity.this, response, Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(UserDetailActivity.this, error.getMessage(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("user_status", user_status);
params.put("user_id", user_id);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
//Calling method below
finishAndSetResult();
//Intent intent = new Intent(this, UserListActivity.class);
//startActivity(intent);
}
private void finishAndSetResult() {
if (getParent() == null) {
setResult(RESULT_OK, null);
} else {
getParent().setResult(RESULT_OK, null);
}
finish();
}
Update - Solution
So I realized I was using two different requests. One with my AppController class that does the immediate update and another that seems to wait until another event on the UI thread (?) takes place:
// Correct one to use
AppController.getInstance().addToRequestQueue(userReq);
//Delayed
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
Figured it out, update in original post. Turns out I did not need startActivityForResult() or anything like that as suggested. Thanks for trying to help FerDensetsu :)