0

我知道有很多问题询问关于刷新列表时返回最后滚动的位置。但是我不知道为什么在我的情况下(适配器)给定的答案不起作用。我有一个适配器,在给定时间它会刷新新信息并将其加载到适配器中。我想要的是,当它刷新时不要再回到适配器的顶部并保存以前的状态。这是我使用的代码。

创建时

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

asynctask 分为两部分。检索信息并将数据适配到适配器中。这是适应它的代码:

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



        }
    });

}

但是我应该如何保存适配器的状态,然后考虑之前的状态开始显示项目。

谢谢

编辑:我已经尝试过在返回 ListView 时保持/保存/恢复滚动位置的答案,但我无法使其工作。

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



    }
}
4

2 回答 2

0

你可以这样使用

getListView().setSelection(position); 

ListView 的方法。

您可以从传递给适配器的列表长度中获取“位置”。

于 2013-10-04T05:43:26.603 回答
0

声明您在全球范围内查看列表,然后您可以保留新数据调用之前的最后一个位置。之后,您可以调用 listview 进行位置选择。

于 2013-10-04T07:17:18.503 回答