2012-07-26 109 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

相關問題