You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio中ArrayList存储Item跨Activity按序保存问题求助

Android ArrayList存储Item对象按序追加功能实现方案

错误原因分析

  • 集合类型误用:你混淆了数组和ArrayList的用法,ArrayList<Item>[]是存储ArrayList对象的数组,你的需求只是存储多个Item对象,直接使用单个ArrayList<Item>即可。ArrayList本身是动态扩容的有序集合,调用add()方法会自动将元素追加到列表末尾,对应下一个索引位置,完全满足你按编号存储的需求,不需要额外声明数组。
  • 变量作用域错误:你之前将itemlist定义在onCreate方法内,属于局部变量,每次MainActivity3触发onCreate都会新建一个空列表,之前存储的内容会全部丢失。
  • 计数变量逻辑错误:你写的int loop=0; loop++是局部变量,每次执行onCreate都会重新初始化loop为0,无法实现累加计数的效果,而且ArrayList本身已经维护了索引,不需要额外声明计数变量。

正确实现代码

package com.example.java;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity3 extends AppCompatActivity {
    private static final String Tag = "MainAcitivity3";
    // 把列表、适配器、ListView提为成员变量,全局可访问
    private ArrayList<Item> itemList;
    private PersonListAdapter adapter;
    private ListView mListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        Log.d(Tag, "onCreate: Started.");
        
        // 初始化控件和列表,只在第一次创建Activity时执行一次
        mListView = findViewById(R.id.listView);
        itemList = new ArrayList<>();
        adapter = new PersonListAdapter(this, R.layout.adapter_view_layout, itemList);
        mListView.setAdapter(adapter);

        // 处理第一次启动的Intent数据
        processIntentData(getIntent());
    }

    // 重写onNewIntent,处理Activity已经存在时接收的新Intent数据
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        processIntentData(intent);
    }

    // 统一处理Intent传过来的Item数据
    private void processIntentData(Intent intent) {
        // 取出传递的参数
        String asd = intent.getStringExtra(MainActivity2.namabarang);
        String asdf = intent.getStringExtra(MainActivity2.jumlahstock);
        String number = intent.getStringExtra(MainActivity2.extraint);

        // 只有参数不为空时才添加新条目
        if (asd != null && asdf != null) {
            Item sparepart = new Item(asd, asdf);
            // add方法自动将新Item追加到列表末尾,对应下一个索引位置
            itemList.add(sparepart);
            // 通知适配器数据更新,刷新列表显示
            adapter.notifyDataSetChanged();
        }

        // 处理布局显示逻辑
        if(number != null) {
            FrameLayout lay = findViewById(R.id.frames);
            if (number.equals("1")) {
                lay.setVisibility(View.INVISIBLE);
                mListView.setVisibility(View.VISIBLE);
            }
        }
    }

    public void onBtnClick (View view){
        Intent intent = new Intent(this,MainActivity2.class);
        startActivity(intent);
    }
}

补充说明

  • 在MainActivity2中跳转回MainActivity3的代码中,需要给Intent添加intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);,确保复用已有的MainActivity3实例,不会每次都创建新实例导致数据丢失。
  • 上述实现是内存存储,应用杀掉后数据会清空,如果需要退出应用后再次打开还保留之前存储的Item数据,可以额外将itemList序列化后存入SharedPreferences或者本地数据库。

内容的提问来源于stack exchange,提问作者user16774956

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 20:39:03