Holly
Holly

Reputation: 1976

How do i use putExtra/get_Extra when working with intents?

I have a list where each item is a row in an sqlite table, i'm trying to pass the row id of the item they clicked on from one activity to the next. Here's the method im trying to set up in activity A,

public static final int REQUEST_CODE=0; //variable used when i want to startActivityForResult..

public void ListItemCommonIntent(long id, View view){
    Log.i("Blah", "current item pressed is" + id);
    Intent keypadIntent = new Intent(view.getContext(), Keypad.class);
    keypadIntent.putExtra(Keypad.selectedRowId, id);
    startActivityForResult (keypadIntent, REQUEST_CODE);
}

And here's where i'm trying to get the id in activity b and put it in selectedRowId

public Long selectedRowId;
private String findBudgetQuery = "SELECT BUDGET_AMOUNT FROM CAT_BUD_TAB WHERE _ID='" + selectedRowId + "'";

public void FindBudgetForId(){
    //This method should query for current budget and..
    SQLiteDatabase db = budgetData.getWritableDatabase();
    selectedRowId = getIntent().getLongExtra(selectedRowId, 1);
    db.rawQuery(findBudgetQuery, null);
}

However i just cant get it to work, i think i need more information, can someone explain to how to use putExtra and the get something on the otherside. I think i don't fully understand the parametres on even method.

Upvotes: 2

Views: 9475

Answers (2)

Blumer
Blumer

Reputation: 5035

The first parameter of putExtra and getExtra should be a string that you use to identify the information you're passing. For example:

intent.putExtra("mySelectedRowId", rowId);

You can use this same key to fetch the information later, the second parameter being a default value to use if the key is not found:

selectedId = intent.getLongExtra("mySelectedRowId", 1);

Upvotes: 2

iarwain01
iarwain01

Reputation: 424

Starting an intent with an extra field.

   public void onClick(DialogInterface dialog, int id) {
       Intent intent = new Intent(mContext, org.gpsagenda.DetailsContainer.class);
       intent.putExtra("id", item.ID());
       mContext.startActivity(intent);
   }

Getting the extra field in the started activity.

int id = getIntent().getExtras().getInt("id");

Beware though that you can get null if the Extra is not set!

Upvotes: 4

Related Questions