2
我想從我的SQLite數據庫中獲取一些數據到我的ListView
。現在,當數據庫啓動時,我的數據庫中的數據加載到Toast
中,因此我相信我處於正確的軌道上。我只是無法將數據放入ListView
。我已經看過一些教程,但我仍然不確定。這裏是我OverviewActivity.java
類:將數據從SQLite數據庫加載到ListView中
public class OverviewActivity extends ListActivity implements View.OnClickListener {
private DBAdapter dbAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.overview_activity);
dbAdapter = new DBAdapter(this);
dbAdapter.open();
Cursor c = dbAdapter.getAllValues();
if (c.moveToFirst()) {
do {
DisplayTitle(c);
} while (c.moveToNext());
}
}
public void DisplayTitle(Cursor c) {
Toast.makeText(this,
"id: " + c.getString(0) + "\n" +
"Number: " + c.getString(1) + "\n" +
"Name: " + c.getString(2) + "\n" +
"Version: " + c.getString(3) + "\n" +
"Method: " + c.getString(4) + "\n" +
"Chain Number: " + c.getString(5) + "\n" +
"Caught: " + c.getString(6) + "\n",
Toast.LENGTH_LONG).show();
}
@Override
public void onClick(View view) {
//TODO: AUTOGENERATED METHOD
}
@Override
public void onDestroy(){
super.onDestroy();
dbAdapter.close();
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_add:
Intent intent = new Intent(OverviewActivity.this, VersionMethodActivity.class);
startActivity(intent);
break;
}
return false;
}
}
這裏是我DBAdapter.java
類:
public static final String KEY_ID = "_id";
public static final String KEY_NUMBER = "number";
public static final String KEY_NAME = "name";
public static final String KEY_VERSION = "version";
public static final String KEY_METHOD = "method";
public static final String KEY_CHAIN_NUMBER = "chainNumber";
public static final String KEY_CAUGHT = "caught";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "Chaining";
private static final String DATABASE_TABLE = "ValuesTable";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE =
"create table " + DATABASE_TABLE + "(" +
KEY_ID + " integer primary key autoincrement, " +
KEY_NUMBER + " integer, " +
KEY_NAME + " text, " +
KEY_VERSION + " text, " +
KEY_METHOD + " text, " +
KEY_CHAIN_NUMBER + " integer, " +
KEY_CAUGHT + " boolean)";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter (Context ctx){
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion
+ " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS titles");
onCreate(db);
}
}
public DBAdapter open() throws SQLException{
db = DBHelper.getWritableDatabase();
return this;
}
public void close(){
DBHelper.close();
}
public long insertValues(int number, String name, String version, String method, int chainNumber, boolean caught){
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NUMBER, number);
initialValues.put(KEY_NAME, name);
initialValues.put(KEY_VERSION, version);
initialValues.put(KEY_METHOD, method);
initialValues.put(KEY_CHAIN_NUMBER, chainNumber);
initialValues.put(KEY_CAUGHT, caught);
return db.insert(DATABASE_TABLE, null, initialValues);
}
public boolean deleteValues(long rowId){
return db.delete(DATABASE_TABLE, KEY_ID + " = " + rowId, null) > 0;
}
public Cursor getAllValues(){
return db.query(DATABASE_TABLE, new String[]{
KEY_ID,
KEY_NUMBER,
KEY_NAME,
KEY_VERSION,
KEY_METHOD,
KEY_CHAIN_NUMBER,
KEY_CAUGHT},
null, null, null, null, null);
}
public Cursor getValues(long rowId) throws SQLException {
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[]{
KEY_ID,
KEY_NUMBER,
KEY_NAME,
KEY_VERSION,
KEY_METHOD,
KEY_CHAIN_NUMBER,
KEY_CAUGHT},
KEY_ID + " = " + rowId,
null, null, null, null, null);
if (mCursor != null){
mCursor.moveToFirst();
}
return mCursor;
}
public boolean updateValues(long rowId, int number, String name, String version, String method, int chainNumber, boolean caught){
ContentValues args = new ContentValues();
args.put(KEY_NUMBER, number);
args.put(KEY_NAME, name);
args.put(KEY_VERSION, version);
args.put(KEY_METHOD, method);
args.put(KEY_CHAIN_NUMBER, chainNumber);
args.put(KEY_CAUGHT, caught);
return db.update(DATABASE_TABLE , args, KEY_ID + " = " + rowId, null) > 0;
}
}
就如何實現這一目標有什麼建議?
你可以發佈你想要填充listview的類嗎?例如,如果將內容視圖設置爲列表視圖所在的xml,並通過findviewbyid – LBJ33
找到列表視圖,則需要創建一個用於存儲值的pojo類,並將其用於自定義適配器以便更好地使用,請參閱以下參考鏈接:http:// stackoverflow.com/questions/37905218/how-to-show-in-listviewusing-okhttp-library/37942092#37942092 – Vickyexpert