2014-12-03 92 views
0

我使用的是使用androidplot庫繪製的兩個圖表的Listview。每當我有新的傳入數據時,我都不知道如何不斷更新列表視圖。如何使用androidplot刷新動態數據圖表的列表視圖

我有一種方法從設備不斷接收隨機數據。我必須用這個新數據更新這兩個圖。我使用由Array適配器構建的自定義視圖適配器,如下所示。

class MyViewAdapter extends ArrayAdapter<View> { 
    public MyViewAdapter(Context context, int resId, List<View> views) { 
     super(context, resId, views); 
    } 


    @Override 
    public int getCount() { 
     return 2; 
    } 

    @Override 
    public View getView(int pos, View convertView, ViewGroup parent) { 
     LayoutInflater inf = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     View v = convertView; 
     if (v == null) { 
      v = inf.inflate(R.layout.listview_example_item, parent, false); 
     } 

     p = (XYPlot) v.findViewById(R.id.xyplot); 

     Random generator = new Random(); 
     p.setTitle("plot" + pos); 

     for (int k = 0; k < NUM_SERIES_PER_PLOT; k++) { 

      double rl = Math.random(); 
      double gl = Math.random(); 
      double bl = Math.random(); 

      double rp = Math.random(); 
      double gp = Math.random(); 
      double bp = Math.random(); 
      if(setArrayValues != null){ 
      if(pos == 0) { 
       XYSeries series = new SimpleXYSeries(setArrayValues.getSeries1Numbers(), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "RPM"); 
       //XYSeries series = new SimpleXYSeries(nums, SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "RPM"); 
       p.addSeries(series, new LineAndPointFormatter(
         Color.rgb(new Double(rl * 255).intValue(), new Double(gl * 255).intValue(), new Double(bl * 255).intValue()), 
         Color.rgb(new Double(rp * 255).intValue(), new Double(gp * 255).intValue(), new Double(bp * 255).intValue()), 
         null, null)); 
      } 
      else{ 
       XYSeries series = new SimpleXYSeries(setArrayValues.getSeries2Numbers(), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "SPEED"); 
       //XYSeries series = new SimpleXYSeries(nums, SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "SPEED"); 
       p.addSeries(series, new LineAndPointFormatter(
         Color.rgb(new Double(rl * 255).intValue(), new Double(gl * 255).intValue(), new Double(bl * 255).intValue()), 
         Color.rgb(new Double(rp * 255).intValue(), new Double(gp * 255).intValue(), new Double(bp * 255).intValue()), 
         null, null)); 
      } 
     } 
     } 
     p.redraw(); 



     return v; 
    } 
} 

以下方法從設備接收數據並更新相關的Arraylists。

public void receivePackage() throws SocketException { 

    new Thread() { 

     byte[] packet = new byte[64]; 
     DatagramPacket data = new DatagramPacket(packet, 
       packet.length); 
     DatagramSocket socket = new DatagramSocket(null); 

     JSONObject json; 

     @Override 
     public void run() { 
      try { 
       isRunning = true; 
       socket.setReuseAddress(true); 
       socket.bind(new InetSocketAddress(50009)); 
      } catch (SocketException e1) { 
       e1.printStackTrace(); 
      } 
      try { 

       while (isRunning) { 

        socket.receive(data); 

        final String string = new String(
          data.getData(), 0, 
          data.getLength(), "UTF-8"); 

        json = new JSONObject(string); 

        runOnUiThread(new Runnable() { 

         @Override 
         public void run() { 
          try {         
           int wiper = json.getInt("wiper"); 
           int speed = json.getInt("speed"); 
           speed = speed + 2000; 

           setArrayValues.addValues(wiper,speed); 
           lv.invalidateViews(); 

          } catch (JSONException e) { 
           e.printStackTrace(); 
          } 
         } 
        }); 
       } 
      } catch (UnsupportedEncodingException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      socket.close(); // keep this out of while 
     } 
    }.start(); 
} 

其實我無法繪製圖中的數據點。我已經使用

setArrayValues.addValues(wiper,speed); 

我有一個單獨的輔助類設置值,這是我在該行

XYSeries series = new SimpleXYSeries(setArrayValues.getSeries1Numbers(), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "RPM"); 

XYSeries series = new SimpleXYSeries(setArrayValues.getSeries2Numbers(), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "RPM"); 

使用,但值會不會在情節顯示兩個不同的ArrayList。你能爲此提出任何解決方案嗎?

+0

看看notifyDataSetChanged方法 - > http://developer.android.com/reference/android/widget/BaseAdapter.html#notifyDataSetChanged() – 2014-12-03 14:48:31

回答

0

notifyDatasetChanged將重新觸發您的可見視圖的getView。 請注意,在設置新數據之前,您必須重置圖形。 這不會是有效的壽。 如果你只有兩張圖,你爲什麼使用listView?帶有線性佈局的ScrollView將更容易管理。

+0

我現在在圖中顯示數據點時出現了一些問題。我編輯了這個問題。你可以看看它。 – Shiva 2014-12-03 17:30:36

0

沒有看到你所有的代碼,我猜想以下情況之一。

1)您從不初始化setArrayValues,因此setArrayValues == null,因此係列永遠不會創建。

2)您沒有在適配器上調用notifyDataSetChanged。例如,如果您希望圖形重複更新,那麼您需要在適配器上調用notifyDataSetChanged,這會觸發getView()的調用,您將在其中創建圖。

該問題似乎超出了您所顯示的代碼範圍。有些事情沒有做好。它很難甚至告訴你是否實際設置了一個適配器。例如,似乎設置代碼的最簡單方法是創建一個ArrayAdapter,然後調用notifyDataSetChanged。看起來你正在試圖通過使用lv.invalidateViews();來完成同樣的事情,在那裏我猜測lv代表ListView。如果是這樣的話,那就是問題所在......你需要爲listView提供一些類型的適配器,而且好像你沒有。嘗試設置一個arrayAdapter,然後代替lv.invalidateViews();,使用

myAdapter.notifyDataSetChanged();