【问题标题】:Save Object into Array onDestroy Android Studio将对象保存到数组 onDestroy Android Studio
【发布时间】:2021-03-19 08:01:58
【问题描述】:

我的一个任务要求我创建一个从 IBM Watson 生成的对象列表。我已经弄清楚如何将单个对象添加到列表中,但是当我导航回图像选择时,我不确定如何将该项目保存到列表中。目前,ArrayList 擦除。

我可以让它基本上复制同一个对象,但是当我返回时一切都消失了。

我想知道是否有人知道保留我的数组 onDestroy 的方法,或者至少可以在我的代码中看到一个问题,该问题会导致我的数组在 onCreate 上擦除。我需要将 ArrayList 设为全局变量吗? 任何帮助表示赞赏。谢谢。

  • 我正在为列表使用设计 xml,我认为它与问题无关,但如果需要可以添加
  • captureLoad 是我选择图像的活动。这是从这个回来的几个活动

https://imgur.com/a/5stM4HJ

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

【问题讨论】:

标签: java android arraylist ibm-watson


【解决方案1】:

据我了解,您的问题可以通过使用 startActivityForResult() 而不是 startActivity 来解决

看看这个:https://www.javatpoint.com/android-startactivityforresult-example

【讨论】:

  • 感谢您的建议。我试图实现它,但它需要一个 requestCode,那会是启动相机/画廊的 resultCode 吗?还是换一个?
  • 还是一样的。
【解决方案2】:

有效的是使我的 ArrayList 成为静态变量,而不仅仅是普通的 ArrayList。这似乎允许列表保留其数据。

我还在我的 onCreate 上方声明了它,这可能会或可能不会有所作为(Java 不是我的第一语言)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-13
    • 1970-01-01
    • 1970-01-01
    • 2021-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多