[英]RecyclerView Activity getting killed after Returning
我已經放了一個 recyclerview,它顯示了一些包含文本和圖像的卡片。 卡片是可點擊的,並將用戶引導至另一個活動以查看卡片內容。 但是當用戶返回搜索活動時,recyclerview 會刪除這些項目。 它顯示未找到項目,因為如果異步任務返回結果無效,我已放置文本未找到項目。
提前致謝。
我已經包含了下面的代碼:
@Override
public void onRestart() {
super.onRestart();
if(!cd.isConnectingToInternet()){
alert.showAlertDialog(pgrentertabsearch.this,
"No Internet Connection",
"Please connect to Internet", false);
// stop executing code by return
return;
}
else {
search(areavalue, pricevalue, numberofbedsvalue);
}
}
private void search(final String searcharea, final String searchprice, final String searchnumberofbeds) {
class PGAsync extends AsyncTask<String, Void, String> {
private Dialog loadingDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
spinner.setVisibility(View.VISIBLE);
loading.setVisibility(View.VISIBLE);
}
@Override
protected String doInBackground(String... params) {
String searcharea = params[0];
String searchprice = params[1];
String searchnumberofbeds = params[2];
InputStream is = null;
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("searcharea", searcharea));
nameValuePairs.add(new BasicNameValuePair("searchprice", searchprice));
nameValuePairs.add(new BasicNameValuePair("searchnumberofbeds", searchnumberofbeds));
String result = null;
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(
"http://xxx.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
result = sb.toString();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(String result) {
String s = jsonresult.trim();
spinner.setVisibility(View.GONE);
loading.setVisibility(View.GONE);
if (!s.equalsIgnoreCase("invalid")) {
found.setVisibility(View.VISIBLE);
myJSON = result;
showList();
} else {
pgnotfound.setVisibility(View.VISIBLE);
pgnotfound1.setVisibility(View.VISIBLE);
pgnotfoundimage.setVisibility(View.VISIBLE);
}
}
}
PGAsync g = new PGAsync();
g.execute(searcharea, searchprice, searchnumberofbeds);
}
protected void showList() {
try {
JSONObject jsonObj = new JSONObject(myJSON);
pgarray = jsonObj.getJSONArray(TAG_RESULTS);
for(int i=0;i<pgarray.length();i++){
JSONObject c = pgarray.getJSONObject(i);
PGCardItemReturn pgcarditemreturn = new PGCardItemReturn();
pgcarditemreturn.setId(c.getString(TAG_ID));
pgcarditemreturn.setFurnishing(c.getString(TAG_FURNISHING));
pgcarditemreturn.setArea(c.getString(TAG_AREA));
pgcarditemreturn.setPrice(c.getString(TAG_PRICE));
pgcarditemreturn.setNumberofbeds(c.getString(TAG_NUMBEROFBEDS));
pgcarditemreturn.setImageUrl(c.getString(TAG_IMAGE_URL));
listPG.add(pgcarditemreturn);
}
} catch (JSONException e) {
e.printStackTrace();
}
adapter = new PGSearchCardAdapter(listPG, this);
//Adding adapter to recyclerview
recyclerView.setAdapter(adapter);
}
在 oncreate 方法中:
if(!cd.isConnectingToInternet()){
alert.showAlertDialog(pgrentertabsearch.this,
"No Internet Connection",
"Please connect to Internet", false);
// stop executing code by return
return;
}
else {
search(areavalue, pricevalue, numberofbedsvalue);
}
她是 logcat:
11-28 15:56:06.377 1894-2620/eya.eya D/skia: --- SkImageDecoder::Factory returned null
11-28 15:56:07.552 1894-2619/eya.eya D/skia: --- SkImageDecoder::Factory returned null
11-28 15:56:07.843 1894-2620/eya.eya D/skia: --- SkImageDecoder::Factory returned null
11-28 15:56:08.858 1894-1936/eya.eya W/EGL_emulation: eglSurfaceAttrib not implemented
11-28 15:56:08.858 1894-1936/eya.eya W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa3bb8020, error=EGL_SUCCESS
11-28 15:56:08.886 1894-1894/eya.eya E/RecyclerView: No adapter attached; skipping layout
11-28 15:56:08.898 1894-1894/eya.eya E/RecyclerView: No adapter attached; skipping layout
11-28 15:56:09.023 1894-1936/eya.eya D/OpenGLRenderer: endAllStagingAnimators on 0xa4349f00 (RippleDrawable) with handle 0xa3bffb50
11-28 15:56:09.233 1894-1894/eya.eya E/RecyclerView: No adapter attached; skipping layout
11-28 15:57:57.915 1894-1901/eya.eya W/art: Suspending all threads took: 5.356ms
11-28 16:02:08.480 1894-1901/eya.eya W/art: Suspending all threads took: 5.204ms
11-28 16:02:38.129 1894-1901/eya.eya W/art: Suspending all threads took: 5.316ms
如果我的理解是正確的,那么有兩個活動:
Activity A -> RecyclerView 顯示搜索結果
活動 B -> 每個回收商查看項目的詳細信息
當您從 Activity B 返回 Activity A 時,您的 Activity B 已被銷毀。
因此,您需要覆蓋 Activity A 中的 onSaveInstanceState(Bundle savedInstanceState) 和 onRestoreInstanceState(Bundle savedInstanceState),以檢索您的數據列表。
例子:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(outState);
savedInstanceState.putParcelable("key", myObject);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Object myObject = savedInstanceState.getParcelable("key");
}
小心,你的對象類必須實現 Parcelable。
您可以查看文檔: http : //developer.android.com/reference/android/os/Parcelable.html
當您單擊“類概述”時,有一個典型的 Parcelable 實現。
還有一個自動實現 Parcelable 的工具:
http://dallasgutauckis.com/2012/01/20/parcelabler-for-implementing-androids-parcelable-interface/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.