1

new to Java here :)

The problem I'm facing is that I am unable to display the database from the Main Activity with a click of a button. The data in the database is being passed from another activity, would like to know if I'm missing anything or how would I go about fixing this. Thanks :)

(Method being used in the Main Activity after the onCreate)

MainActivity

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

LogsActivity

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

NOTE - I am aware this may not be right way to do as it would mean creating a new database in the Main Activity and trouble with the adapter, so any help would be appreciated. :)

John Joe
  • 12,412
  • 16
  • 70
  • 135
Adi
  • 13
  • 3
  • pass data through bundle for reference see this http://stackoverflow.com/questions/15445182/passing-data-from-one-activity-to-another-using-bundle-not-displaying-in-secon – rookieDeveloper Dec 09 '16 at 05:03

2 Answers2

0

Create a separate class for Database. Here are some links to how to create SQLite database in android.

Nainal
  • 1,728
  • 14
  • 27
  • I have created a seperate class for the database (called RoomDatabase). I want the data (in the database) to be displayed in the Main Activity when a widget button is pressed. – Adi Dec 19 '16 at 09:46
  • save the values you want to store in database in one activity and retrieve it from database in another activity – Nainal Dec 19 '16 at 10:06
  • You can refer the links which I have provided – Nainal Dec 19 '16 at 10:08
  • My apologies but i cannot seem to be able to find how I can display the data from another activity. Could you be more specific in how I can do this? and does this mean I cannot use the method shown above, DisplayDataInTable(), in a different activity? – Adi Dec 20 '16 at 05:39
  • Thank you so much for the help, highly appreciated :) – Adi Dec 21 '16 at 04:37
0

These are the methods to save and fetch in 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;
    }

In your activity where you have to save in db:

RoomDatabase  roomDatabase=new RoomDatabase(getContext());
roomDatabase.addDetails(status,roomValue);

In your activity where you have to retrieve from db:

RoomDatabase  roomDatabase=new RoomDatabase(getContext());
List<String> detailList=db.getAllDetail();

in detailList you can get the values.

Nainal
  • 1,728
  • 14
  • 27