以下是我的代码,但我如何在另一个活动/类中显示数据库项? 我真的想知道如何在另一个活动中显示已添加到数据库的所有内容。谢谢!
主要活动:
public class StartingActivity extends ActionBarActivity {
ArrayList<String> nameList = new ArrayList<String>();
// Gender: 0=Male, 1=Female
int tier, gender;
DBAdapter db = new DBAdapter(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting);
EditText editName = (EditText) findViewById(R.id.editName);
EditText editTier = (EditText) findViewById(R.id.editTier);
EditText editDate = (EditText) findViewById(R.id.editDate);
editName.setHint("Enter athlete's name");
editTier.setHint("Enter athlete's tier");
editDate.setHint("Enter date");
}
// Creates database Adapter
public void getAllRecords() {
// Get all records
db.open();
Cursor c = db.getAllRecords();
if(c.moveToFirst())
{
do {
DisplayRecord(c);
} while (c.moveToNext());
}
db.close();
}
public void getRecord(Cursor c) {
// Get Specific athlete
db.open();
Cursor cursor = db.getRecord(1);
if (c.moveToFirst()) {
DisplayRecord(c);
} else {
Toast.makeText(this, "No athlete found!", Toast.LENGTH_LONG).show();
}
db.close();
}
public void deleteRecord(Cursor c) {
// Delete Specific athlete
db.open();
if (db.deleteRecord(1)) {
Toast.makeText(this, "Deleted athlete!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Delete failed!", Toast.LENGTH_LONG).show();
}
}
public void onClick_btnConfirm(View view) {
final RadioButton rdbMale = (RadioButton) findViewById(R.id.rbMale);
final RadioButton rdbFemale = (RadioButton) findViewById(R.id.rbFemale);
EditText editName = (EditText) findViewById(R.id.editName);
EditText editTier = (EditText) findViewById(R.id.editTier);
EditText editDate = (EditText) findViewById(R.id.editDate);
TextView textOutput = (TextView) findViewById(R.id.textOutput);
Intent intentList = new Intent(StartingActivity.this, ListActivity.class);
// Checks if Edit name has anything filled in. If not, then pop up displays.
if (editName.getText().length() == 0 || editTier.getText().length() == 0 || editDate.getText().length() == 0) {
Toast.makeText(this, "Please fill out above fields",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Name, date, and tier has been added to list", Toast.LENGTH_LONG).show();
}
if (rdbMale.isChecked()) {
gender = 0;
}
if (rdbFemale.isChecked()) {
gender = 1;
}
DBAdapter db = new DBAdapter(this);
db.open();
long id = db.insertRecord(editName.getText().toString(), editTier.getText().toString(), editDate.getText().toString(),
gender);
db.close();
}
public void DisplayRecord(Cursor c)
{
Toast.makeText(this,
"id: " + c.getString(0) + "\n" +
"Name: " + c.getString(1) + "\n" +
"Tier: " + c.getString(2) + "\n" +
"Date: " + c.getString(3) + "\n" +
"Gender: " + c.getString(4) + "\n", Toast.LENGTH_LONG).show();
}
public void onClick_btnList(View view) {
Intent intent = new Intent(getApplicationContext(), ListActivity.class);
startActivity(intent);
}
}
列出活动:
public class ListActivity extends ActionBarActivity {
DBAdapter db = new DBAdapter(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
TextView textList = (TextView) findViewById(R.id.textList);
db.getAllRecords();
}
}
数据库很好,但现在是:
public class DBAdapter {
public static final String KEY_ROWID = "id";
public static final String KEY_NAME = "name";
public static final String KEY_TIER = "tier";
public static final String KEY_DATE = "date";
public static final String KEY_GENDER = "gender";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "athleteDB";
private static final String DATABASE_TABLE = "myTable";
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_CREATE =
"create table if not exists assignments (id integer primary key autoincrement, "
+ "name VARCHAR not null, tier VARCHAR, date date, gender VARCHAR );";
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)
{
try {
db.execSQL(DATABASE_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}
@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 contacts");
onCreate(db);
}
}
//---opens the database---
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
//---closes the database---
public void close()
{
DBHelper.close();
}
//---insert a record into the database---
public long insertRecord(String name, String tier, String date, int gender)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NAME, name);
initialValues.put(KEY_TIER, tier);
initialValues.put(KEY_DATE, date);
initialValues.put(KEY_GENDER, gender);
return db.insert(DATABASE_TABLE, null, initialValues);
}
//---deletes a particular record---
public boolean deleteRecord(long rowId)
{
return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
//---retrieves all the records---
public Cursor getAllRecords()
{
return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME,
KEY_TIER, KEY_DATE, KEY_GENDER}, null, null, null, null, null);
}
//---retrieves a particular record---
public Cursor getRecord(long rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
KEY_NAME, KEY_TIER, KEY_DATE, KEY_GENDER},
KEY_ROWID + "=" + rowId, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
//---updates a record---
public boolean updateRecord(long rowId, String name, String tier, String date, int gender)
{
ContentValues args = new ContentValues();
args.put(KEY_NAME, name);
args.put(KEY_TIER, tier);
args.put(KEY_DATE, date);
args.put(KEY_GENDER, gender);
return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}
}
答案 0 :(得分:0)
你的getAllRecords方法返回一个游标。请参阅以下示例供您参考: 首先创建一个保存引用的对象:ex。
public class Person{
private int id;
private String name;
private String tier;
private String date;
//do your constructor and getter setter methods
}
Cursor rs = mydb.getAllRecords;
rs.moveToFirst();
ArrayList<Person> personList = new ArrayList<Person>(); //create an arraylist
do{ //use a for or while loop
int id = rs.getString(rs.getColumnIndex(DBHelper.KEY_ROWID));
String name = rs.getString(rs.getColumnIndex(DBHelper.KEY_NAME)); //get all the table column you need
String tier = rs.getString(rs.getColumnIndex(DBHelper.KEY_TIER));
String date = rs.getString(rs.getColumnIndex(DBHelper.KEY_DATE));
personList.add(new Person(id,name,tier,date));
}while(rs.moveToNext());
textview.setText(personList.get(0).getname()); //display it