通过for循环(GraphView)将多个值放入DataPoint
Putting multiple values in DataPoint via for loop (GraphView)
我目前正在开发一个应用程序,在应用程序内部,我需要绘制一些折线图。我研究并找到了一个工具,但问题如下:
public class graphActivity extends AppCompatActivity {
GraphView graph;
SharedPrefManager sharedPrefManager = new SharedPrefManager();
int exercisePosition;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_graph);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//getting the exercise position from caller activity and proceeding
//accordingly...
if(savedInstanceState == null){
Bundle extras = getIntent().getExtras();
if(extras == null){
//do nothing
} else{
exercisePosition = extras.getInt("position");
}
} else{
exercisePosition = (int) savedInstanceState.getSerializable("position");
}
graph = (GraphView) findViewById(R.id.graph);
ArrayList<Exercise> exercises= sharedPrefManager.readSharedPref(this);
//getting the exercise from exercises array list so that the specific graph for the exercise can be shown.
Exercise exercise = exercises.get(exercisePosition);
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(new DataPoint[] {
//need to put for loop here but I can't...
//new DataPoint(x,y);
});
graph.addSeries(series);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
确切地说,我需要使用 for 循环填充 DataPoint 变量,因为数据将由用户输入,但是当我尝试实现 for 循环时它很生气,我不知道如何解决它.如果有人在任何帮助之前处理过这种事情,我们将不胜感激。
做这样的事情,
DataPoint[] dp = new DataPoint[10];
for(int i=1;i<=10;i++){
dp[i] = new DataPoint(x, y);
}
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(dp);
如果您不知道您将拥有多少个数据点,您可以使用 ArrayList:
if (cursor.moveToFirst()) {
List<DataPoint> dataPoints = new ArrayList<DataPoint>();
do {
dataPoints.add(
new DataPoint(cursor.getInt(0),
cursor.getInt(1)));
} while (cursor.moveToNext());
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(
dataPoints.toArray(new DataPoint[0]));
}
我目前正在开发一个应用程序,在应用程序内部,我需要绘制一些折线图。我研究并找到了一个工具,但问题如下:
public class graphActivity extends AppCompatActivity {
GraphView graph;
SharedPrefManager sharedPrefManager = new SharedPrefManager();
int exercisePosition;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_graph);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//getting the exercise position from caller activity and proceeding
//accordingly...
if(savedInstanceState == null){
Bundle extras = getIntent().getExtras();
if(extras == null){
//do nothing
} else{
exercisePosition = extras.getInt("position");
}
} else{
exercisePosition = (int) savedInstanceState.getSerializable("position");
}
graph = (GraphView) findViewById(R.id.graph);
ArrayList<Exercise> exercises= sharedPrefManager.readSharedPref(this);
//getting the exercise from exercises array list so that the specific graph for the exercise can be shown.
Exercise exercise = exercises.get(exercisePosition);
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(new DataPoint[] {
//need to put for loop here but I can't...
//new DataPoint(x,y);
});
graph.addSeries(series);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
确切地说,我需要使用 for 循环填充 DataPoint 变量,因为数据将由用户输入,但是当我尝试实现 for 循环时它很生气,我不知道如何解决它.如果有人在任何帮助之前处理过这种事情,我们将不胜感激。
做这样的事情,
DataPoint[] dp = new DataPoint[10];
for(int i=1;i<=10;i++){
dp[i] = new DataPoint(x, y);
}
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(dp);
如果您不知道您将拥有多少个数据点,您可以使用 ArrayList:
if (cursor.moveToFirst()) {
List<DataPoint> dataPoints = new ArrayList<DataPoint>();
do {
dataPoints.add(
new DataPoint(cursor.getInt(0),
cursor.getInt(1)));
} while (cursor.moveToNext());
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(
dataPoints.toArray(new DataPoint[0]));
}