简体   繁体   English

Android:使用另一个活动中的数组列表填充表

[英]Android: Populating a Table with an Arraylist from another Activity

I need to be pointed in the right direction here. 我需要在这里指出正确的方向。

I've got an Arraylist(String) of names, and an Arraylist(String) of ratings. 我有一个名称的Arraylist(String)和一个评级的Arraylist(String)。 I want to display the names in column 1, and the rating values in column 2 in such a way that the user can see the name, and the rating they gave beside it in a tablelayout in the next activity. 我想在第1列中显示名称,并在第2列中显示评分值,以便用户可以在下一个活动的表格布局中看到该名称及其旁边给出的评分。

The arraylists exist in the main activity, and need to be sent to the next activity, let's call it 'Activity Two'. 数组列表存在于主活动中,需要发送到下一个活动,我们将其称为“第二活动”。 Not sure how to achieve this with Arraylists. 不确定如何使用Arraylists实现此目的。

So, basically. 所以,基本上。 I need to know.. 我需要知道..

How to create a table layout column that will display the user entered data dynamically, and receives that data from an Arraylist in another activity. 如何创建一个表布局列,该列将动态显示用户输入的数据,并在另一个活动中从Arraylist接收该数据。

Any advice is greatly appreciated my fellow Wizards. 我的巫师同伴们,任何建议都将不胜感激。

One suggestion is to simple write the data out to a SharedData. 一种建议是简单地将数据写出到SharedData中。 Then you could call it back from any activity at any time. 然后,您可以随时通过任何活动将其调用。

I do this to have preferences set in one view that are then used later by a widget. 我这样做是为了在一个视图中设置首选项,然后供小部件使用。 Same app. 相同的应用。 Different views. 不同的看法。 Same data. 相同的数据。

To clarify: I think I used to wrong term in saying SharedData. 需要澄清的是:我认为我在说SharedData时常使用错误的术语。 What I really should have said was "SharedPreferences". 我真正应该说的是“ SharedPreferences”。

The way it works is, at some point in your activity, you write the data out. 它的工作方式是在活动的某个时刻将数据写出。 You just tell it what values to write to what keys, and there it is. 您只需要告诉它要写入哪些键的值就可以了。 In the background, the system stores an XML file unique to your app. 系统在后台存储您的应用唯一的XML文件。 Once that file is there, any other activity in your app can call it up to retrieve the values. 有了该文件后,您应用中的任何其他活动都可以调用它来检索值。

The full explanation for this can be found here: http://developer.android.com/guide/topics/data/data-storage.html 完整的解释可以在这里找到: http : //developer.android.com/guide/topics/data/data-storage.html

I use this for an app where the actual main activity is a Widget. 我将此用于实际主要活动是小部件的应用程序。 The preferences for that widget are called up from SharedPreferences. 该小部件的首选项从SharedPreferences中调用。 Those preferences are initially written in a normal full screen activity. 这些首选项最初是在正常的全屏活动中编写的。 Once you set them, you close the activity, and the next time the widget updates, it grabs the current values. 设置它们后,您将关闭活动,并且下次小部件更新时,它将获取当前值。

If you already have the information for your ArrayList and are simply calling a new Intent to open you should be able to pass this information in a Bundle to the new class. 如果您已经有了ArrayList的信息,并且只是在调用一个新的Intent以打开,则应该可以将Bundle中的信息传递给新类。 Since ArrayList implements Serializable you can pass the entire array to the new intent in a bundle and then load that in the new intent that you created. 由于ArrayList实现了Serializable,因此您可以将整个数组传递到包中的新意图中,然后将其加载到您创建的新意图中。

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

Now that you have passed in the ArrayLists you need to extract that information in the new Intent that you called. 现在您已经传递了ArrayList,现在需要在您调用的新Intent中提取该信息。 This should be done in the onCreate of ActivityTwo 这应该在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));

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM