从一个活动传递数据并从主活动显示数据库

时间:2016-12-09 05:00:30

标签: java android android-database

这里的Java新手:)

我面临的问题是,我无法通过单击按钮从主活动中显示数据库。数据库中的数据正在从另一个活动传递,想知道我是否遗漏了什么或者我将如何解决这个问题。谢谢:))

(在onCreate之后的主要活动中使用的方法)

MainActivity

user

LogsActivity

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);
        }
    }

注意 - 我知道这可能不是正确的方法,因为它意味着在主要活动中创建一个新的数据库并且适配器出现问题,所以任何帮助都将不胜感激。 :)

2 个答案:

答案 0 :(得分:0)

为Database创建一个单独的类。以下是一些如何在android中创建SQLite数据库的链接。

答案 1 :(得分:0)

这些是在RoomDatabase类中保存和获取的方法: -

    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;
    }

在您必须以db:

保存的活动中
RoomDatabase  roomDatabase=new RoomDatabase(getContext());
roomDatabase.addDetails(status,roomValue);

在您必须从db:

中检索的活动中
RoomDatabase  roomDatabase=new RoomDatabase(getContext());
List<String> detailList=db.getAllDetail();

在detailList中,您可以获取值。