【问题标题】:Android: Populating a Table with an Arraylist from another ActivityAndroid:使用来自另一个 Activity 的 Arraylist 填充表
【发布时间】:2015-12-10 23:00:11
【问题描述】:

我需要在这里指出正确的方向。

我有一个 Arraylist(String) 名称和一个 Arraylist(String) 评级。我想在第 1 列中显示名称,在第 2 列中显示评分值,以便用户可以在下一个活动的表格布局中看到名称以及他们在其旁边给出的评分。

arraylists存在于主activity中,需要发送到下一个activity,我们称它为'Activity two'。不知道如何使用 Arraylists 来实现这一点。

所以,基本上。我需要知道..

如何创建一个表格布局列,该列将动态显示用户输入的数据,并从另一个活动中的 Arraylist 接收该数据。

任何建议都非常感谢我的巫师伙伴。

【问题讨论】:

  • 我已经填充了我的 ArrayLists,我不确定是否应该使用意图来发送数据,或者是否应该做其他事情。我认为有一种常用的方法可以实现这一点。

标签: java android arraylist tablelayout


【解决方案1】:

如果您已经拥有 ArrayList 的信息并且只是调用一个新的 Intent 来打开,您应该能够在 Bundle 中将此信息传递给新类。由于 ArrayList 实现了 Serializable,您可以将整个数组传递给 bundle 中的新意图,然后将其加载到您创建的新意图中。

    // Class that you have the ArrayList in MainActivity
    ArrayList<String> names = new ArrayList<>();
    names.add("NAME 1");
    names.add("NAME 2");

    ArrayList<String> ratings = new ArrayList<>();
    ratings.add("10");
    ratings.add("8");

    // Create the Bundle and add the ArrayLists as serializable
    Bundle bundle = new Bundle();
    bundle.putSerializable("NAMES", names);
    bundle.putSerializable("RATINGS", ratings);

    // Start new intent with ArrayList Bundle passed in
    Intent intent = new Intent(this, ActivityTwo.class);
    intent.putExtra("KEY", bundle);
    startActivity(intent);

现在您已经传入了 ArrayLists,您需要在您调用的新 Intent 中提取该信息。这应该在ActivityTwo的onCreate中完成

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_phone_list_custom);

    // Get Extras that were passed in
    Bundle extras = getIntent().getExtras();

    // Not null then we can do stuff with it
    if (extras != null) {
        // Since we passed in a bundle need to get the bundle that we passed in with the key  "KEY"
        Bundle arrayListBundle = extras.getBundle("KEY");
        // and get whatever type user account id is

        // From our Bundle that was passed in can get the two arrays lists that we passed in - Make sure to case to ArrayList or won't work
        ArrayList<String> names = (ArrayList) arrayListBundle.getSerializable("NAMES");
        ArrayList<String> ratings = (ArrayList) arrayListBundle.getSerializable("RATINGS");

        // TODO Make this do more than just log
        Log.i(TAG, "Name=" + names.get(0) + " Rating=" + ratings.get(0));
        Log.i(TAG, "Name=" + names.get(1) + " Rating=" + ratings.get(1));

    }

【讨论】:

  • 我爱你,非常感谢你抽出时间来解决这个问题@Trevor Will
【解决方案2】:

一个建议是将数据简单地写入 SharedData。然后你可以随时从任何活动中回调它。

我这样做是为了在一个视图中设置首选项,然后由小部件使用。相同的应用程序。不同的看法。相同的数据。

澄清: 我想我以前用错词来说 SharedData。我真正应该说的是“SharedPreferences”。

它的工作方式是,在您的活动中的某个时刻,您将数据写出。您只需告诉它要将哪些值写入哪些键,就可以了。在后台,系统会存储一个您的应用程序独有的 XML 文件。一旦该文件存在,您应用中的任何其他活动都可以调用它来检索值。

可以在这里找到完整的解释: http://developer.android.com/guide/topics/data/data-storage.html

我将它用于实际主要活动是小部件的应用程序。该小部件的首选项是从 SharedPreferences 中调用的。这些首选项最初是在正常的全屏活动中编写的。一旦你设置了它们,你就关闭了 Activity,下次小部件更新时,它会获取当前值。

【讨论】:

  • 你所说的“SharedData”是什么意思,你说的是使用 Intent 还是其他东西,现在我正在尝试获取活动 1 中的数据,发送到活动 3,然后跳过 2 .
  • 您已经在这里接受了答案。但我会用更多信息更新我的。
  • 非常感谢您的澄清!
猜你喜欢
  • 2015-10-08
  • 1970-01-01
  • 2014-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多