skip to Main Content

How can i send/receive an updated ArrayList using bundle.
Here is my code:

File: Epargne.java (apply interest to all accounts and return an updated ArrayList)

    public ArrayList<Epargne> paiementInterets () {
        Guichet guichet = new Guichet();
        ArrayList<Epargne> epargneList = new ArrayList<Epargne>(guichet.listEpargne);
        guichet.initListEpargne(epargneList);

        epargneList.forEach((item) -> {
            float nouveauBalance =  item.getSoldeCompte() * 1.1;
            item.setSoldeCompte(nouveauBalance);
        });
        return epargneList;
    }

File: Admin.java (receive updated ArrayList and send it to ListEpargne.java using Bundle)

    public void onClick_Admin_PaiementInteret(View view) {
        Guichet guichet = new Guichet();
        Epargne epargne = new Epargne();
        ArrayList<Epargne> epargneList = (ArrayList<Epargne>) epargne.paiementInterets();

        Intent i = new Intent(Admin.this, ListeEpargne.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("arraylist", epargneList);
        i.putExtras(bundle);

        Toast.makeText(this, "Intérêts de 1.25% est appliqué sur les comptes épargne",
                        Toast.LENGTH_LONG).show();
    }


    // when button is clicked, it display updated ArrayList (updated account balance) 
    public void onClick_ListEpargne(View view) {
        Intent listEpargne = new Intent(this, ListeEpargne.class);
        startActivity(listEpargne);
    }

File: ListeEpargne.java (receive updated ArrayList and pass it to adapter for display)

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_liste_epargne);

        Guichet guichet = new Guichet();
        ArrayList<Epargne> epargneList = new ArrayList<Epargne>(guichet.listEpargne);
        guichet.initListEpargne(epargneList);

        Bundle bundleObject = getIntent().getExtras();
        if (bundleObject != null) {
            epargneList = (ArrayList<Epargne>) bundleObject.getSerializable("arraylist");
        }

        EpargneAdapter adapterEpargne = new EpargneAdapter(ListeEpargne.this, R.layout.liste_comptes, epargneList);
        final ListView listEpargne = (ListView) findViewById(R.id.maListe);

        listEpargne.setAdapter(adapterEpargne);
    }

ISSUE: bank balance doesn’t get updated after I applied the interest. It seems like I am unable to pass the updated ArrayList to the adapter in ListeEpargne.java. I’m not getting any error message.

Can someone help help me figure out what I am doing wrong please. It has something to do with passing the updated ArrayList from Admin.java to ListeEpargne.java

2

Answers


  1. Direct with intent you can use:

    intent.putExtra("list",arraylist)
    

    to receive it use:

    arraylist=intent.getSerializedExtra<Typeof arraylist>("list")
    
    Login or Signup to reply.
  2. The intent you put the array and the intent you use to start the new Activity are not same one.

    The intent you use to startActivity is empty, it does not have the array. Try to put the list in that intent.

    Something like this:

    public void onClick_ListEpargne(View view) {
        Intent listEpargne = new Intent(this, ListeEpargne.class);
    
        Bundle bundle = new Bundle();
        bundle.putSerializable("arraylist", epargneList);
        listEpargne.putExtras(bundle);
    
        startActivity(listEpargne);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search