-1

I'm student and developing online food booking app . I'm getting food list from api and displaying in list.On + button I'm able to add item in cart but I want on cart image click , I move on new activity and there I want to get all added items list and display in list view.1) 1st code snippet it my adapter class where I have + button to increase the item count .2) 2nd Code is my model class 3) 3rd code snippet is my fragment code where on button click i want to move on order class.

I advance thanks.Please help me.

 static ArrayList<AddedPizzaInfo> bookmarker_array = new ArrayList<AddedPizzaInfo>();
 holder.increase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                count++;
                holder.integer_number.setText(String.valueOf(count));
                total_value = holder.integer_number.getText().toString();if(actionListener!=null)actionListener.onItemTap(holder.itemCopyIV);
                AddedPizzaInfo bookmarkerPojo = new AddedPizzaInfo();
                bookmarkerPojo.SetName(c.getName());
                bookmarker_array.add( bookmarkerPojo);// adding item but
=======
public class AddedPizzaInfo {
private String name = "";
public AddedPizzaInfo() {}
public AddedPizzaInfo( String name) {
    this.name = name;
}
public void SetName(String name){this.name = name;}
public String GetName(String name){
    return this.name;
}

}

====textCartItemCount.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent startSecondActivityIntent = new Intent(Veg_Pizza_Activity.this, Order_Summary.class); startSecondActivityIntent.putExtra("items", Parcels.wrap(bookmarker_array)); startActivity(startSecondActivityIntent); (Veg_Pizza_Activity.this).overridePendingTransition(0, 0); } });

lavika
  • 39
  • 5

2 Answers2

0

Make your Item class implement @Parcel (using this library https://github.com/johncarl81/parceler)

then, on the first activity:

Intent startSecondActivityIntent = new Intent(this, SecondActivity.class);
startSecondActivityIntent.putExtra("items", Parcels.wrap(yourListOnItems));
startActivity(startSecondActivityIntent);

on the second acitivty:

@Override
protected void onCreate(Bundle savedInstanceState) {
    Bundle bundle = this.getIntent().getExtras();
    listOfItems = Parcels.unwrap(bundle.getParcelable("items"));
}

Alternatively to using Parceler library you could implement Parceleable natively but will take too much time and generate too much boilerplate code.

EDIT:

AddedPizzaInfo.class:

@Parcel
public class AddedPizzaInfo{
    //code whatever

    //add empty constructer, required by parceler
    public AddedPizzaInfo(){} 

    //rest of the code
}

build.gradle:

...

dependencies {

   ///other dependencies
    implementation 'org.parceler:parceler-api:1.1.9'
    annotationProcessor 'org.parceler:parceler:1.1.9'
}

and then run the code I first added when you want to send the objects you your second activity.

Tiago Ornelas
  • 1,109
  • 8
  • 21
  • holder.increase.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {count++; holder.integer_number.setText(String.valueOf(count)); total_value = holder.integer_number.getText().toString(); if(actionListener!=null)actionListener.onItemTap(holder.itemCopyIV);AddedPizzaInfo bookmarkerPojo = new AddedPizzaInfo();bookmarkerPojo.SetName(c.getName());bookmarker_array.add( bookmarkerPojo);}); – lavika Jul 24 '18 at 10:59
  • adding by this way . I have shared my code above sir, now please share where I'm doing mistake. – lavika Jul 24 '18 at 11:00
  • holder.increase.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {count++; holder.integer_number.setText(String.valueOf(count)); total_value = holder.integer_number.getText().toString(); if(actionListener!=null)actionListener.onItemTap(holder.itemCopyIV);AddedPizzaInfo bookmarkerPojo = new AddedPizzaInfo();bookmarkerPojo.SetName(c.getName());bookmarker_array.add( bookmarkerPojo);}); – lavika Jul 24 '18 at 11:14
  • Edited my answer. The code you provided is very unclear. – Tiago Ornelas Jul 24 '18 at 11:22
  • "listOfItems" what is it? will you plz tell me sir? – lavika Jul 24 '18 at 11:39
  • it's and ArrayList – Tiago Ornelas Jul 24 '18 at 11:44
  • Unable to find generated Parcelable class for com.pizza_App.AddedPizzaInfo, verify that your class is configured properly and that the Parcelable .Getting this error. – lavika Jul 24 '18 at 11:59
  • Check here https://github.com/johncarl81/parceler an example of a class implementing Parcel. Check if your is the same – Tiago Ornelas Jul 24 '18 at 12:53
  • all is same sir – lavika Jul 24 '18 at 12:57
  • Okay Sir @ Tiago Ormelas – lavika Jul 25 '18 at 04:35
0

To send list data from one activity use the following code sample

Intent intent = new Intent(this, TargetActivity.class);
        intent.putStringArrayListExtra("your_list",listOfData);
        startActivity(intent);

To retrieve the list in TargetActivity use

if(getIntent()!=null){
    your_array_list = getIntent().getStringArrayListExtra("your_list");
}
Trenton Trama
  • 4,890
  • 1
  • 22
  • 27