passing data from list view to another activity

前端 未结 6 2066
再見小時候
再見小時候 2020-12-18 15:23

i want to create an activity, in which, i would like to have a listview, there can be about 20-30 items in a list view,on tapping any particular value in listview, it should

6条回答
  •  隐瞒了意图╮
    2020-12-18 16:12

    Implement ListView's OnItemClickListener, once you handle this event, try to get the location of the row that was clicked.

    Once you get it, access that particular row position in the source array (or whatever else you're having). This way, you'll have the data that you want to pass to another activity.

    Now use this code:

    Intent anotherActivityIntent = new Intent(this, AnotherActivity.class);
    anotherActivityIntent.putExtra("my.package.dataToPass",dataFromClickedRow);
    startActivity(anotherActivityIntent);
    

    and when the anotherActivityIntent starts the AnotherActivity class, use following code to access the value that you had passed:

    Bundle recdData = getIntent().getExtras();
    String myVal = recdData.getString("my.package.dataToPass");
    

    Now you have your data in myVal variable. you can use any other data type, whichever you like.

提交回复
热议问题