How to pass bean class one activity to another activity on android

前端 未结 4 1932
情书的邮戳
情书的邮戳 2021-01-16 11:25

Hi this is my listview onClicklister.

when i click the list item , I pass the the arraylist which is getting from bean class one activity to another activity like

相关标签:
4条回答
  • 2021-01-16 11:37

    I think it is possible. You have to send the Object of your class like this,

       intent.putExtra("RouteBean", bean); 
    

    And retrieve it like this in your next activity,

    getIntent().getSerializableExtra("RouteBean");
    

    But your class has to implement Serializable Interface.

    Or you can use Parcelable Interface,

    Here is a Example,

    https://stackoverflow.com/a/6923794/603744

    For the first method, your class should be like this,

    public class RouteBean implements Serializable 
    {
    
    }
    

    And for the next one,

    public class RouteBean implements Parcelable 
    {
    
    }
    
    0 讨论(0)
  • 2021-01-16 11:39

    Make your RouteBean class implements Parcelable interface. Then you can pass your custom class objects as bundle in intent to other activity.

    You can then use-

    class RouteBean implements Parceable Then while calling intent.

    Bundle bundle = new Bundle();
    RouteBean yourObj = new RouteBean();
    bundle.putParcelable("bundlename", yourObj);
    

    And in next Activity you can use

    RouteBean yourObj bundle.getParcelable("bundlename");
    

    More info on Parceable http://developer.android.com/reference/android/os/Parcelable.html.

    0 讨论(0)
  • 2021-01-16 11:40

    You can pass your object using Parcelable class..

    something like,

    public class RouteBean implements Parcelable {
    
    }
    

    Once you have your objects implement Parcelable it's just a matter of putting them into your Intents with putExtra():

    Intent i = new Intent();
    i.putExtra("object", Parcelable_Object);
    

    Then you can pull them back out with getParcelableExtra():

    Intent i = getIntent();
    RouteBean bean = (RouteBean) i.getParcelableExtra("object");
    

    For more info look at this SO question How to pass an object from one activity to another on Android

    0 讨论(0)
  • 2021-01-16 11:59

    Yes you can do that by in1.putExtra("beanObject", bean).

    public void onItemClick(AdapterView<?> arg0, View arg1,
                    int position, long id) {
    
                bean = (ActivitiesBean) adapter.getItem(position); //ActivitiesBean is the name of the bean class
    
                Intent in1 = new Intent(firstclass.this, secondclass.class);
                in1.putExtra("beanObject", bean);
                startActivity(in1);
            }
    
        });
    

    and use this for the secondclass.java

    ActivitiesBean bean =  (ActivitiesBean) getIntent().getSerializableExtra("beanObject");
    txt_title.setText(bean.getTitle());  //txt_title is the object of the textview 
    
    0 讨论(0)
提交回复
热议问题