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