RecyclerView活动在返回后被杀死

时间:2015-11-28 10:34:57

标签: java android json

我已经放了一个Recyclerview,它显示了一些包含文字和图像的卡片。这些卡是可点击的,并将用户引导到另一个活动以查看卡内容。但是当用户返回搜索活动时,recyclerview会删除这些项目。它显示没有找到项目,因为如果异步任务返回结果无效,我已经放置了文本No item found。

提前致谢。

我已经包含以下代码:

 @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

1 个答案:

答案 0 :(得分:0)

如果我的未成年人是正确的,有两个活动:

活动A - &gt; RecyclerView显示搜索结果

活动B - &gt;每个recyclerview项目的详细信息

当您从活动B返回活动A时,您的活动B已被销毁。

因此,您需要在活动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/