请帮助我理解为什么我的ArrayAdapter工作不正常,我的意思是它显示我作为参数给出的字符串列表。我在模拟器中收到了流动的消息"不幸的是'程序名称'已停止"。但是当我给出一个常规的String []数组时,它会正确显示这些项目。 这是"有问题的"代码部分。
//this method is in Class DatabaseAccess, it return a record from db as List<String>
public List<String> getQuestionRecord() {
List<String> list = new ArrayList<>();
int counter = 1;
while (!cursor.isAfterLast() && (counter < 8)) {
list.add(cursor.getString(counter++));
}
if (!cursor.isAfterLast()){
cursor.moveToNext();
}else{
closeCursor();
return null;
}
return list;
}
// this method called from the MainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
databaseAccess = DatabaseAccess.getInstance(this);
databaseAccess.open();
databaseAccess.getCursorReady();
List<String> questionRecord = databaseAccess.getQuestionRecord();
if (questionRecord != null){
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,questionRecord);
listView.setAdapter(adapter);
}else
databaseAccess.close();
}