0

I have 2 android activity's

One where I have a room list, i.e. list of rooms.

The other the room contents. Where all the content of a selected room is displayed.

All I want is for the room id(r_id) to be passed through the room list class into the room overview class where I can retreive all its contents from the database.

I've been trying to figure out where i'm going wrong, I think its around line 94 - 101:

            selectedRoom = (String) ((TextView) view).getText();
            Cursor c2 = sampleDB.rawQuery("SELECT * FROM tbl_roomDesc WHERE roomName =?", new String[] {selectedRoom});

            if (c2 != null ) {
                if  (c2.moveToFirst()) {
                    do {
                        r_id = c2.getString(c2.getColumnIndex("r_id")); 
                    }while (c2.moveToNext());
                }
            }
            c2.close();

Can anyone help me figure out what i'm doing wrong here?

Here is the source for the room list

Here is the source for the room overview

Thanks in advance!

cwiggo
  • 2,541
  • 9
  • 44
  • 87
  • you need to use `Intent.putExtra()` to pass parameter to another activity. – ariefbayu Apr 24 '13 at 09:09
  • also check this answer in case you have large data to pass to an activity http://stackoverflow.com/questions/12819617/issue-passing-large-data-to-second-activity/14706456#14706456 – Rahim Apr 24 '13 at 09:18

2 Answers2

1

To pass data between to Intents, use the putExtras() method.

Example:

Intent i = new Intent(...);
i.putExtras("roomid", "10");
startActivity(i);
Savv
  • 433
  • 2
  • 7
0
     final int roomId = r_id;
     next = (ImageButton) findViewById(R.id.next);
     next.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {

            CreateBuilding.val = "Retrieve";
            sampleDB.close();
            Intent intent2 = new Intent(RoomList.this, TabLayoutActivity.class);
            intent2.putExtra("roomId", roomId);
            setResult(RESULT_OK, intent2);
            finish();
            startActivityForResult(intent2, 0);
        }
    });

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
       if (resultCode == Activity.RESULT_OK) {
          if (data != null) {
            int roomId = data. getIntExtra("roomId", 0);
          }
       }
    }

If I have not misunderstood you put the roomId inside the Intent and retrieve it inside the onActivityResult

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • how do I retrieve the roomId in onActivityResult? – cwiggo Apr 24 '13 at 09:12
  • i'm finding it difficult to understand :( first off, where to put onActivityResult? – cwiggo Apr 24 '13 at 09:23
  • I can not find where do you start RoomOverview – Blackbelt Apr 24 '13 at 09:31
  • that's clear. My question is Where do you start RoomOverview? – Blackbelt Apr 24 '13 at 09:34
  • Altough this is not a bad idea, in practice, it makes more sense to send something to another activity through an intent, and use the `onActivityResult` to retrieve the result of that action. Id go with the answer of SavTheCoder – Janis Peisenieks Apr 24 '13 at 09:35
  • Oh, sorry, TabLayoutActivity.class is where I start RoomOverview. Intent intent2 = new Intent(RoomList.this, TabLayoutActivity.class); In the next.SetOnClickListener, which then starts up this class http://pastebin.com/STAChwjd which starts up roomOverview – cwiggo Apr 24 '13 at 09:36
  • you should override onActivityResult inside TabLayoutActivity. You should find a way to add the roomId value in the intent fo RoomOverview – Blackbelt Apr 24 '13 at 09:43
  • What way could I pass it from inside TablayoutActivity? – cwiggo Apr 24 '13 at 10:04