我需要访问activity的ArrayList对象,以获得要显示在可扩展列表视图上的值。
public static HashMap<String, List<String>> getData() {
    //here i need access the arraylist object
    HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>();
    List<String>  size = new ArrayList<String>();
    size.add("Regular");
    size.add("Large");
    List<String> Addons = new ArrayList<String>();
    Addons.add("one small cheese");
    Addons.add("chicken");
    expandableListDetail.put("Choice of Size", size);
    expandableListDetail.put("Choice of Add ons", Addons);
    return expandableListDetail;
}发布于 2019-11-21 07:28:55
在活动中创建静态ArrayList(例如: itemList)
static ArrayList<Item> itemList = new ArrayList<>();访问创建的数组列表,如下所示
ArrayList<Item> list = YOURACTIVITYNAME.itemList;发布于 2019-11-21 06:17:44
您可以轻松地创建两个单独的函数-一个用于创建size,另一个用于创建Addons。然后在您的Activity中调用函数来设置expandableListDetail。
expandableListDetail.put("Choice of Size", getSizes());
expandableListDetail.put("Choice of Add ons", getAddons());因此,基本上,您只需将getData方法分成两部分,并向Activity提供必要的列表。
发布于 2019-11-21 07:24:38
您有一个静态函数,并且需要在静态方法中访问活动类的普通ArrayList对象,因此,静态关键字的规则是,如果您有静态函数,那么静态函数中的所有成员都必须是静态的。
对于您的情况,您需要对活动类的ArrayList对象进行静态设置,然后再尝试。
https://stackoverflow.com/questions/58968315
复制相似问题