如果我措辞不好,我道歉,但为了清楚起见,我会尽力解释。我正在使用MPAndroidChart绘制折线图,我按照本教程启动并运行https://www.numetriclabz.com/android-line-chart-using-mpandroidchart-tutorial/#Defining_X-axis_labels。我做了一些调整以满足我的需求等等。
点击按钮,我调用一个方法,使用编辑文本字段的值添加另一个条目,在我按下每个按钮增加的位置,这样代码就像entries.add(new Entry(editTextValue, numEntries));
这就是我想要的我在查看当前活动屏幕的同时执行剩余的前一个值,并添加下一个值。但是,一旦我离开该活动并返回该活动,只剩下最后一个值。我的理解是,当我调用我正在使用的drawGraph方法时,我需要有一个for循环来遍历arraylist中的每个元素,但我还没有运气。我尝试使用for(Entry e: entries)
并使用e代替numEntries,但数据类型不兼容。任何帮助是极大的赞赏!
编辑:`public class testActivity扩展AppCompatActivity { int counter = 0; public ArrayList entries = new ArrayList<>(); public static int lifetimeNums; public static int nums = 0; public static int numEntries;
public static String entryLabel = Integer.toString(numEntries);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
reDrawGraph();
}
// Graphing method
public void reDrawGraph(){
LineChart chart = (LineChart) findViewById(R.id.chart);
XAxis xAxis = chart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
chart.getAxisLeft().setAxisMaxValue(100);
chart.getXAxis().setAxisMaxValue(100);
//Creating list of entries
LineDataSet dataset = new LineDataSet(entries, "# of Calls");
// creating labels
ArrayList<String> labels = new ArrayList<String>();
for (int i = 0; i < 10 + numEntries; i++) {
labels.add(Integer.toString(i));
}
LineData data = new LineData(labels, dataset);
entries.add(new Entry(testActivity.nums, numEntries));
chart.animateXY(1000,1000);
chart.notifyDataSetChanged();
chart.invalidate();
chart.setData(data); // set the data and list of lables into chart
}
public void counterClicked(View view){
try {
EditText inputText = (EditText) findViewById(R.id.edit_text_val);
int localNums = Integer.parseInt(inputText.getText().toString());
if (counter < 3) {
nums += localNums;
counter++;
numEntries++;
Toast.makeText(this, "Total Entries" + entries.get(0),
Toast.LENGTH_SHORT).show();
reDrawGraph();
inputText.getText().clear();
}
if (counter == 3){
lifetimeNums++;
numEntries++;
Intent intent = new Intent(this, SelectionActivity.class);
startActivity(intent);
}
}catch (Exception e) {
Toast.makeText(this, "Please enter a value",
Toast.LENGTH_SHORT).show();
}`