2013-10-04 20 views
0

我知道有很多問題詢問有關刷新列表時返回上一個滾動位置的問題。但我不知道爲什麼在我的情況下(適配器)給出的答案不起作用。 我有一個適配器,在給定時間刷新新信息並將其加載到適配器中。我想要的是當它刷新時不再回到適配器的頂部並保存以前的狀態。 這是我使用的代碼。Android:刷新時返回適配器的最後位置

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

的的AsyncTask在2份分割。檢索信息並將數據調整到適配器中。 這裏是適應它的代碼:

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



     } 
    }); 

} 

但我應該怎麼保存適配器的狀態,然後開始展示考慮到以前的狀態的項目。

謝謝

編輯:我試圖在這裏approbed Maintain/Save/Restore scroll position when returning to a 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(); 
     } 



    } 
} 

回答

0

您可以使用這樣

getListView().setSelection(position); 

你的ListView的方法。

您可以從傳遞到適配器的列表長度中獲得'位置'。

+0

但如何檢索該位置?我沒有聲明Listview。 – Katherine99

+0

更新的答案檢查現在 –

+0

對不起,它也不起作用。更新我的問題** – Katherine99

0

聲明你的listview全局,然後你可以保持新數據調用前的最後一個位置。之後你可以調用listview進行位置選擇。

+0

當定時器完成selection = lv.getSelectedItemPosition();我使用lv.setSelection(選擇); adapter.notifyDataSetChanged(); lv.setAdapter(adapter);刷新數據後,但Listview返回到第一個位置。 – Katherine99

相關問題