passing data from list view to another activity

前端 未结 6 2067
再見小時候
再見小時候 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 15:46

    String selectedItem =arrayAdapter.getItem(position);

                Intent intent = new Intent(getApplicationContext(), 
                Your_Second_Activity.class);
                intent.putExtra("selectedItem", selectedItem);
                startActivity(intent);
    
               Second_Activity.Class
               Bundle bundle = getIntent().getExtras();
               String yourItem = bundle.getString("selectedItem");
    
      Now! your selected item is inside in the yourItem Variable...
    
    0 讨论(0)
  • 2020-12-18 15:54

    You can pass data from one activity to another activity:


    see this link


    and to get data for ListView you have to first implement getListView.setOnItemClickListener(), and have to get position of item in ListView and use the index to get data form your adapter from where you are binding data to ListView.


    0 讨论(0)
  • 2020-12-18 15:58
    protected void onListItemClick(ListView l, View v, int position, long id) {
       super.onListItemClick(l, v, position, id);
    
       Object obj = this.getListAdapter().getItem(position);
       String value= obj.toString();
    
       Intent intent= new Intent(CurrrentClass.this,NextClass.class);
       intent.putExtra("value", value);                 
       startActivity(intent);    
    }
    

    Hope this will help you.

    0 讨论(0)
  • 2020-12-18 16:05

    Use Bundle in onClickListner of the listview .

    Bundle will pass data from one activity to Next.

    0 讨论(0)
  • 2020-12-18 16:07

    There are two ways:

    1. Pass it into Intent

      intent.putExtra("jobNo", item.jobNo);
      
    2. Use Application scope

      ((MyApplication) getApplication()).setJobNo(item.jobNo);
      
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题