0

I have 3 Fragments. (HOME,MENU,CART)

When user clicks on items in the MENU FRAGMENT, The clicked items go into a SQLite table(using the INSERT QUERY).

In the Cart Fragment, I use a select query to display the items user has chosen from the MENU FRAGMENT.

So, All this is happening.

PROBLEM:

  1. on MENU FRAGMENT: User clicks on items and adds them to cart (insertion done successfully)
  2. User goes on the CART FRAGEMENT. Updated values not shown in the CART FRAGMENT
  3. Now, If user goes to other fragments, and then comes back , The updated values are shown.

SNAPSHOT:

  1. User adds item 'Sada Uthappa' to the cart from the MENU FRAGMENT.

MENU FRAGMENT

  1. User goes to the CART Fragment now, It shows empty.

enter image description here

  1. User goes to other fragments(like HOME,MENU) and then goes to CART Fragment. Voila. The updated result.

enter image description here

To get the updated data in the CART FRAGMENT, I have to click on the HOME fragment, then MENU fragment then CART fragment.

MENU FRAGMENT CODE:

Here is the code of the MENU Fragment: I call this method on Add to cart Button

public void onAddToCart(){
    databaseHelper.onCreate();
    success=databaseHelper.onInsert(iname.getText().toString(),qty.getText().toString(),t11.getText().toString());
    Toast.makeText(getContext(),"Row id affected : "+success,Toast.LENGTH_SHORT).show();
}

Here is the code onInsert() from DatabaseHelper class:

 public long onInsert(String itemName,String qty,String price){
    SQLiteDatabase db=this.getWritableDatabase();
    ContentValues contentvalue=new ContentValues();
    contentvalue.put("NAME",itemName);
    contentvalue.put("QTY",Integer.parseInt(qty));
    contentvalue.put("PRICE",Integer.parseInt(price));
    long result=db.insert(table,null,contentvalue);
    return result;
}

CART FRAGMENT CODE:

 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
     view=inflater.inflate(R.layout.fragment_mycart,container,false);
     cartlistview=(ListView)view.findViewById(R.id.listView1);

    db=new DatabaseHelper(this.getContext());
    db.onCreate();
    Cursor res=db.onView();
    int len=res.getCount();

    listCartItems = new ArrayList<CartItems>();
    listCartItems.add(new CartItems(0,"Item Name", "Quantity", "Price","Delete"));

    if(len==0)
    {
        Toast.makeText(this.getContext(),"Cart is Empty.",Toast.LENGTH_SHORT).show();
        statusOfCart=false;
    }
    else {
        while (res.moveToNext()) {
            int id=res.getInt(0);
            String itemname = res.getString(1).toString();  // 0 is id, 1 is name, 2 is qty, 3 price
            String itemqty = Integer.toString(res.getInt(2));
            String itemprice = Integer.toString(res.getInt(3)) ;
            Toast.makeText(this.getContext(),itemname,Toast.LENGTH_SHORT).show();
            listCartItems.add(new CartItems(id,itemname, itemqty, itemprice,"X"));
        }
    }
    CartListAdapter cartListAdapter = new CartListAdapter(getContext(), R.layout.cartlist_layout, listCartItems);
    cartlistview.setAdapter(cartListAdapter);

    return view;
}
  • 1
    Log something info in `onCreateView` of `CartFragment`. Check the log when you are in `MenuFragment`. You will know yourself whats happening – Raghunandan Nov 01 '16 at 06:46
  • You're probably not reloading the Fragment after the UPDATE. Or you could simply requery the table and show its contents again. – Phantômaxx Nov 01 '16 at 09:45
  • How to reload a fragment? – Aadesh Kulkarni Nov 01 '16 at 11:44
  • Have a look athis answer of mine: http://stackoverflow.com/questions/20702333/refresh-fragment-at-reload/20702418#20702418 – Phantômaxx Nov 01 '16 at 11:46
  • @Raghunandan That was a good idea. I did that and found myself in greater trouble. When I am in MenuFragment, I recieved the log message declared in the CartFragment. That means, Even when the Fragment is displaying the contents of Menu, It is actually somehow in the Cart fragment as well. Why is that happening ? ;( – Aadesh Kulkarni Nov 01 '16 at 11:52
  • @Rotwang - The App doesnt load the entire fragment when i insert the detach,attach code. It waits for sometime and then crashes. – Aadesh Kulkarni Nov 02 '16 at 17:06

1 Answers1

1

When a view pager is initialized 3 fragments are initialized with current showing one and adjacent two.

So the onCreateView(...) is initialized first.

After you are modifying in first fragment doesn't update on third fragment unless the updation is happened.

So in Fragment there is a method

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
     if(isVisibleToUser){
    //update fragment
    //do you code}
}
noobEinstien
  • 3,147
  • 4
  • 24
  • 42