我使用的是使用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。你能爲此提出任何解決方案嗎?
看看notifyDataSetChanged方法 - > http://developer.android.com/reference/android/widget/BaseAdapter.html#notifyDataSetChanged() – 2014-12-03 14:48:31