将数据从SQLite数据库加载到ListView中

问题描述:

我想从我的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; 
} 
} 

就如何实现这一目标有什么建议?

+1

你可以发布你想要填充listview的类吗?例如,如果将内容视图设置为列表视图所在的xml,并通过findviewbyid – LBJ33

+1

找到列表视图,则需要创建一个用于存储值的pojo类,并将其用于自定义适配器以便更好地使用,请参阅以下参考链接:http:// *.com/questions/37905218/how-to-show-in-listviewusing-okhttp-library/37942092#37942092 – Vickyexpert

假设你正在使用TextView S代表每个列的数据库后,您可以使用加载了ListView活动中的以下代码:

DBAdapter db = new DBAdapter(this); 
Cursor cursor = db.getAllValues(); 

// The columns that you want to bind 
String[] columns = new String[] { 
    db.KEY_NUMBER, 
    db.KEY_NAME, 
    db.KEY_VERSION, 
    db.KEY_METHOD, 
    db.KEY_CHAIN_NUMBER, 
    db.KEY_CAUGHT 
}; 

// The IDs of the TextViews you want to bind the columns with 
int[] to = new int[] { 
    R.id.number, 
    R.id.name, 
    R.id.version, 
    R.id.method, 
    R.id.chain_number, 
    R.id.caught 
}; 

// Attach the layout file and the columns to the ListView 
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
    this, 
    R.layout.your_listview_layout, 
    cursor, 
    columns, 
    to, 
    0); 

ListView listView = (ListView) findViewById(R.id.your_listview); 
// Assign the adapter to the ListView 
listView.setAdapter(dataAdapter); 

确保你有一个布局文件你的ListView。您可能需要更改此代码以满足您的需求。如果您有任何疑问,请在下面评论。