问题描述
伙计们,我正在构建一个应用程序,该应用程序通过从mysql数组中请求的mysql值生成一些动态图.
Hey guys i am building an app which produces a dynamic graph through some values i get from mysql which are requested in json array.
我的代码是:
Intent i = this.getActivity().getIntent();
HashMap<String, String> hashMap = (HashMap<String, String>) i.getSerializableExtra("stockInfo");
name = i.getStringExtra("name");
ball = i.getStringExtra("price");
final ArrayList<String> myItems = new ArrayList<String>(hashMap.values());
myItems.remove(name);
myItems.remove(ball);
Button buttonLiveGraph = (Button)rootView.findViewById(R.id.buttonlive);
buttonLiveGraph.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(getActivity().getApplicationContext(), LiveGraph.class);
intent.putExtra("stockprices", myItems.toString());
getActivity().startActivity(intent);
}
});
在LiveGraph活动中,我得到如下意图值:
and in LiveGraph activity i get the intent values like so:
import org.achartengine.GraphicalView;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class LiveGraph extends Activity {
private static GraphicalView view;
private LiveGraphClass live = new LiveGraphClass();
private static Thread thread;
private String myPrices;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_live_graph);
Intent i = this.getIntent();
myPrices = i.getStringExtra("stockprices");
thread = new Thread(){
@Override
public void run() {
for (double i = 0; i < 15; i++) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Point p = getDataFromReceiver(i);
live.addNewPoints(p);
view.repaint();
}
}
};
thread.start();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
view = live.getView(this);
setContentView(view);
}
public Point getDataFromReceiver(double x){
return new Point(x, Double.parseDouble(myPrices));
}
}
我的LiveGraphClass活动:
My LiveGraphClass activity:
public class LiveGraphClass {
private GraphicalView view;
private TimeSeries dataset = new TimeSeries("Rain Fall");
private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
private XYSeriesRenderer renderer = new XYSeriesRenderer();
private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
public LiveGraphClass(){
mDataset.addSeries(dataset);
mRenderer.addSeriesRenderer(renderer);
renderer.setColor(Color.WHITE);
renderer.setPointStyle(PointStyle.SQUARE);
renderer.setFillPoints(true);
mRenderer.setExternalZoomEnabled(true);
mRenderer.setZoomButtonsVisible(true);
mRenderer.setBackgroundColor(Color.BLACK);
mRenderer.setApplyBackgroundColor(true);
mRenderer.setZoomEnabled(true);
mRenderer.setYTitle("Range");
mRenderer.setXTitle("Values");
}
public GraphicalView getView(Context context){
view = ChartFactory.getLineChartView(context, mDataset, mRenderer);
return view;
}
public void addNewPoints(Point p){
dataset.add(p.getX(), p.getY());
}
}
我的积分活动
public class Point {
private double x;
private double y;
public Point(double x, double y) {
super();
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
}
我得到这个错误
03-25 12:20:09.515: E/AndroidRuntime(1671): java.lang.NumberFormatException: Invalid double: "[9.32, 5.22, 10.201, 2.54, 3.214, 1.02, 0.21, 0.147, 7.01, 0.365, 8.23, 0.254, 8.89, 0.155, 4.32]"
03-25 12:20:09.515: E/AndroidRuntime(1671): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
03-25 12:20:09.515: E/AndroidRuntime(1671): at java.lang.StringToReal.parseDouble(StringToReal.java:269)
03-25 12:20:09.515: E/AndroidRuntime(1671): at java.lang.Double.parseDouble(Double.java:295)
有人可以帮我解决这个问题吗?为什么会发生这种情况,因为我通过意图发送了双打并且我得到了双打,所以我尝试生成一个双图?
Can anybody help me solve this problem?? why is that happening since i send doubles through intent and i get doubles so i try to produce a double graph??
我的php代码:
<?php
try {
$handler = new PDO(this is not available);
$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (Exception $e) {
echo $e->getMessage();
die();
}
$query = $handler->query('SELECT * FROM metoxes');
$records = array();
$records = $query->fetchAll(PDO::FETCH_ASSOC);
$json['metoxes'] = $records;
echo json_encode($json);
?>
提前谢谢!任何帮助都会受到欢迎和赞赏.
Thanks in advance! any help will be much welcomed and appreaciated.
推荐答案
您以字符串[]的形式发送Intent,但尝试像字符串一样接收它,这是不可能的.而是这样做:
You send the Intent as a string[] but you trying to receive it like as a String which is not possible. So do this instead:
像这样修改LiveGraph类:
Modify your LiveGraph class like this:
public class LiveGraph extends ActionBarActivity {
private static GraphicalView view;
private LiveGraphClass live = new LiveGraphClass();
private static Thread thread;
static String[] myPrices;
double point;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_live_graph);
Toolbar tb = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(tb);
Intent i = this.getIntent();
myPrices = i.getStringArrayExtra("stockprices");
thread = new Thread() {
@Override
public void run() {
for (int i = 0; i < myPrices.length; i++) {
try {
Thread.sleep(800);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Point p = new Point(i, Double.parseDouble(myPrices[i]));
live.addNewPoints(p);
view.repaint();
}
}
};
thread.start();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
view = live.getView(this);
setContentView(view);
}
}
希望有帮助!
这篇关于如何传递ArrayList< String>转到另一项活动并将其转换为两倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!