0

How do I save instance of my listview in the following example? Please help me out. I don't know where and how to add the onSaveInstance method and how do I call it.

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();

    }
}

I tried checking if savedInstance !=null in onCreate method.but the app crashes.

Stanley Ko
  • 3,383
  • 3
  • 34
  • 60
Elvi T
  • 11
  • 2

1 Answers1

0

You can save your data when onSaveInstanceState was called. and, you can restore it when onRestoreInstanceState was called, Or onCreate was called.

In below code, I used Gson to marshalling list to string. See detail examples about 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);
}
Stanley Ko
  • 3,383
  • 3
  • 34
  • 60