SQLite 데이터베이스의 데이터로 ListView를 채우려는 내 머리를 때리고 있습니다. 무수한 튜토리얼과 똑같이 무수한 게시물을 읽었지만 분명히 중요한 것을 얻지는 않습니다. 다음 두 코드가 왜 함께 작동하지 않는지, 아니면 완전히 다른 무언가를보아야하는지에 대한 힌트를 누군가가 얻을 수 있기를 바랬습니다. 어떤 도움을 주시면 감사하겠습니다. 내가 얻는 결과는 힘이 가깝다는 것이다. ListView에 객체를 SimpleCursorAdapter 반환SQLite 데이터베이스의 데이터로 ListView 채우기
public void displayNurseRoster(){
listView = (ListView) findViewById(R.id.list);
// create instance of DbCommunicator
DbCommunicator rosterView = new DbCommunicator(this);
// open instance
rosterView.open();
// instantiate SimpleCursorAdapter instance and set value
SimpleCursorAdapter cursorAdapter;
cursorAdapter = rosterView.getRosterListViewAdapter(this);
// close database instance
rosterView.close();
// set adapter to listView
listView.setAdapter(cursorAdapter);
}
방법 채우기 시작
방법 :
public SimpleCursorAdapter getRosterListViewAdapter (Context context) {
// method variables
int[] to = new int[] {R.id.rosterListLname};
// used ArrayList because of unpredictability of array length
List<String> dataArray = new ArrayList<String>();
String[] columns = new String[] {KEY_NURSE_ROWID, KEY_LNAME};
// create cursor
Cursor cursor = sqldb.query(NURSE_TABLE, columns, null, null, null,
null, KEY_LNAME);
int iLname = cursor.getColumnIndex(KEY_LNAME);
cursor.moveToFirst();
String result = "";
while(!cursor.isAfterLast()){
result = result
+ cursor.getString(iLname) + "\n";
dataArray.add(result);
}
// convert ArrayList to String array for use with SimpleCursorAdapter
String [] from = (String[]) dataArray.toArray();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(context,
R.layout.edit_roster, cursor, from, to);
return adapter;
}
아직이 작은 문제를 해결하고 있습니다. 어떤 도움이라도 대단히 감사하겠습니다. – deadEddie