简体   繁体   English

如何在Android中处理SQLite数据集

[英]How can I process a SQLite dataset in Android

I need to process a SQLite dataset in an Android system. 我需要在Android系统中处理SQLite数据集。

In my dataBaseHelper file (DataBaseAccessor) I have the following code (which when attached to a listview shows the relevant data). 在我的dataBaseHelper文件(DataBaseAccessor)中,我具有以下代码(将其附加到列表视图时将显示相关数据)。

public static ArrayList<QuestionListQuestion> getQuestionListQuestions(long id){
String qry = "select QuestionListQuestionID, QuestionListQuestionQuestionListID, QuestionListQuestionQuestionID, QuestionListQuestionSortOrder, QuestionListQuestionSupplementalQuestionIDYes, QuestionListQuestionSupplementalQuestionIDNo, QuestionListQuestionSupplementalQuestionIDText, QuestionListQuestionSurveyGroupID from QuestionListQuestion  where  QuestionListQuestionQuestionListID=" + id;
ArrayList<QuestionListQuestion> list = new ArrayList<QuestionListQuestion>();
try{
    Cursor cursor = wdb.rawQuery(qry, null);
while (cursor.moveToNext()) {
QuestionListQuestion questionlistquestion = new QuestionListQuestion();
    questionlistquestion.QuestionListQuestionID = cursor.getLong(0);
    questionlistquestion.QuestionListQuestionQuestionListID = cursor.getLong(1);
    questionlistquestion.QuestionListQuestionQuestionID = cursor.getLong(2);
    questionlistquestion.QuestionListQuestionSortOrder = cursor.getLong(3);
questionlistquestion.QuestionListQuestionSupplementalQuestionIDYes = cursor.getString(4);       questionlistquestion.QuestionListQuestionSupplementalQuestionIDNo = cursor.getString(5);
questionlistquestion.QuestionListQuestionSupplementalQuestionIDText = cursor.getString(6);
questionlistquestion.QuestionListQuestionSurveyGroupID = cursor.getLong(7);
list.add(questionlistquestion);
}
    cursor.close();
}
catch (Exception e) {
    e.printStackTrace();
}
return list;
}

I now need to extend the system so that I can process the data and create new records in another table based on the original list returned. 现在,我需要扩展系统,以便可以处理数据并根据返回的原始列表在另一个表中创建新记录。

I tried the following attached to a button (selecting the relevant list ID from a spinner):- 我尝试将以下内容附加到按钮上(从微调器中选择相关列表ID):

QuestionListID = (String) SiteGenerateQuestions.this.spnQuestL.getSelectedItem().toString();
long SpinnerSelectedBT;
SpinnerSelectedBT = GenerateQuestions.this.spnQuestL.getSelectedItemId();
list = DatabaseAccessor.getQuestionListQuestions(SpinnerSelectedBT);
for (int i=0; i < list.size(); i++){
Toast.makeText(SiteGenerateQuestions.this," list.get(" + i + ")   = " + list.get(i) + " "  , Toast.LENGTH_SHORT).show();
}

The Toast displays the following:- 吐司显示以下内容:

list.get(0) =  com.tw.question.entity.QuestionListQuestion@407a6F70
list.get(1) =  com.tw.question.entity.QuestionListQuestion@407bc170
etc...

How can I get access to the actual data instead of ... .entity.QuestionListQuestion@407bc170 or am I completely off-track? 我该如何访问实际数据而不是... .entity.QuestionListQuestion @ 407bc170,还是我完全偏离轨道了?

Many Thanks 非常感谢

The output you're seeing is because you haven't implemented the toString() method of your QuestionListQuestion class. 您看到的输出是因为尚未实现QuestionListQuestion类的toString()方法。 Other than that, it seems like you do have the data you're looking for. 除此之外,似乎您确实拥有所需的数据。 Just try outputting the ID of your object, rather than concatenating the object itself (which will call toString() under the hood). 只需尝试输出对象的ID,而不是串联对象本身(这将在toString()调用toString() )。

Also, since it looks like you're doing a database operation when a button gets clicked, make sure to do your database operations outside the UI thread. 另外,由于单击按钮看起来像是在进行数据库操作,因此请确保在UI线程外进行数据库操作。 Check out the docs for a high level overview. 查看文档以获取高层次的概述。 You might want to use an AsyncTask for your database operation in question. 您可能要对所涉及的数据库操作使用AsyncTask

I agree with @wsanville the get() method will return the object in that location of the list. 我同意@wsanville的get()方法将在列表的该位置返回对象。 When you print out an object (in a toast, log, System.out.println etc) it will use the toString() value in the printout. 当您打印出一个对象(在吐司,日志,System.out.println等中)时,它将使用打印输出中的toString()值。 The default toString() is the package name followed by @ which is followed by a hex representation of that object. 默认的toString()是程序包名称,后跟@,后跟该对象的十六进制表示形式。 Your class will need to override the toString() method so when you use get() it will print out whatever you put in your toString() method. 您的类将需要重写toString()方法,因此当您使用get() ,它将打印出您放入toString()方法中的所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM