简体   繁体   中英

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

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

您正在调用startActivity ,其intent是没有将int作为额外的。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM