Android 初学者-将sqlite数据作为全局变量加载
Database.javaAndroid 初学者-将sqlite数据作为全局变量加载,android,eclipse,sqlite,Android,Eclipse,Sqlite,Database.java public class Data extends SQLiteOpenHelper { public static String week, classes, title, mat, it, comp, que; ///更常见的代码,如创建、删除数据库 public String[] getAllIAI(){ // Select All Query String selectQuery = "SELECT * FROM " + TABLE_I
public class Data extends SQLiteOpenHelper {
public static String week, classes, title, mat, it, comp, que;
///更常见的代码,如创建、删除数据库
public String[] getAllIAI(){
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_IAI + "WHERE KEY_week =" +LessonPlanner.itemWeek+ "AND KEY_class=" +LessonPlanner.itemClass ;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor3 = db.rawQuery(selectQuery, null);
String[] data = null;
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
title=cursor.getString(3);
mat=cursor.getString(4);
it=cursor.getString(5);
} while (cursor.moveToNext());
}
// closing connection
cursor.close();
db.close();
// returning lables
return data;
}
Java
宣布
TextView title, mat, IT;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recomend);
title = (TextView) findViewById(R.id.titleText);
mat = (TextView) findViewById(R.id.materialText);
IT = (TextView) findViewById(R.id.instTText);
title.setText(Data.title);
mat.setText(Data.mat);
IT.setText(Data.it);
我认为代码在某些地方是错误的,因为Textview(多行文本)没有显示任何内容。
有人能指出我的错误吗?我对Eclipse一无所知。您的代码中有几个错误,首先,您返回的数组“data”总是空的,因此您当然无法从中获得任何信息 其次,如果您计划从数据库中获取多个结果,您应该声明一个列表并将从数据库中获取的对象添加到其中,目前您的循环完全没有用处,您实例化了从未使用过的变量 下面是一个示例,演示如何从数据库中获取列表,但也可以获取光标、数组或任何需要的内容:
// Getting All Categories By ID
public List<Category> getCategoriesList() {
String selectQuery = "SELECT " + CATEGORY_ID + ", " + CATEGORY_NAME + " FROM " + CATEGORY_TABLE + " ORDER BY " + CATEGORY_ID + " ASC";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
List<Category> categoriesList = new ArrayList<Category>();
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Category category = new Category(cursor.getInt(0), cursor.getString(1), false);
categoriesList.add(category);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return categoriesList;
}
//按ID获取所有类别
公共列表getCategoriesList(){
String selectQuery=“选择”+CATEGORY\u ID+”,“+CATEGORY\u NAME+”来自“+CATEGORY\u TABLE+”订单依据“+CATEGORY\u ID+”ASC”;
SQLiteDatabase db=this.getReadableDatabase();
Cursor Cursor=db.rawQuery(selectQuery,null);
List categoriesList=新建ArrayList();
//循环遍历所有行并添加到列表
if(cursor.moveToFirst()){
做{
Category Category=新类别(cursor.getInt(0),cursor.getString(1),false);
类别列表。添加(类别);
}while(cursor.moveToNext());
}
cursor.close();
db.close();
返回分类列表;
}
注意:在这个示例中,我从数据库中获取了几行,如果您只想获取一行,只需删除循环do while并直接返回对象。您知道如何设置断点并跟踪代码吗?我建议这样做。另外,您可以使用Log.v或Log.d将内容打印到控制台,您可以看到实际发生的情况。我不知道。我是全新的,我正在为我的作业使用这些应用程序。该应用程序实际上运行良好。只是多行文本中没有显示数据。您应该了解如何设置和使用断点,以及如何打印日志语句。如果你能做到这一点,你将是一个相当危险的程序员。我应该做些什么来改变它,使它真正从sqlite获取数据并存储在变量中?我所做的应用程序实际上是基于他们选择的“选择”(Where条件)获取1行数据。只有一行和多列。列中的每个数据都将以xml格式的多行文本显示。