0

I'm trying to create an app like shopping cart Using this to access my database http://www.tutecentral.com/restful-api-for-android-part-2/

And i'm stuck at adding products to cart, so far I understand that the selected products go to arraylist in a few tutorials. In the code below I have two Activities, the MaterialView (this shows the details of the materials and has the option to add to cart), and the MaterialCart (shows the list of selected products.)

this is the block of code in MaterialView to send the values to MaterialCart

  ButtonAdd.setOnClickListener(new View.OnClickListener(){

        public void onClick (View view){

            Intent i=new Intent(MaterialView.this, MaterialCart.class);
            i.putExtra("mID", mid);
            i.putExtra("name", Name.getText().toString());
            i.putExtra("qty", Qty.getText().toString());
            i.putExtra("price", Price.getText().toString());
            i.putExtra("stock", Stock.getText().toString());
            i.putExtra("rqQty", RqQty.getText().toString());
            startActivity(i);

           Toast.makeText(MaterialView.this, "Added Succesfully.", Toast.LENGTH_LONG).show();

        }


    } );

I have used Intent to pass the values (I'm pretty sure this method is wrong, I also tried calling the MaterialCart class itself to access the arrayList so I can add values and it didn't work)

This is the block of codes in my MaterialCart to receive the values

public class MaterialCart extends Activity {

final ArrayList<PropertyCartTable> materialProperties = new ArrayList<>();

@SuppressLint("LongLogTag")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_material_cart);

    Intent i = new Intent();
    Bundle extras = getIntent().getExtras();

    try{
        String Name = extras.getString("name");
        String Qty = extras.getString("qty");
        String Price = extras.getString("price");
        String Stock = extras.getString("stock");
        String RqQty = extras.getString("rqQty");
        String ID = extras.getString("mID");

        Log.d("EXTRAS:", Name + " " + Qty + " " + ID);
        materialProperties.add(new PropertyCartTable( ID,Name,Qty,Price,Stock,RqQty));

        getIntent().removeExtra("Name");
        getIntent().removeExtra("Qty");
        getIntent().removeExtra("Price");
        getIntent().removeExtra("Stock");
        getIntent().removeExtra("RqQty");
        getIntent().removeExtra("MID");


    }
    catch (Exception h){
        Log.d("Exception!",h.toString());
    }



   // materialProperties.add(array);
    Log.d("MaterialView.Cart isEmpty", String.valueOf(materialProperties.isEmpty()));

    if(materialProperties.isEmpty()) {

        Toast.makeText(this, "You have no materials to request.", Toast.LENGTH_LONG).show();
        i = new Intent(MaterialCart.this, ProductDetails.class);
        startActivity(i);
    }else{
        ArrayAdapter<PropertyCartTable> adapter = new propertyArrayAdapter(this, 0, materialProperties);
        ListView listView = (ListView) findViewById(R.id.lv_materialcart);
        listView.setAdapter(adapter);
    }


}

The codes work for receiving the values, but when I go back to the materialView (or choose another product) the ArrayList doesn't append the values.

What I'm trying to achieve here is to add the values from the MaterialView (even if the user adds many prodducts) to MaterialCart's ArrayList.

kol
  • 41
  • 8
  • 1
    u need to use a combination of SharedPrefs and startActivityForResult methods – MadScientist Mar 16 '17 at 06:43
  • you have 2 options, you can use `StartActivityForResult` or convert it to use fragment and saving its instance in the backstack. – Rashid Mar 16 '17 at 06:47
  • @SecretCoder the StartActivityForResult, can it store multiple arraylistvalues too? I found this http://stackoverflow.com/a/40969871/3681880 and it shows a single string of values from second activity to main activity. can it be treated as a temporary list of tables I can store values to? I'll be accessing three activities in total. I've only started to use android studio three ddays ago and this confuses me. – kol Mar 16 '17 at 07:15
  • @kol yes it can by using `Bundle` . package your array in a bundle and pass it in your `startActyivityForResult` – Rashid Mar 16 '17 at 07:16
  • @SecretCoder I keep getting an error, something with parcelable, I was trying to pass the whole ArrayList so I tried to do another method by using ArrayList instead but the result ended with the same error when I passed the ArrayList to the ArrayList . I also tried xiaoyuan's answer but still got the same error. – kol Mar 17 '17 at 12:55
  • @kol the PropertyCartTable must extend to Parcelable if you want to past an Arraylist of Object – Rashid Mar 20 '17 at 02:44

1 Answers1

2

You can let your Application contain the data:

public class MyApp extends Application {

    private static List<String> data = new ArrayList<>();

    public static void addItem(String item) {
        data.add(item);
    }

    public static List<String> getData() {
        return data;
    }
}

And when button is clicked: ButtonAdd.setOnClickListener(new View.OnClickListener(){

    public void onClick (View view){
        MyApp.addItem(your item);
        Intent i=new Intent(MaterialView.this, MaterialCart.class);
        startActivity(i);
    }


} );

And in MaterialCart.class:

   List<String> data = MyApp.getData();

But remember:data will be clear when app is closed.And if you want save it locally,you need to use SharedPreferences

xiaoyuan
  • 423
  • 4
  • 13