0

如何在以下示例中保存我的列表视图实例?请帮帮我。我不知道在哪里以及如何添加 onSaveInstance 方法以及如何调用它。

public class Shows1 extends Activity {


ArrayList<Actors> actorsList;

String url;

ActorAdapter adapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    setContentView(R.layout.main);

    Intent intent=getIntent();
    int pos=intent.getIntExtra("pos",0);
    int text=intent.getIntExtra("Tag",0);
    if(pos==0) {
        url = "http://indian-television-guide.appspot.com/indian_television_guide?channel=axn&date=" + text;
    }
   //other items of the list view

    actorsList = new ArrayList<Actors>();
    new JSONAsyncTask().execute(url);

    ListView listview = (ListView) findViewById(R.id.list);
    adapter = new ActorAdapter(getApplicationContext(), R.layout.row1, actorsList);

    listview.setAdapter(adapter);


    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                                long id) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), actorsList.get(position).getName(), Toast.LENGTH_LONG).show();
        }
    });
}


class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {

    ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(Shows1.this);
        dialog.setMessage("Loading, please wait");
        dialog.show();
        dialog.setCancelable(false);
    }

    @Override
    protected Boolean doInBackground(String... urls) {
        try {

            //------------------>>
            HttpGet httppost = new HttpGet(urls[0]);
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = httpclient.execute(httppost);

            // StatusLine stat = response.getStatusLine();
            int status = response.getStatusLine().getStatusCode();

            if (status == 200) {
                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity);


                JSONObject jsono = new JSONObject(data);
                JSONArray jarray = jsono.getJSONArray("listOfShows");

                for (int i = 0; i < jarray.length(); i++) {
                    JSONObject object = jarray.getJSONObject(i);

                    Actors actor = new Actors();

                    actor.setName(object.getString("showTitle"));
                    actor.setDescription(object.getString("showTime"));

                    actor.setImage(object.getString("showThumb"));

                    actorsList.add(actor);
                }
                return true;
            }

            //------------------>>

        } catch (ParseException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }

    protected void onPostExecute(Boolean result) {
        dialog.cancel();
        adapter.notifyDataSetChanged();
        if(result == false)
            Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();

    }
}

我尝试检查 onCreate 方法中是否 savedInstance !=null。但应用程序崩溃了。

4

1 回答 1

0

您可以在调用 onSaveInstanceState 时保存数据。并且,您可以在调用 onRestoreInstanceState 或调用 onCreate 时恢复它。

在下面的代码中,我使用 Gson 将列表编组为字符串。查看有关 Gson 的详细示例:http ://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    String savedList = savedInstanceState.getString("actorsList");  
    java.lang.reflect.Type listType = new TypeToken<List<Actors>>() {
    }.getType();
    actorsList = gson.fromJson(savedList, listType);    
    super.onRestoreInstanceState(savedInstanceState);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    Gson gson = new Gson();
    outState.putStringArrayList("actorsList", gson.toJson(actorsList));
    super.onSaveInstanceState(outState);
}
于 2016-01-14T04:42:13.370 回答