0

I know there are many questions asking about returning to the last position scrolled when the list has been refreshed. However I don't know why in my case (Adapter) the given answers don't work. I have an adapter where at a given time it refreshes with new info and loads it in the adapter. What I want is that when it refreshes not come again to the top of the adapter and save the previous state. Here is the code I use.

OnCreate

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.all_candidatos);
        if (Titulacion.IsReachable1(getApplicationContext())){

                new CargarCandidatos().execute();
        timer();


            }else{
                Toast.makeText(getApplicationContext(), R.string.errorserver, Toast.LENGTH_LONG).show();
            }


        setListAdapter(adapter);

        candidatosList = new ArrayList<HashMap<String, String>>();

The asynctask is divided in 2 parts. The retrieval of information and adapting the data into the adapter. Here is the code of adapting it:

protected void onPostExecute(String file_url) {
    runOnUiThread(new Runnable() {
        public void run() {

            adapter = new SimpleAdapter(
                    Monitorizacion.this, candidatosList,
                    R.layout.list_item, new String[] { TAG_ID,TAG_NSERIE,TAG_TABLET,
                            TAG_DNI, TAG_NOMBRE, TAG_TEST, TAG_PREGUNTA, TAG_BATERIA,TAG_CORRECTAS, TAG_ERRORES},
                    new int[] { R.id.autoid,R.id.id,R.id.tablet, R.id.dni, R.id.nombre, R.id.test, R.id.pregunta, R.id.bateria, R.id.correctas, R.id.fallos});
            adapter.notifyDataSetChanged();
            setListAdapter(adapter);



        }
    });

}

But how should I save the state of the adapter and then start showing the items considering the previous state.

Thank you

Edit: I have tried the answer approbed here Maintain/Save/Restore scroll position when returning to a ListView, but I cannot make it work.

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.all_candidatos);
        if (Titulacion.IsReachable1(getApplicationContext())){

                new CargarCandidatos().execute();
        timer();


            }else{
                Toast.makeText(getApplicationContext(), R.string.errorserver, Toast.LENGTH_LONG).show();
            }


        setListAdapter(adapter);

        candidatosList = new ArrayList<HashMap<String, String>>();

    lv = (ListView)findViewById(android.R.id.list);


        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                String idd = ((TextView) view.findViewById(R.id.dni)).getText()
                        .toString();

                Intent in = new Intent(getApplicationContext(),
                        MonitDetail.class);
                in.putExtra("idd", idd);

                startActivityForResult(in, 100);
            }
        });

    }



//
//  
    public void timer(){
         new CountDownTimer(tiempo, 1000) {

                public void onTick(long millisUntilFinished) {

                }

                public void onFinish() {
                    index = lv.getFirstVisiblePosition();
                    v = lv.getChildAt(0);
                    top = (v == null) ? 0 : v.getTop();
                      if (Titulacion.IsReachable1(getApplicationContext())){

                          new CargarCandidatos().execute();
                  timer();


                      }else{
                          Toast.makeText(getApplicationContext(), R.string.errorserver, Toast.LENGTH_LONG).show();
                      }
             }
         }.start();}




    class CargarCandidatos extends AsyncTask<String, Void, String> {





        protected String doInBackground(String... args) {




                try {
                    monitorizar();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();}
                return null;




        }


        protected void onPostExecute(String file_url) {
            runOnUiThread(new Runnable() {
                public void run() {

                    adapter = new SimpleAdapter(
                            Monitorizacion.this, candidatosList,
                            R.layout.list_item, new String[] { TAG_ID,TAG_NSERIE,TAG_TABLET,
                                    TAG_DNI, TAG_NOMBRE, TAG_TEST, TAG_PREGUNTA, TAG_BATERIA,TAG_CORRECTAS, TAG_ERRORES},
                            new int[] { R.id.autoid,R.id.id,R.id.tablet, R.id.dni, R.id.nombre, R.id.test, R.id.pregunta, R.id.bateria, R.id.correctas, R.id.fallos});
                    lv.setSelectionFromTop(index, top);

                    adapter.notifyDataSetChanged();

                    lv.setAdapter(adapter);





                }
            });

        }

    }
    public void monitorizar() throws Exception{
        try {
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("fecha",Titulacion.fecha()));

            JSONObject json = jParser.makeHttpRequest(url_candidatos, "GET", params);
            ArrayList<HashMap<String, String>> temp;
            temp = new ArrayList<HashMap<String, String>>();


            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {

                candidatos = json.getJSONArray(TAG_CANDIDATOS);

                for (int i = 0; i < candidatos.length(); i++) {
                    JSONObject c = candidatos.getJSONObject(i);
                    String id = c.getString(TAG_ID);
                    String nserie = c.getString(TAG_NSERIE);
                    String tablet = c.getString(TAG_TABLET);
                    String dni = c.getString(TAG_DNI);
                    String nombre = c.getString(TAG_NOMBRE);
                    String test = c.getString(TAG_TEST);
                    String pregunta = c.getString(TAG_PREGUNTA);
                    String bateria = c.getString(TAG_BATERIA);
                    String correctas = c.getString(TAG_CORRECTAS);
                    String errores = c.getString(TAG_ERRORES);

                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put(TAG_ID, id);
                    map.put(TAG_NSERIE, nserie);
                    map.put(TAG_TABLET, tablet);
                    map.put(TAG_DNI, dni);
                    map.put(TAG_NOMBRE, nombre);
                    map.put(TAG_TEST, test);
                    map.put(TAG_PREGUNTA, pregunta);
                    map.put(TAG_BATERIA, bateria);
                    map.put(TAG_CORRECTAS, correctas);
                    map.put(TAG_ERRORES, errores);
                    temp.add(map);
                    candidatosList = temp;
                }
            } 
        } catch (JSONException e) {
            e.printStackTrace();
        }



    }
}
Community
  • 1
  • 1
Katherine99
  • 980
  • 4
  • 21
  • 40

2 Answers2

0

You can use like this

getListView().setSelection(position); 

method of your ListView.

where you can get 'position' from the length of list you are passing to the adapter.

Jitender Dev
  • 6,907
  • 2
  • 24
  • 35
0

Declare you listview Globally and then you can keep the last position before New-Data-call. After then you can call listview for a position selection.

Suresh Sharma
  • 1,826
  • 22
  • 41
  • I use this when the timer finishes selection = lv.getSelectedItemPosition(); and I use lv.setSelection(selection); adapter.notifyDataSetChanged(); lv.setAdapter(adapter); after refreshing the data, but the Listview returns to the first position. – Katherine99 Oct 04 '13 at 07:29