1

こんにちはこれは私のlistviewonClicklisterです。

リスト項目をクリックすると、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);
      }
    });

「RouteBean」を渡すと、次のアクティビティの値を取得します。

Beanクラスを渡すことは可能ですか?

4

4 に答える 4

5

Parcelableclassを使用してオブジェクトを渡すことができます。

何かのようなもの、

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

詳細については、このSOの質問をご覧ください。Androidで1つのアクティビティから別のアクティビティにオブジェクトを渡す方法

于 2012-07-26T07:17:11.620 に答える
1

可能だと思います。このようにクラスのオブジェクトを送信する必要があります。

   intent.putExtra("RouteBean", bean); 

そして、次のアクティビティでこのように取得します。

getIntent().getSerializableExtra("RouteBean");

ただし、クラスはSerializableインターフェイスを実装する必要があります。

または、Parcelable Interface を使用できます。

ここに例があります、

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

最初のメソッドの場合、クラスは次のようになります。

public class RouteBean implements Serializable 
{

}

そして次は、

public class RouteBean implements Parcelable 
{

}
于 2012-07-26T07:14:51.080 に答える
1

RouteBean クラスがParcelableインターフェースを実装するようにします。次に、カスタム クラス オブジェクトをインテント バンドルとして他のアクティビティに渡すことができます。

その後、使用できます-

クラス RouteBean は、インテントの呼び出し中に Parceable Then を実装します。

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

そして、次のアクティビティで使用できます

RouteBean yourObj bundle.getParcelable("bundlename");

Parceable の詳細については、http://developer.android.com/reference/android/os/Parcelable.html を参照してください

于 2012-07-26T07:15:33.457 に答える
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 
于 2012-07-26T07:21:25.367 に答える