我已经坚持了一段时间了,所以我终于找到了一些提示。我的应用程序的主要活动在onCreate上执行Volley GET请求,我的ListView项目显示正常。单击列表中的项目后,我将一些数据传递给另一个活动,然后我执行一个Volley POST请求来更新我的数据库。然后我被带回我的主要活动,其中先前选择的ListView项目应该立即更新(从列表中消失或更改TextViews等),但如果我选择另一个项目并重复该过程,它似乎是一个循环。
我一直用onResume测试各种各样的东西,但我似乎无法让它工作。我甚至将整个凌空请求放在一个方法中,并尝试将其调用onResume,但这会导致我的进度对话框挂起并复制列表中的所有内容。但是,通过工具栏中的方法调用的列表的手动刷新工作正常,以及来自定时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();
}
更新 - 解决方案
所以我意识到我正在使用两个不同的请求。一个用我的AppController类进行立即更新,另一个看起来要等到UI线程(?)上的另一个事件发生:
// Correct one to use
AppController.getInstance().addToRequestQueue(userReq);
//Delayed
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
答案 0 :(得分:0)
您应该使用startActivityForResult调用触发post请求的Activity:
int POST_ACTIVITY = 1;
Intent i = new Intent(this, PostActivity.class);
startActivityForResult(i, POST_ACTIVITY);
在您的帖子活动中,一旦您知道请求发送到服务器且没有错误,您可以调用下一个方法来完成它并返回listview活动:
private void finishAndSetResult() {
if (getParent() == null) {
setResult(RESULT_OK, null);
} else {
getParent().setResult(RESULT_OK, null);
}
finish();
}
在listview活动中再次返回,覆盖onActivityResult:
方法@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == POST_ACTIVITY && resultCode == RESULT_OK) {
//Clear any data in your listview, relaunch the get request
//and populate it again
}
}
答案 1 :(得分:0)
想出来,在原帖中更新。事实证明我不需要startActivityForResult()或类似的建议。感谢您试图帮助FerDensetsu:)