2013-11-15 35 views
0

我對TCP套接字工作動態ListView控件的值,我收到了一些一堆數據的每一個秒,需要在的ListView,以顯示它,我使用自定義數組適配器動態的ListView如何更新的Android

我的問題是,當我從TCP套接字接收到一組新數據時,一個新對象正在爲listItem(findViewById)創建,因爲它在我滾動列表視圖時再次移動到頂部我的listView。

當我只考慮一組數據時,我沒有發現上述問題。

我的代碼:

doAsynchronousTask = new TimerTask() { 

     @Override 
     public void run() { 
      finalizer = new Runnable() { 
       public void run() { 
          try {         
           new RetriveStock().execute(); // AsyncTask Executes for every 1 sec. 
          } catch (Exception e) { 
           // TODO: handle exception 
          } 
        } 
       } 
      }; 
      handler.post(finalizer); 
     } 
    }; 

    timer.schedule(doAsynchronousTask, 0, 1000); // execute in every 1000 

//的AsyncTask

public class RetriveStock extends AsyncTask<Void, Void, ArrayList<User>> { 

    @Override 
    protected ArrayList<User> doInBackground(Void... params) { 
     message += client.clientReceive(1); // Receive data from socket and put into message (string global variable). 
     printJson(); // Here I receive data from JSON and put into object 
     adb = new RSSListAdaptor(DefaultMarketWatch.this, 
       R.layout.rssitemview, list); // "list" is a global listAdapter. 
     return null; 
    } 

    @Override 
    protected void onCancelled() { 
     super.onCancelled(); 
    } 

    protected void onPostExecute(ArrayList<User> result) { 

     lv.setAdapter(adb); 
     adb.notifyDataSetChanged(); 
     super.onPostExecute(result); 
    } 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
    } 

    @Override 
    protected void onProgressUpdate(Void... values) { 
     super.onProgressUpdate(values); 
    } 

} 

// printJson()

public void printJson() { 
    String str = ""; 

    /* SOME CODE HERE 
    FORMATING "message" (STRING VARIABLE) TO A PERFECT JSONARRAY FORMATE, AND PUT INTO VARIABLE "str" */ 
    str = "[" + str + "]"; 

    try { 
     JSONArray jsonArray = new JSONArray(str); 

     for (int i = 0; i < jsonArray.length(); i++) { 
      JSONObject json = jsonArray.getJSONObject(i); 
     User obj = new User(); 

     // MY CODE, EITHER ADDING NEW OBJECT OR UPDATING THE EXISTING USER OBJECT. 

     list.add(obj); 

    } catch (JSONException e1) { 
     e1.printStackTrace(); 
    } 
} 

//我的自定義ArrayAdapter。

public class RSSListAdaptor extends ArrayAdapter<User> { 
    private List<User> objects = null; 

    public RSSListAdaptor(Context context, int textviewid, 
      List<User> objects) { 
     super(context, textviewid, objects); 
     this.objects = objects; 
    } 

    @Override 
    public int getCount() { 
     return ((null != objects) ? objects.size() : 0); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 


    public View getView(final int position, View convertView, 
      ViewGroup parent) { 
     View view = convertView; 
     if (null == view) { 
      LayoutInflater vi = (LayoutInflater) DefaultMarketWatch.this 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      view = vi.inflate(R.layout.rssitemview, null); 

     } 

     try { 
      User u = objects.get(position); 
      if (null != u) { 

       TextView title_list = (TextView) view.findViewById(R.id.symbol); 
       TextView ltp_list = (TextView) view.findViewById(R.id.ltp); 
       TextView high_list = (TextView) view.findViewById(R.id.high); 
       TextView low_list = (TextView) view.findViewById(R.id.low); 
       TextView persend_list = (TextView) view.findViewById(R.id.persent); 

       RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) title_list 
         .getLayoutParams(); 

      params.setMargins(0, 0, 25, 0); 
      title_list.setText("TITLE"); 
      ltp_list.setText("LTP"); 
      high_list.setText("HIGH"); 
      low_list.setText("LOW"); 
      persend_list.setText("PERSENTAGE"); 

      } 
     } catch (IllegalStateException is) { 
      is.printStackTrace(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return view; 
    } 

} 

回答

1

試試這個:

// save index and top position 
int index = mList.getFirstVisiblePosition(); 
View v = mList.getChildAt(0); 
int top = (v == null) ? 0 : v.getTop(); 

// update listview with new data 

// restore 
mList.setSelectionFromTop(index, top); 

或者

// Save ListView state 
Parcelable state = listView.onSaveInstanceState(); 

// Set new items 
listView.setAdapter(adapter); 

// Restore previous state (including selected item index and scroll position) 
listView.onRestoreInstanceState(state); 
+0

太棒了!我試過你的第二個選項,它工作。謝謝。:) – user2085965

1

不要重新創建doInBackground適配器和不復位它onPostExecute

在構造函數或某處明智而是創建一個空的適配器,當數據進來,數據追加到您的適配器,然後調用notifyDataSetChanged