从一个 activity 传递数据并从 Main Activity 显示数据库
passing data from one activity and displaying the database from the Main Activity
这里 Java 的新手 :)
我面临的问题是我无法通过单击按钮从 Main Activity 显示数据库。数据库中的数据是从另一个 activity 传递过来的,想知道我是否遗漏了什么或者我将如何解决这个问题。谢谢:)
(在 onCreate 之后的 Main Activity 中使用的方法)
主要Activity
public void displayDataInTable() {
roomDatabase myOpenHelper;
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String status = bundle.getString("status");
String roomValue =bundle.getString("roomValue");
//List for the name of the players and their scores
List<String> roomNumber = new ArrayList<String>();
List<String> roomStatus = new ArrayList<String>();
//The data being read from the database
myOpenHelper = new roomDatabase(this, DATABASE_NAME, null, VERSION_NUMBER);
SQLiteDatabase db = myOpenHelper.getReadableDatabase();
Cursor cursor = db.query(roomDatabase.TABLE_NAME, null, null, null, null, null,null,null);
//The name and the score is printed on the row of the list
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex("_id"));
String roomValueA = cursor.getString(cursor.getColumnIndex("roomValue"));
String statusA = cursor.getString(cursor.getColumnIndex("status"));
roomNumber.add(roomValueA);
roomStatus.add(" Recipe Ordered " + statusA);
}
//The items to de displayed are sent to the AdapterB
if ((roomNumber != null) && (roomStatus != null)) {
AdapterB adapter = new AdapterB(this, roomNumber, roomStatus);
setListAdapter(adapter);
}
}
日志Activity
public class LogsActivity extends ListActivity{
roomDatabase myOpenHelper;
final String DATABASE_NAME = "roomstatus.db";
final int VERSION_NUMBER = 1;
ContentValues values1 = new ContentValues();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//variables are extracted from the bundle
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String status = bundle.getString("status");
String roomValue = bundle.getString("roomValue");
myOpenHelper = new roomDatabase(this, DATABASE_NAME, null, VERSION_NUMBER);
SQLiteDatabase db = myOpenHelper.getWritableDatabase();
values1.put("status", status);
values1.put("roomValue", roomValue);
Toast.makeText(LogsActivity.this, status, Toast.LENGTH_LONG).show();
//Data is entered into the table
db.insert(roomDatabase.TABLE_NAME, null, values1);
Intent newIntentBundle = new Intent(LogsActivity.this, MainActivity.class);
Bundle bundleA = new Bundle();
bundle.putString("status", status);
bundle.putString("roomValue", roomValue);
newIntentBundle.putExtras(bundle);
startActivity(newIntentBundle);
}
}
注意 - 我知道这可能不是正确的做法,因为这意味着在 Main Activity 中创建一个新数据库并给适配器带来麻烦,所以任何帮助将不胜感激。 :)
为数据库创建一个单独的 class。以下是有关如何在 android.
中创建 SQLite 数据库的一些链接
这些是在 RoomDatabase 中保存和获取的方法 class:-
public boolean addDetails(String status,String roomValue){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(KEY_STATUS,status);
values.put(KEY_ROOMVALUE,roomValue);
db.insert(TABLE_NAME,null,values);
db.close();
return true;
}
public List<String> getAllDetail(){
List<String> detailList=new ArrayList<>();
SQLiteDatabase db=this.getReadableDatabase();
Cursor cursor=db.rawQuery("select * from "+TABLE_NAME,null);
String status,roomValue;
if(cursor.moveToFirst()){
do{
status=cursor.getString(cursor.getColumnIndex(KEY_STATUS));
roomValue=cursor.getString(cursor.getColumnIndex(KEY_ROOMVALUE));
detailList.add(status);
detailList.add(roomValue);
}while (cursor.moveToNext());
}
return detailList;
}
在你的 activity 你必须保存在数据库中的地方:
RoomDatabase roomDatabase=new RoomDatabase(getContext());
roomDatabase.addDetails(status,roomValue);
在您的 activity 中,您必须从数据库中检索:
RoomDatabase roomDatabase=new RoomDatabase(getContext());
List<String> detailList=db.getAllDetail();
在detailList中可以得到值。
这里 Java 的新手 :)
我面临的问题是我无法通过单击按钮从 Main Activity 显示数据库。数据库中的数据是从另一个 activity 传递过来的,想知道我是否遗漏了什么或者我将如何解决这个问题。谢谢:)
(在 onCreate 之后的 Main Activity 中使用的方法)
主要Activity
public void displayDataInTable() {
roomDatabase myOpenHelper;
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String status = bundle.getString("status");
String roomValue =bundle.getString("roomValue");
//List for the name of the players and their scores
List<String> roomNumber = new ArrayList<String>();
List<String> roomStatus = new ArrayList<String>();
//The data being read from the database
myOpenHelper = new roomDatabase(this, DATABASE_NAME, null, VERSION_NUMBER);
SQLiteDatabase db = myOpenHelper.getReadableDatabase();
Cursor cursor = db.query(roomDatabase.TABLE_NAME, null, null, null, null, null,null,null);
//The name and the score is printed on the row of the list
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex("_id"));
String roomValueA = cursor.getString(cursor.getColumnIndex("roomValue"));
String statusA = cursor.getString(cursor.getColumnIndex("status"));
roomNumber.add(roomValueA);
roomStatus.add(" Recipe Ordered " + statusA);
}
//The items to de displayed are sent to the AdapterB
if ((roomNumber != null) && (roomStatus != null)) {
AdapterB adapter = new AdapterB(this, roomNumber, roomStatus);
setListAdapter(adapter);
}
}
日志Activity
public class LogsActivity extends ListActivity{
roomDatabase myOpenHelper;
final String DATABASE_NAME = "roomstatus.db";
final int VERSION_NUMBER = 1;
ContentValues values1 = new ContentValues();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//variables are extracted from the bundle
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String status = bundle.getString("status");
String roomValue = bundle.getString("roomValue");
myOpenHelper = new roomDatabase(this, DATABASE_NAME, null, VERSION_NUMBER);
SQLiteDatabase db = myOpenHelper.getWritableDatabase();
values1.put("status", status);
values1.put("roomValue", roomValue);
Toast.makeText(LogsActivity.this, status, Toast.LENGTH_LONG).show();
//Data is entered into the table
db.insert(roomDatabase.TABLE_NAME, null, values1);
Intent newIntentBundle = new Intent(LogsActivity.this, MainActivity.class);
Bundle bundleA = new Bundle();
bundle.putString("status", status);
bundle.putString("roomValue", roomValue);
newIntentBundle.putExtras(bundle);
startActivity(newIntentBundle);
}
}
注意 - 我知道这可能不是正确的做法,因为这意味着在 Main Activity 中创建一个新数据库并给适配器带来麻烦,所以任何帮助将不胜感激。 :)
为数据库创建一个单独的 class。以下是有关如何在 android.
中创建 SQLite 数据库的一些链接这些是在 RoomDatabase 中保存和获取的方法 class:-
public boolean addDetails(String status,String roomValue){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(KEY_STATUS,status);
values.put(KEY_ROOMVALUE,roomValue);
db.insert(TABLE_NAME,null,values);
db.close();
return true;
}
public List<String> getAllDetail(){
List<String> detailList=new ArrayList<>();
SQLiteDatabase db=this.getReadableDatabase();
Cursor cursor=db.rawQuery("select * from "+TABLE_NAME,null);
String status,roomValue;
if(cursor.moveToFirst()){
do{
status=cursor.getString(cursor.getColumnIndex(KEY_STATUS));
roomValue=cursor.getString(cursor.getColumnIndex(KEY_ROOMVALUE));
detailList.add(status);
detailList.add(roomValue);
}while (cursor.moveToNext());
}
return detailList;
}
在你的 activity 你必须保存在数据库中的地方:
RoomDatabase roomDatabase=new RoomDatabase(getContext());
roomDatabase.addDetails(status,roomValue);
在您的 activity 中,您必须从数据库中检索:
RoomDatabase roomDatabase=new RoomDatabase(getContext());
List<String> detailList=db.getAllDetail();
在detailList中可以得到值。