我有两节课。我试图从数据库值绘制图表 但是我无法将值从一个类发送到另一个类,其中使用了数组。请仔细阅读我的代码并帮助我。
以下是存在数组值的类(现在我正在使用静态值)
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.achartengine.chartdemo.demo.chart.IDemoChart;
import org.achartengine.chartdemo.demo.chart.TemperatureChart;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class ChartDemo extends ListActivity {
double a = 10;
double pass[] = { 7, 0, 4, 8, 3, 5, 7, 6, 8, 19, 11, 4 };
Intent intent;
private IDemoChart[] mCharts = new IDemoChart[] { new TemperatureChart() };
private String[] mMenuText;
private String[] mMenuSummary;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int length = mCharts.length;
mMenuText = new String[length];
mMenuSummary = new String[length];
TemperatureChart obj=new TemperatureChart();
obj.getArray(pass);
for(int i=0;i<pass.length;i++)
{
Log.i("sanket",pass[i]+"");
}
for (int i = 0; i < length; i++) {
mMenuText[i] = mCharts[i].getName();
mMenuSummary[i] = mCharts[i].getDesc();
}
setListAdapter(new SimpleAdapter(this, getListValues(),
android.R.layout.simple_list_item_2, new String[] {
IDemoChart.NAME, IDemoChart.DESC }, new int[] {
android.R.id.text1, android.R.id.text2 }));
}
private List<Map<String, String>> getListValues() {
List<Map<String, String>> values = new ArrayList<Map<String, String>>();
int length = mMenuText.length;
for (int i = 0; i < length; i++) {
Map<String, String> v = new HashMap<String, String>();
v.put(IDemoChart.NAME, mMenuText[i]);
v.put(IDemoChart.DESC, mMenuSummary[i]);
values.add(v);
}
return values;
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// intent = new Intent(this, TemperatureChart.class);
// if (position <= mCharts.length + 1) {
intent = mCharts[position].execute(this);
// intent.putExtra("sam", a);
// Log.i("sanket",a+"");
// }
startActivity(intent);
}
}
现在这是我想要那些数组值的第二个类 问题是这个类在填充数组中的那些项之前被初始化,所以这个类中的数组是空的
package org.achartengine.chartdemo.demo.chart;
import org.achartengine.ChartFactory;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.model.RangeCategorySeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint.Align;
import android.util.Log;
/**
* Temperature demo range chart.
*/
public class TemperatureChart implements IDemoChart {
// static double b;
double[] maxValues;
public void getArray(double array1[]) {
array1 = new double[12];
this.maxValues = array1;
for(int i=0;i<array1.length;i++)
{
Log.i("sanket",maxValues[i]+"");
}
}
/**
* Returns the chart name.
*
* @return the chart name
*/
public String getName() {
return "MarkS range chart";
}
/**
* Returns the chart description.
*
* @return the chart description
*/
public String getDesc() {
return "The monthly Marks (vertical range chart)";
}
/**
* Executes the chart demo.
*
* @param context
* the context
* @return the built intent
*/
public Intent execute(Context context) {
// b = getIntent().getIntExtra("sam", 0);
// Log.i("sanket",b+"");
double[] minValues = new double[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
// double[] maxValues = new double[] { 7, 0, 4, 8, 3, 5, 7, 6, 8, 19,
// 11,
// 4 };
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
RangeCategorySeries series = new RangeCategorySeries("Marks");
int length = minValues.length;
for (int k = 0; k < length; k++) {
series.add(minValues[k], maxValues[k]);
}
dataset.addSeries(series.toXYSeries());
int[] colors = new int[] { Color.CYAN };
XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);
setChartSettings(renderer, "Monthly Marks range", "Month",
"Marks Obtained", 0.5, 12.5, 0, 20, Color.GRAY, Color.LTGRAY);
renderer.setBarSpacing(0.5);
renderer.setXLabels(0);
renderer.setYLabels(10);
renderer.addXTextLabel(1, "Jan");
renderer.addXTextLabel(3, "Mar");
renderer.addXTextLabel(5, "May");
renderer.addXTextLabel(7, "Jul");
renderer.addXTextLabel(10, "Oct");
renderer.addXTextLabel(12, "Dec");
renderer.addYTextLabel(5, "OK");
renderer.addYTextLabel(10, "GOOD");
renderer.addYTextLabel(20, "Nice");
renderer.setMargins(new int[] { 30, 70, 10, 0 });
renderer.setYLabelsAlign(Align.RIGHT);
SimpleSeriesRenderer r = renderer.getSeriesRendererAt(0);
r.setDisplayChartValues(true);
r.setChartValuesTextSize(12);
r.setChartValuesSpacing(3);
r.setGradientEnabled(true);
r.setGradientStart(-20, Color.BLUE);
r.setGradientStop(20, Color.GREEN);
return ChartFactory.getRangeBarChartIntent(context, dataset, renderer,
Type.DEFAULT, "Marks Range");
}
protected void setChartSettings(XYMultipleSeriesRenderer renderer,
String title, String xTitle, String yTitle, double xMin,
double xMax, double yMin, double yMax, int axesColor,
int labelsColor) {
renderer.setChartTitle(title);
renderer.setXTitle(xTitle);
renderer.setYTitle(yTitle);
renderer.setXAxisMin(xMin);
renderer.setXAxisMax(xMax);
renderer.setYAxisMin(yMin);
renderer.setYAxisMax(yMax);
renderer.setAxesColor(axesColor);
renderer.setLabelsColor(labelsColor);
}
protected XYMultipleSeriesRenderer buildBarRenderer(int[] colors) {
XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
renderer.setAxisTitleTextSize(16);
renderer.setChartTitleTextSize(20);
renderer.setLabelsTextSize(15);
renderer.setLegendTextSize(15);
int length = colors.length;
for (int i = 0; i < length; i++) {
SimpleSeriesRenderer r = new SimpleSeriesRenderer();
r.setColor(colors[i]);
renderer.addSeriesRenderer(r);
}
return renderer;
}
}
答案 0 :(得分:1)
您可以使用静态变量和方法解决问题。将要共享的变量设置为静态。也可以在同一个类中编写静态方法来获取它。并从另一个类访问该方法。您可以在此处查看示例: - http://www.daniweb.com/software-development/java/threads/326941/passing-data-between-classes。
答案 1 :(得分:1)
不知道你在这做什么:
public void getArray(double array1[]) {
array1 = new double[12];
this.maxValues = array1;
for(int i=0;i<array1.length;i++)
{
Log.i("sanket",maxValues[i]+"");
}
}
你用新的double [12]覆盖输入参数array1,因此覆盖了.maxValues。
答案 2 :(得分:1)
//Create a public class:
public class Values{
public static int[] val = null;
}
//Set array in one class:
Values.val = int[] arr1;//arr1 containing your values
//Get array in another class:
int[] arr2 = Values.val;
答案 3 :(得分:0)
如果将数组转换为int arraylist然后在活动之间传递它然后将其转换回另一个活动中的数组会更好。