【发布时间】:2021-03-19 08:01:58
【问题描述】:
我的一个任务要求我创建一个从 IBM Watson 生成的对象列表。我已经弄清楚如何将单个对象添加到列表中,但是当我导航回图像选择时,我不确定如何将该项目保存到列表中。目前,ArrayList 擦除。
我可以让它基本上复制同一个对象,但是当我返回时一切都消失了。
我想知道是否有人知道保留我的数组 onDestroy 的方法,或者至少可以在我的代码中看到一个问题,该问题会导致我的数组在 onCreate 上擦除。我需要将 ArrayList 设为全局变量吗? 任何帮助表示赞赏。谢谢。
- 我正在为列表使用设计 xml,我认为它与问题无关,但如果需要可以添加
- captureLoad 是我选择图像的活动。这是从这个回来的几个活动
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);
}
}
【问题讨论】:
-
在这里查看答案。这可能会帮助你stackoverflow.com/questions/1944656/android-global-variable
标签: java android arraylist ibm-watson