如何使用ActiveAndroid从预先填充的数据库中检索数据?

时间:2017-04-23 09:49:41

标签: android sqlite activeandroid

我试图在我的项目中实现ActiveAndroid来读取我的资产文件夹中的现有数据库。我已经执行了查询方法并将其放在我的arraylist中,但它不起作用。请查看我的代码。

MainActivity.java:

public class MainActivity extends AppCompatActivity {

//Array list for storing all the inventoryItems
private ArrayList<String> questionItems;

//Adapter for listview
private ArrayAdapter questionItemsAdapter;
private ListView listViewQuestion;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ActiveAndroid.initialize(this);

    listViewQuestion = (ListView) findViewById(R.id.listViewQuestion);

    //Initializing arraylist
    questionItems = new ArrayList<>();

    showList();
}

public static List<Questions> getAll() {
    return new Select()
            .from(Questions.class)
            .orderBy("question ASC")
            .execute();
}

private void showList() {
    //Creating a list and getting all questions from the method
    List<Questions> questions = getAll();

    //Adding all the items of the questions to arraylist
    for (int i = 0; i < questions.size(); i++) {
        Questions question = questions.get(i);
        questionItems.add(question.question);
    }

    //Creating our adapter
    questionItemsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, questionItems);

    //Adding adapter to listview
    listViewQuestion.setAdapter(questionItemsAdapter);

}
}

Questions.java:

@Table(name = "Questions")
public class Questions extends Model {

@Column (name = "question")
public String question;

@Column (name = "answer")
public String answer;

public Questions(){
    super();
}

public Questions(String question, String answer){
    super();
    this.question = question;
    this.answer = answer;
}
}

结果只显示一个空白区域。我哪里做错了? 我已经使用其他方法在这个预先填充的数据库上工作了将近2个月,但我找不到我需要的那个。

0 个答案:

没有答案