[英]Populate ListView in android from SQLite database
我正在學習android編程,現在我正在制作一個使用數據庫的應用程序。 現在我正在使用SQLite來存儲數據。
我已經成功創建了可以在數據庫中存儲/插入數據的應用程序。 它的工作,我已經使用根資源管理器檢查了我的數據庫。
現在在第二個活動中,比如說DisplayRecords,我想將我的記錄顯示到動態填充的列表視圖中。 我如何通過編程實現這一目標。
這是我第二次活動的代碼。
public class SearchResult extends ActionBarActivity {
ListView ls;
SQLiteDatabase db;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.search_result);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ls = (ListView)findViewById(R.id.displayListView);
Cursor c = db.rawQuery("SELECT * FROM records",null);
if(c.getCount()==0){
Toast.makeText(getApplicationContext(), "No Records Found", Toast.LENGTH_LONG).show();
setContentView(R.layout.activity_main);
}
StringBuffer buffer = new StringBuffer();
while(c.moveToNext()){
buffer.append("Topic"+c.getString(0));
buffer.append("Description"+c.getString(1));
}
//now how can i put the fetched value in ListView
}
誰能幫幫我嗎
謝謝
保存您的數據的類
public class Naats_VO {
private String id;
private String person_id;
private String title;
private String keywords;
private String language;
private String type;
private String length;
private String url;
private String fav_count;
private String hit_count;
private String release_date;
public Naats_VO() {
}
public Naats_VO(String id, String person_id, String title, String keywords,
String language, String type, String length, String url,
String fav_count, String hit_count, String release_date) {
super();
this.id = id;
this.person_id = person_id;
this.title = title;
this.keywords = keywords;
this.language = language;
this.type = type;
this.length = length;
this.url = url;
this.fav_count = fav_count;
this.hit_count = hit_count;
this.release_date = release_date;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPerson_id() {
return person_id;
}
public void setPerson_id(String person_id) {
this.person_id = person_id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getKeywords() {
return keywords;
}
public void setKeywords(String keywords) {
this.keywords = keywords;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getLength() {
return length;
}
public void setLength(String length) {
this.length = length;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getFav_count() {
return fav_count;
}
public void setFav_count(String fav_count) {
this.fav_count = fav_count;
}
public String getHit_count() {
return hit_count;
}
public void setHit_count(String hit_count) {
this.hit_count = hit_count;
}
public String getRelease_date() {
return release_date;
}
public void setRelease_date(String release_date) {
this.release_date = release_date;
}
}
包含ListView的活動
private List<Naats_VO> naats_list;
private Naat_Model db;
Naats_Adapter adapter ;
// Initilizae in OnCreate
naats_list; = new ArrayList<Naats_VO>();
// Naat_Model is class which communicate with my db.
db = new Naat_Model(this);
naats_list = db.getFavorites();
adapter = new Naats_Adapter(this, naats_list );
listView.setAdapter(adapter);
Naat_Model
public class Naat_Model extends _BaseModel {
// Contacts table name persons
public static final String TABLE_NAAT = "naats";
// persons Table Columns names
public static final String KEY_ID = "id";
public static final String KEY_PERSON_ID1 = "person_id";
public static final String KEY_TITLE = "title";
public static final String KEY_KEYWORDS = "keywords";
public static final String KEY_LANG1 = "language";
public static final String KEY_TYPE = "type";
public static final String KEY_LENGTH = "length";
public static final String KEY_URL = "url";
public static final String KEY_FAV_COUNT = "fav_count";
public static final String KEY_HIT_COUNT = "hit_count";
public static final String KEY_RELEASE_DATE = "release_date";
public static final String KEY_FAVR = "favorite";
public static final String KEY_RECENT = "recent";
public static final String KEY_DOWNLOAD_STATUS = "status";
public static final String KEY_DOWNLOAD_id = "downloader_id";
public static final String KEY_NOTIFICATION_ID = "notifiaction_id";
public static final String KEY_CREATED_AT = "created_at";
private SQLiteDatabase db;
private Context _context;
public Naat_Model(Context context) {
super(TABLE_NAAT, context);
// TODO Auto-generated constructor stub
_context = context;
}
public List<Naats_VO> getFavorites() {
List<Naats_VO> Naats_list = new ArrayList<Naats_VO>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_NAAT + " WHERE "
+ KEY_FAVR + " = 1";
Database dbHelper = new Database(_context);
this.db = dbHelper.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (c.moveToFirst()) {
do {
Naats_VO naats = new Naats_VO();
naats.setId(c.getString(c.getColumnIndex(KEY_ID)));
naats.setPerson_id(c.getString(c.getColumnIndex(KEY_PERSON_ID1)));
naats.setTitle(c.getString(c.getColumnIndex(KEY_TITLE)));
naats.setKeywords(c.getString(c.getColumnIndex(KEY_KEYWORDS)));
naats.setLanguage(c.getString(c.getColumnIndex(KEY_LANG1)));
naats.setType(c.getString(c.getColumnIndex(KEY_TYPE)));
naats.setLength(c.getString(c.getColumnIndex(KEY_LENGTH)));
naats.setUrl(c.getString(c.getColumnIndex(KEY_URL)));
naats.setFav_count(c.getString(c.getColumnIndex(KEY_FAV_COUNT)));
naats.setHit_count(c.getString(c.getColumnIndex(KEY_HIT_COUNT)));
naats.setRelease_date(c.getString(c
.getColumnIndex(KEY_RELEASE_DATE)));
// Adding contact to list
Naats_list.add(naats);
} while (c.moveToNext());
}
db.close();
c.close();
// return contact list
return Naats_list;
}
此類會將您的數據庫與應用程序http://i.stack.imgur.com/0fFtD.png連接起來
public class Database extends SQLiteAssetHelper {
private static final String DATABASE_NAME = "naat_collection";
private static final int DATABASE_VERSION = 1;
public Database(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// super(context, DATABASE_NAME,
// context.getExternalFilesDir(null).getAbsolutePath(), null,
// DATABASE_VERSION);
}
}
因此,在很大的范圍內使用Android中的數據庫進行工作,我將盡力為您提供一些指導。
首先,您應該使用ContentResolver而不是直接調用數據庫來獲取Cursor,這有助於避免UI掛在艱難的查詢上,因為ContentResolver在單獨的線程中執行查詢。
為了解決您的任務(使用數據填充ListView並在更改時進行更新),Android提供了Loaders機制。
您必須通過Activity中的getLoaderManager方法使用LoaderManager 。 然后實現LoaderManager.LoaderCallbacks(我將其稱為loaderCollback)
在loaderCallback方法onCreateLoader中,您應該創建特定的CursorLoader (它將完成所有工作,以正確查詢單獨線程中的db)
在loaderCallback方法onLoadFinished中初始化加載器之后,您將收到光標,所有要做的就是調用ListView的CursorAdapter的swapCursor方法。 最后是魔術:您必須編寫這樣的內容
cursor.setNotificationUri(getActivity.getContentResolver(), queryUri)
從高角度來看這一切。
有關更多詳細信息,請查看本教程。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.