Java 不能';t从游标窗口读取第0行第1列
我很难在我的应用程序中使用图形,但我不知道这段代码哪里出了问题。代码生成但在选择活动时崩溃。我想我可能是结构不正确,因为我可能在从数据库获取数据之前试图创建一个图表。如果有人能指出我的错误,我将不胜感激Java 不能';t从游标窗口读取第0行第1列,java,android,android-graphview,Java,Android,Android Graphview,我很难在我的应用程序中使用图形,但我不知道这段代码哪里出了问题。代码生成但在选择活动时崩溃。我想我可能是结构不正确,因为我可能在从数据库获取数据之前试图创建一个图表。如果有人能指出我的错误,我将不胜感激 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_graph); my
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_graph);
myDB = new DatabaseCode(this);
SQLiteDatabase db = myDB.getWritableDatabase();
Cursor cursor=db.rawQuery("select * from "+TABLE_NAME,null);
if(cursor.getCount()==0){
return;
}
else {
ArrayList<Integer> ListArray = new ArrayList<Integer>();
//
while (cursor.moveToNext()) {
ListArray.add(cursor.getInt(cursor.getColumnIndex("COL_2")));
}
GraphView graph = (GraphView) findViewById(R.id.graph);
LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[] {
new DataPoint(0,ListArray.get(0)),
new DataPoint(1,ListArray.get(1)),
new DataPoint(2,ListArray.get(2))
});
graph.addSeries(series);
}
}
}
当从游标访问数据时,您必须检查游标中的数据,并将游标移动到第一个位置,循环将按照下面的示例进行
if(cursor.moveToFirst()) //this will move cursor to first position
{
do{
//your code to fetch data from cursor
ListArray.add(cursor.getInt(cursor.getColumnIndex("COL_2")));
}while(cursor.moveToNext());
}
同时检查“COL_2”列名是否正确,是否符合架构要求。您不知道问题出在哪里是什么意思
在com.example.fitmess.project.GraphActivity.onCreate(GraphActivity.java:36)
干杯这是答案,是的,Colu 2是错误的,谢谢。
if(cursor.moveToFirst()) //this will move cursor to first position
{
do{
//your code to fetch data from cursor
ListArray.add(cursor.getInt(cursor.getColumnIndex("COL_2")));
}while(cursor.moveToNext());
}