0
데이터베이스가 모두 설정되어 실행 중입니다. 하지만 지금은 내가 가지고있는 문제는 그것을 listview에로드하고있다. 사이트에서 다른 질문을 시도했지만 아무 것도 내 질문에 대답하지 않습니다. 초보자 (초보자)의 관점에서. 데이터베이스를리스트 뷰로로드하려면 어떻게해야합니까? 데이터베이스의 데이터를 목록보기로 표시
이
이 내 수업 시간에 무엇을 가지고 : (public class Inventory extends Activity {
DBAdapter db = new DBAdapter(this);
private Cursor cursor = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inventory_screen);
try {
String destPath = "/data/data/" + getPackageName()
+ "/databases/InventoryDB";
File f = new File(destPath);
if (!f.exists()) {
CopyDB(getBaseContext().getAssets().open("InventoryDB"),
new FileOutputStream(destPath));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
DBAdapter db = new DBAdapter(this);
try {
db.open();
Cursor c = db.getAllRecords();
if (c.moveToFirst()) {
do {
DisplayRecord(c);
} while (c.moveToNext());
}
db.close();
} catch (Exception e) {
Log.e("ERROR", "ERROR IN CODE:" + e.toString());
e.printStackTrace();
}
}
public void CopyDB(InputStream inputStream, OutputStream outputStream)
throws IOException {
// ---copy 1K bytes at a time---
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
}
// List View
public void DisplayRecord(Cursor c) {
ListView InventoryItems = (ListView) findViewById(R.id.listViewInventory);
String[] Items = new String[] {c.getString(1)};
ArrayAdapter<String> ItemsAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, Items);
InventoryItems.setAdapter(ItemsAdapter);
//Toast msg = Toast.makeText(Inventory.this, "id: " + c.getString(0)
// + "\n" + "Item: " + c.getString(1) + "\n", Toast.LENGTH_SHORT);
//msg.show();
}
난 그냥 데이터베이스에 1 항목을 끌어 설정 목록보기가 그게 (디스플레이 클래스는 하단에) 내 모든를 지식이 확장 됨)
어떻게 모든 데이터를 표시합니까? simpleCursorAdapter는 들었지만 관련이 있는지는 알 수 없다. 이것에 대한
당신이'SimpleCursorAdapter'을 사용하는 방법을 보여줍니다 Listview''에 안드로이드 가이드를 확인해 봤어? http://developer.android.com/guide/topics/ui/layout/listview.html – oldergod
* 올바른 방법으로 사용하려면 위 링크에서 설명한대로 로더 API를 사용하십시오. –