代码之家  ›  专栏  ›  技术社区  ›  Igal

如何将列表从一个活动传递到另一个活动?

  •  0
  • Igal  · 技术社区  · 2 年前

    我是安卓工作室的新手,正在尝试开发我的第一个应用程序。

    我有3个活动MainActivity、DashboardActivity和SplashActivity。

    仪表板活动-

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dashboard);
    
        Hooks();
        allQuestionsList=listS;
        Collections.shuffle(allQuestionsList);
        modelClass = listS.get(index);
    
        setAllData();
    

    }

    启动活动-

    public class SplashActivity extends AppCompatActivity {
    public static ArrayList<ModelClass> listS;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_activity_main);
    
        listS = new ArrayList<>();
        listS.add(new ModelClass("here is the question", "a", "b", "c", "d", "answer"));
        listS.add(new ModelClass("Under-inflated tires increase fuel consumption. A single tire under-inflated by 56 kpa (8 psi) can increase fuel consumption by?", "25 per cent", "15 per cent", "Seven per cent", "Four per cent", "Four per cent"));
        listS.add(new ModelClass("You are not allowed to park a vehicle within _____________ of a pedestrian corridor", "9 metres", "3 metres", "30 metres", "15 metres", "15 metres"));
        listS.add(new ModelClass("What class licence permits the holder to operate a motorcycle?", "Class 4", "Class 1", "Class 6", "Class 5", "Class 6"));
        listS.add(new ModelClass("Seatbelts must be worn by all drivers and passengers:", "Only when driving in the city", "Only when carrying passengers", "All the time unless exempted by law", "Only when driving on the highway", "All the time unless exempted by law"));
    
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(SplashActivity.this, DashboardActivity.class);
                startActivity(intent);
            }
        }, 1500);
    }
    
    

    这是我的AndroidManifest-

    <application
    android:allowBackup="true"
    android:dataExtractionRules="@xml/data_extraction_rules"
    android:fullBackupContent="@xml/backup_rules"
    android:icon="@drawable/mainscreenlogo"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/Theme.QuizTheoryApplication"
    tools:targetApi="31">
    
    <activity
        android:name=".example.QuizTheoryApplication.MainActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    
    <activity
        android:name=".example.QuizTheoryApplication.DashboardActivity"
        android:label="Preferences"
        android:exported="true">
        <intent-filter>
            <action android:name="com.iphonik.chameleon.AppPreferenceActivity" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    
    <activity
        android:name=".example.QuizTheoryApplication.SplashActivity"
        android:label="Preferences"
        android:exported="true">
        <intent-filter>
            <action android:name="com.iphonik.chameleon.AppPreferenceActivity" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    

    问题是我的Splash活动没有运行,当然,列表总是空的。我尝试添加Dashboard活动,但没有成功-

    new Handler().postDelayed(new Runnable() 
    { @Override public void run() 
    { Intent intent = new Intent(DashboardActivity.this, SplashActivity.class); startActivity(intent); } }, 1500);
    
    

    我也试着使用 intent.putStringArrayListExtra("list", listS);

    ArrayList<String> receivedList = getIntent().getStringArrayListExtra("list")
    但没有任何成功。我很乐意在这件事上提供帮助。谢谢

    0 回复  |  直到 2 年前
        1
  •  0
  •   basileus    2 年前

    我不知道你的ModelClass到底是什么。但如果你能让它成为Parcelable或Serializable,那么你就可以简单地使用Bundle将数据(listS)发送到新的Activity。你应该这样做:

    在里面 飞溅活跃度 :

       new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent intent = new Intent(SplashActivity.this, DashboardActivity.class);
            Bundle extras = new Bundle();
            extras.putParcelableArrayList(YOUR_KEY, listS); // or putSerializable instead if ModelClass can implement Serializable
            intent.putExtras(extras);
            startActivity(intent);
        }
       }
    

    然后在 仪表板活动 :

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dashboard);
    
    if (savedInstanceState != null) {
        listS = savedInstanceState.getParcelableArrayList(YOUR_KEY); // or getSerializable instead you used putSerializable
    }
    
    Hooks();
    allQuestionsList=listS;
    Collections.shuffle(allQuestionsList);
    modelClass = listS.get(index);
    
    setAllData();