How to get added items list from cart on next activity

时间:2018-07-24 10:18:42

标签: android

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

2 个答案:

答案 0 :(得分:0)

使您的Item类实现@Parcel(使用此库https://github.com/johncarl81/parceler

然后,在第一个活动中:

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

关于第二个活动:

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

除了使用Parceler库之外,您可以实现Parceleable natively,但是会花费太多时间并生成太多样板代码。

编辑:

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'
}

,然后运行我想在第二次活动中发送对象时首先添加的代码。

答案 1 :(得分:0)

要从一个活动发送列表数据,请使用以下代码示例

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

要在TargetActivity中检索列表,请使用

if(getIntent()!=null){
    your_array_list = getIntent().getStringArrayListExtra("your_list");
}