有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android:使用另一个活动中的Arraylist填充表

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

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

ArrayList存在于主活动中,需要发送到下一个活动,我们称之为“活动二”。不知道如何使用ArrayList实现这一点

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

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

我的巫师们非常感谢你们的任何建议


共 (2) 个答案

  1. # 1 楼答案

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

    我这样做是为了在一个视图中设置首选项,然后供小部件稍后使用。同样的应用。不同的观点。同样的数据

    为了澄清: 我想我以前说SharedData的时候用错了词。我真正应该说的是“共享参考”

    它的工作方式是,在你活动的某个时刻,你把数据写出来。你只要告诉它要写什么值到什么键,它就在那里。在后台,系统存储应用程序特有的XML文件。一旦该文件存在,应用程序中的任何其他活动都可以调用它来检索值

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

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

  2. # 2 楼答案

    如果您已经有了ArrayList的信息,并且只是调用了一个新的意图来打开,那么您应该能够将这些信息打包传递给新类。由于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);
    

    既然已经传入了ArrayList,就需要在调用的新意图中提取这些信息。这应该在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));
    
        }