如何访问另一个 class 的列表视图

how to access another class's listview

所以我有 class 1。class 1 的 XML 文件有列表视图。我也有 class 2。我想让 class 2 使用 class 1 的列表视图。

your_array_list.add("foo");
    your_array_list.add("bar");

    // This is the array adapter, it takes the context of the activity as a
    // first parameter, the type of list view as a second parameter and your
    // array as a third parameter.
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
            this,
            android.R.layout.simple_list_item_1,
            your_array_list );

    lv.setAdapter(arrayAdapter);

这是将项目添加到列表视图的代码,但我无法让 class 2 在 class 1 的列表视图上使用此代码。

我遇到过类似的情况,我想从多个活动中访问一个整数。我所做的是:

  1. 新建javaclass

  1. 在javaclass中创建一个数组列表。 确保它是静态的,以便您可以从其他人使用它 classes/activities

    3.Now,要在另一个 class 中访问它,请执行以下操作:

    ClassName.myArrayList.add("Doing from another class!");

另外一个class,一个arrayList就可以了,不一定要多。它只能有一个用途,就是存放你的全局变量:

 public class ClassName{
ArrayList <String> myArrayList = new ArrayList <String>();
}

有趣的事实:

您已经在访问来自其他 classes 的大量变量。例如,我们经常使用的 R.id.* 东西...我们从 R class

访问它