如何在活动中显示sqlite中的arraylist然后迭代

时间:2017-04-19 16:40:24

标签: java android sqlite

我已经从我的数据库创建了一个arraylist,如下所示

 public List<String> getAllQuestions(){
    List<String> questions = new ArrayList<>();

    String query = "SELECT QUESTION FROM QUESTIONS ";
    Cursor cursor=db.rawQuery(query, null);

    while(cursor.moveToNext()) {
        questions.add(cursor.getString(0));
    }
    cursor.close();


    return questions;

}

并且基本上想要在数组列表中显示第一个问题,然后单击我的答案并在数组列表中显示下一个问题......直到所有问题都已显示和回答。

但我的代码无法正常工作。

Button btnAnswer1, btnAnswer2, btnAnswer3;
LoginDataBaseAdapter LoginDataBaseAdapter;
EditText editNameComplete;
int index = 0;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.message);

    LoginDataBaseAdapter = new LoginDataBaseAdapter(CompleteSurveyActivity.this);
    LoginDataBaseAdapter.open();



    editNameComplete = (EditText) findViewById(R.id.editTextQuestion);

    TextView tv = (TextView) findViewById(R.id.questionnn);

This one works when i only want to get the first without using the array  method.
  //tv.setText(LoginDataBaseAdapter.getQuestion());


however when i try to use the array here, it get a "cannot resolve symbol error.
    tv.setText(LoginDataBaseAdapter.getAllQuestions.get(index));

    //get refs of buttons
    btnAnswer1 = (Button) findViewById(R.id.buttonAnswer1);
    btnAnswer1.setText(LoginDataBaseAdapter.getAnswer1());
    btnAnswer2 = (Button) findViewById(R.id.buttonAnswer2);
    btnAnswer2.setText(LoginDataBaseAdapter.getAnswer2());
    btnAnswer3 = (Button) findViewById(R.id.buttonAnswer3);
    btnAnswer3.setText(LoginDataBaseAdapter.getAnswer3());
}

我基本上根本不确定如何调用和使用创建arraylist的getAllQuestions方法。

1 个答案:

答案 0 :(得分:0)

在两个类中导入java.util.ArrayList;java.util.List;,它应该可以正常工作。在尝试.get(index)时使用List; ,所以你必须导入它。