2012-07-26 108 views
0

嗨,这是我的listview onClicklister。如何将豆类一项活动传递给Android上的另一项活动

当我点击列表中的项目,我通过它从bean类一个活动越来越像下面。另一项活动ArrayList中,

,但我想知道我们可以通过bean类下一个活动?

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

如果我通过“路由Bean”,我得到下一个活动的值。

是否可以传递bean类?

+0

看[这里] [1]。有很多方法可以做到这一点。 [1]:http://stackoverflow.com/questions/11679574/app-design-pass-date-through-intents-or-use-singletons/11679728#11679728 – 2012-07-27 02:56:56

回答

2

您可以使用Parcelable类传递您的对象..

类似,

public class RouteBean implements Parcelable { 

} 

一旦你有你的对象实现Parcelable它只是将它们放在你的意图与putExtra()的事:

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

然后你就可以将他们拉回来了与getParcelableExtra()

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

欲了解更多信息看这太问题How to pass an object from one activity to another on Android

+0

嗨@ user370305,我需要一个又一个帮助,http://stackoverflow.com/questions/11682164/find-center-geopoint-between-start-geo-point-and-end-geo-point-on -android – RVG 2012-07-27 06:43:10

+0

hi @ user370305,我需要另一个帮助,http://stackoverflow.com/questions/12623302/get-meta-data-of-image-android/12623648#12623648 – RVG 2012-09-28 04:47:34

1

我认为这是可能的。你要发送的对象类是这样的,

intent.putExtra("RouteBean", bean); 

并检索它像这样在你的下一个活动,

getIntent().getSerializableExtra("RouteBean"); 

但是你的类必须实现Serializable接口。

或者你可以使用Parcelable接口,

下面是一个例子,

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

对于第一种方法,你的类应该是这样的,

public class RouteBean implements Serializable 
{ 

} 

而对于下一个,

public class RouteBean implements Parcelable 
{ 

} 
+0

感谢@androselva。 – RVG 2012-07-26 07:31:56

1

使您的RouteBean类实现Parcelable接口。然后,您可以将自定义类对象作为捆绑传递给其他活动。

然后,您可以使用 -

类RouteBean实现Parceable 然后同时呼吁意图。

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

而在接下来的活动中,你可以使用Parceable http://developer.android.com/reference/android/os/Parcelable.html

RouteBean yourObj bundle.getParcelable("bundlename"); 

更多信息。

+0

感谢@anujprashar – RVG 2012-07-26 07:32:15

0

是的,你可以做到这一点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); 
     } 

    }); 

,并使用此为secondclass.java

ActivitiesBean bean = (ActivitiesBean) getIntent().getSerializableExtra("beanObject"); 
txt_title.setText(bean.getTitle()); //txt_title is the object of the textview 
+0

谢谢@rahulpatel – RVG 2012-07-26 07:32:39

+0

@ganesh享受! ! – 2012-07-26 08:51:09

相关问题