kumisku
kumisku

Reputation: 103

pass integer from listview to another activity

I want to pass the integer (id) from listview (when it is clicked) to TheEndActivity.java . But my code cant retrieve the integer properly.

UsernameList.java (it is the class where the integer comes from)

public void onListItemClick(ListView parent, View view, int position, long id) {         
    Intent i = new Intent( this, TheEndActivity.class ); 
    i.putExtra( "int", (int)id);
    Log.e(LOG_TAG, "id value "+id);
    startActivity( i );

    Intent intent = new Intent(UsernameList.this, QuizAppActivity.class);
    startActivity(intent);
}

TheEndActivity.java (Its the class where i need to pass the integer here. it is created inside onCreate method)

    Intent i = getIntent();
    final int number = i.getIntExtra("int", -1);

and i need to use the integer (id) which i save it into database (the operation is done in TheEndActivity.java)

finishBtn.setOnClickListener(new View.OnClickListener() {           
    @Override
    public void onClick(View v) {

        helper.insertMark(currentGame.getRight(), number );
        Log.e(LOG_TAG, "id value: " + number );
        Intent i = new Intent(TheEndActivity.this, MainMenu.class);
        startActivity(i);
    }
});

Solved

The problem is i called 2 activities. Thank you guys

Upvotes: 1

Views: 369

Answers (2)

Macarse
Macarse

Reputation: 93133

You are calling startActivity with an intent that doesn't have the int as an extra.

Upvotes: 0

bschandramohan
bschandramohan

Reputation: 2006

problem seems to be in calling 2 startActivities. Are TheEndActivity and QuizAppActivity both activities?

  1. You have to change TheEndActivity into an IntentService or AsyncTask if its something doing background activity like saving things into database.
  2. If it's not, then launch only TheEndActivity and allow user to navigate to QuizAppActivity from there.

  3. Also try changing
    Intent i = new Intent( this, TheEndActivity.class ); to Intent i = new Intent( UsernameList.this, TheEndActivity.class );

Upvotes: 4

Related Questions