1

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 below.,

But i want to know can we pass the bean class to next activity?

listViewRoutes.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
          long arg3) {
        RouteBean bean = routeList.get(arg2);
        ArrayList<Double> fromLatitude = bean.getFromLatitude();
        ArrayList<Double> fromLongitude= bean.getFromLongitude();
        ArrayList<Double> toLatitude = bean.getToLatitude();
        ArrayList<Double> toLongitude= bean.getToLongitude();
        Intent intent =new Intent("MapActivityView");
        intent.putExtra("fromLon", fromLongitude);
        intent.putExtra("fromLat", fromLatitude);
        intent.putExtra("toLat", toLatitude);
        intent.putExtra("toLon", toLongitude);
        startActivity(intent);
      }
    });

if i pass the "Route Bean", i get the values on next activity.

Is it possible to pass the bean class ?

RVG
  • 3,538
  • 4
  • 37
  • 64
  • Look [here][1]. There are many ways to do that. [1]: http://stackoverflow.com/questions/11679574/app-design-pass-date-through-intents-or-use-singletons/11679728#11679728 – Roger Garzon Nieto Jul 27 '12 at 02:56

4 Answers4

5

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

Community
  • 1
  • 1
user370305
  • 108,599
  • 23
  • 164
  • 151
  • Hi @user370305, i need another one help, http://stackoverflow.com/questions/11682164/find-center-geopoint-between-start-geo-point-and-end-geo-point-on-android – RVG Jul 27 '12 at 06:43
  • hi @user370305, i need another one help , http://stackoverflow.com/questions/12623302/get-meta-data-of-image-android/12623648#12623648 – RVG Sep 28 '12 at 04:47
1

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 
{

}
Community
  • 1
  • 1
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
1

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.

anujprashar
  • 6,263
  • 7
  • 52
  • 86
0

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 
Rahul Patel
  • 3,823
  • 5
  • 30
  • 46