0

An assignment of mine requires me to create a list of objects generated from IBM Watson. I have figured out how to add the singular object to the list, but am unsure of how to save that item to the list when I navigate back to the image selection. Currently, the ArrayList wipes.

I can get it to essentially duplicate the same object, but everything disappears when I navigate back.

I was wondering if anyone knew of a way that would retain my array onDestroy, or at least can see an issue in my code which would be causing my array to wipe onCreate. Do I need to make the ArrayList a global variable? Any help is appreciated. Thanks.

  • I'm using a design xml for the list, I don't think it's relevant to the issue, but can add if need be
  • captureLoad is the activity where I select the image. It is a couple of activities back from this one

https://i.stack.imgur.com/VXoKl.jpg <= screenshots of my issue

public class listViewActivity extends AppCompatActivity {
    //What I need to do is import extras from last activity as attributes of the class.
    //Import first in onCreate, then call on ImageList class and assign variables

    ArrayList<ImageList> results = new ArrayList<ImageList>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);
        Bundle Text = getIntent().getExtras();
        String label = Text.getString("ibmtext");
        Bundle Sc = getIntent().getExtras();
        String score = Sc.getString("ibmscore");
        byte[] byteArray = getIntent().getByteArrayExtra("ibmimage");
        Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
        results.add(new ImageList(label, bmp, score));
        ImageListAdapter adapter = new ImageListAdapter(
                this, R.layout.my_listview_item, results
        );
        ListView listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(adapter);
        //Need to add an array to add the objects to so they are stored, because currently they
        //disappear onDestroy
        //Add button needs to go back to captureLoad activity
        //onClick needs to be new activity similar to edit where you either remove object from          the array or update it's attributes

    }

    public void add(View view) {
        //The add button adds but the arraylist is wiped when you return. Make it stay
        returnToCameraLoad();
        byte[] byteArray = getIntent().getByteArrayExtra("ibmimage");
        Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
        results.add(new ImageList("Test", bmp, "Testin"));
        ImageListAdapter adapter = new ImageListAdapter(
                this, R.layout.my_listview_item, results
        );
        ListView listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(adapter);
        }

    public void returnToCameraLoad(){
        Intent intent = new Intent(this, captureLoad.class);
        intent.putExtra("ibmwatson", "ibmwatson");
        startActivity(intent);
    }
}
MCLyonzo
  • 3
  • 4
  • Check the answers in here. This may help you https://stackoverflow.com/questions/1944656/android-global-variable – udi Mar 19 '21 at 08:20

2 Answers2

0

From what I understand your problem can be solved by using startActivityForResult() instead of startActivity

Take a look at this : https://www.javatpoint.com/android-startactivityforresult-example

0

What worked was making my ArrayList a static variable rather than just a normal ArrayList. This seems to allow the list to retain its data.

I also declared it above my onCreate, which may or may not have made a difference (Java is not my first language)

MCLyonzo
  • 3
  • 4