2013-04-13 16 views
-1

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_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; 
} 

Recomend.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); 

我认为c因为Textview(多行文本)没有显示任何内容,所以Ode在某个地方是错误的。 任何人都可以指出我的错误吗?我完全不熟悉Eclipse。

+0

你知道如何通过代码设置断点和跟踪吗?我会建议这样做。此外,您可以使用Log.v或Log.d将内容输出到控制台,您可以看到正在发生的事情。 – HalR

+0

我不知道。我全新的,我工作这个应用程序为我的任务。该应用程序实际工作正常。只是没有数据显示在多行文本中。 – user2267959

+0

您应该查看如何设置和使用断点以及如何打印日志语句。如果你能做到这一点,你将是一个非常危险的程序员。 – HalR

回答

0

你的代码有几个错误,首先,你返回的数组“数据”总是空的,所以你当然不能从中得到任何东西。其次,如果你打算从数据库中得到几个结果,你应该声明一个列表并添加你从数据库中获得的objets,目前你的循环完全没用,你实例化你永远不会使用的变量。 。

这里是展示如何从数据库得到一个列表中的一个例子,但你也可以得到一个指针,数组或任何你需要:

// 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; 
    } 

注意:在这个例子中,我得到几行从数据库中,如果只想获得一行,只需删除循环do-while并直接返回对象。

+0

我应该改变什么,所以它实际上从sqlite获取数据并存储在变量中? – user2267959

+0

我正在做的应用程序根据他们选择的“选择”[Where条件],实际获得1行数据。只有1行和多列。列中的每个数据都将以xml的多行文本显示。 – user2267959