在我的片段中,我有一个refresh()
方法,在使用AsyncDataClass extends AsyncTask
从服务器获取数据后,会在特定片段中加载RecyclerView。在我的RecycleView适配器中,我使用ContextMenu
从中调用片段的测试函数,负责执行从远程服务器数据库中删除特定项目的另一个AsyncDeleteClass extends AsyncTask
。
我的问题是我在refresh()
PostExecute
方法中调用了AsyncDeleteClass
方法,因为它获得了删除确认。但即使删除了特定项目,我的RecyclerView
也不会刷新。
OnCreateView和ReshDisplay():
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/* Allow activity to show indeterminate progress-bar */
view = inflater.inflate(R.layout.user_plcaes_card_list,container,false);
ctx=inflater.getContext();
//Get Calling Activity an call its function
MainActivity activity = (MainActivity) getActivity();
user_id = activity.getUserId();
// Toast.makeText(activity,user_id,Toast.LENGTH_LONG).show();
RefreshDisplay();
return view;
}
private void RefreshDisplay(){
recList = (RecyclerView) view.findViewById(R.id.cardList);
recList.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(ctx);
llm.setOrientation(LinearLayoutManager.VERTICAL);
recList.setLayoutManager(llm);
AsyncDataClass asyncRequestObject = new AsyncDataClass();
asyncRequestObject.execute(serverurl);
}
test()方法负责删除和刷新RecyclerView
public void test(String dpid){
deletePid = dpid;
AsyncDeleteClass asyncDeleteRequestObject = new AsyncDeleteClass();
asyncDeleteRequestObject.execute(deleteServerUrl);
}
AsyncDeleteClass:
private class AsyncDeleteClass extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String responseBody = "";
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
HttpConnectionParams.setSoTimeout(httpParameters, 5000);
HttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpPost httpPost = new HttpPost(params[0]);
String jsonResult = "";
try {
HttpResponse response = httpClient.execute(httpPost);
jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
System.out.println("Returned Json object " + jsonResult.toString());
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return jsonResult;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
Log.i("Async : ","PRe Exec");
pDialog = new ProgressDialog(ctx);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
System.out.println("Resulted Value: " + result);
if(result.equals("") || result == null){
Toast.makeText(ctx, "Server connection failed", Toast.LENGTH_LONG).show();
}
int jsonResult = returnParsedJsonObject(result);
if(jsonResult == -1){
Toast.makeText(ctx, "Sorry Unable To Delete", Toast.LENGTH_LONG).show();
}
if(jsonResult == 1){
Toast.makeText(ctx, "Place Deleted Successfully", Toast.LENGTH_LONG).show();
RefreshDisplay();
}
if (pDialog.isShowing())
pDialog.dismiss();
}
private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
try {
while ((rLine = br.readLine()) != null) {
answer.append(rLine);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return answer;
}
}
AsyncDataClass:
private class AsyncDataClass extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String responseBody = "";
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
HttpConnectionParams.setSoTimeout(httpParameters, 5000);
HttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpPost httpPost = new HttpPost(params[0]);
String jsonResult = "";
try {
HttpResponse response = httpClient.execute(httpPost);
jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
System.out.println("Returned Json object " + jsonResult.toString());
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return jsonResult;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
Log.i("Async : ","PRe Exec");
pDialog = new ProgressDialog(ctx);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
System.out.println("Resulted Value: " + result);
if(result.equals("") || result == null){
Toast.makeText(ctx, "Server connection failed", Toast.LENGTH_LONG).show();
}
int jsonResult = returnParsedJsonObject(result);
if(jsonResult == -1){
Toast.makeText(ctx, "Sorry No Places Found", Toast.LENGTH_LONG).show();
}
if(jsonResult == 1){
//Toast.makeText(ctx, "", Toast.LENGTH_LONG).show();
try {
JSONObject jsonObj = new JSONObject(result);
// Getting JSON Array node
places = jsonObj.getJSONArray("result");
// looping through All Contacts
for (int i = 0; i < places.length(); i++) {
JSONObject place = places.getJSONObject(i);
// String slot = c.getString(TAG_SLOT);
// serverReturn.add(slot);
UserPlaces placeFroServer = new UserPlaces();
placeFroServer.setId(place.getString(TAG_PID));
placeFroServer.setName(place.getString(TAG_PNAME));
placeFroServer.setDescription(place.getString(TAG_DESC));
placeFroServer.setImg(place.getString(TAG_IMG));
placeFroServer.setLat(place.getString(TAG_LAT));
placeFroServer.setLng(place.getString(TAG_LNG));
placesList.add(placeFroServer);
}
places_adapter = new PlacersCardAdapter(placesList,ctx, CheckPlacesFragment.this);
recList.setAdapter(places_adapter);
} catch (JSONException e) {
String ex = e.getMessage();
e.printStackTrace();
}
}
if (pDialog.isShowing())
pDialog.dismiss();
}
private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
try {
while ((rLine = br.readLine()) != null) {
answer.append(rLine);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return answer;
}
}