从通知点击发送数据时,只从arraylist获取一个元素

时间:2017-04-25 09:37:34

标签: android push-notification android-pendingintent

您好我显示了通知,并根据我显示通知的圈子的距离,我想将所有标记emailid发送到待处理的意图活动。

这是我的代码

@Override
public void onLocationChanged(Location location) {
    if (circle != null)
        circle.remove();
    this.location = location;
    double latitude = location.getLatitude();
    double longitude = location.getLongitude();
    LatLng latLng = new LatLng(latitude, longitude);
//        googleMap.addMarker(new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    googleMap.animateCamera(CameraUpdateFactory.zoomTo(14));
    circle = googleMap.addCircle(new CircleOptions()
            .center(new LatLng(latitude, longitude))
            .strokeColor(Color.RED)
            .strokeWidth(2)
            .radius(1000));
    circle.setCenter(latLng);


    float[] distance = new float[2];
    if (!abc.isEmpty()) {
        mar_list=new ArrayList<>();
        for (int i = 0; i < abc.size(); i++) {
            Location.distanceBetween(abc.get(i).latitude, abc.get(i).longitude, circle.getCenter().latitude, circle.getCenter().longitude, distance);

            if (distance[0] <= circle.getRadius()) {
                mar_list.add(locationlist.get(i).get("usrnm"));
                manager1 = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                Intent intent = new Intent(MainActivity.this, OfferActivity.class);
                intent.putExtra("list",mar_list);
                Log.v("TAG",""+mar_list.toString());
                //Here mar_list contain three items but in offer activity only one item is displayed      
                PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
                mBuilder = new NotificationCompat.Builder(this);
                mBuilder.setOngoing(false);
                Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
                mBuilder.setContentTitle("Offer")
                        .setStyle(new NotificationCompat.BigTextStyle().bigText("You Have Entered in Offer zone.To see offer in your area please proceed"))
                        .setContentText("You Have Entered in Offer zone.To see offer in your area please proceed")
                        .setLargeIcon(largeIcon)
                        .setContentIntent(pendingIntent)
                        .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
                        .setAutoCancel(true)
                        .setSmallIcon(R.mipmap.ic_launcher);
                manager1.notify(1, mBuilder.build());



            } else {
                Log.v("TAG ", "" + distance[0] + " radius: " + circle.getRadius());
                Toast.makeText(getBaseContext(), "outside, distance from center: " + distance[0] + " radius: " + circle.getRadius(), Toast.LENGTH_LONG).show();
            }

        }

        Log.v("TAG",""+mar_list.toString());
    } else {
        Log.v("TAG ", "" + distance[0] + " radius: " + circle.getRadius());
        Toast.makeText(getBaseContext(), "outside, distance from center: " + distance[0] + " radius: " + circle.getRadius(), Toast.LENGTH_LONG).show();
    }
}

根据我的代码,它只传递第一个值,我希望所有arraylist传递给下一个活动

这是有效性的

public class OfferActivity extends AppCompatActivity {
ArrayList<String> mylist;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_offer);
    mylist = (ArrayList<String>) getIntent().getSerializableExtra("list");
    Log.v("TAG", "" + mylist.toString());
}

}

2 个答案:

答案 0 :(得分:0)

试试这个:

编译此gradle compile 'com.google.code.gson:gson:2.7'

String str = new Gson().toJson(mar_list);

intent.putExtra("list",str);

从意图中获取列表

YourModelClass model = new Gson().fromJson(str,YourModelClass.class);

答案 1 :(得分:0)

MainActivity.java

改变这个:

 intent.putExtra("list",mar_list);

intent.putStringArrayListExtra("list", mar_list);

<强> offeractivity.java

改变这个:

mylist = (ArrayList<String>) getIntent().getSerializableExtra("list");

mylist = getIntent().getExtras().getStringArrayList("list");