gpt4 book ai didi

java - 从返回的 HashMap 中获取列表

转载 作者:行者123 更新时间:2023-11-30 10:58:27 26 4
gpt4 key购买 nike

我有一个类有一个名为 prepareListData 的方法.此方法准备数据以填充 ExpandableListView在安卓系统中。当该方法被放置在调用它的类中时,它可以完美地工作并且不需要返回任何东西,因为我使用私有(private)字段。当我将该方法移动到它自己的类并使其成为静态时,我得到一个空指针,因为它显然需要返回它准备的数据。

我创建的类具有相同的方法 ( prepareListData ),它返回一个 HashMap<String, List<String>> .我正在尝试从我希望分配 List<> 的其他类调用该方法在返回的HashMapList<String>多变的。有没有办法在 Java 中实现这一点?

我以前从未尝试过,所以我什至不知道从哪里开始。

这是我使用 prepareListData 方法的类:

public class PrepareListData {

public static HashMap prepareListData() {
List<String> gpsMenuListDataHeader = new ArrayList<>();
HashMap<String, List<String>> gpsMenuListDataChild = new HashMap<>();

// Main group data
gpsMenuListDataHeader.add("Job Card");
gpsMenuListDataHeader.add("Sign Off");
gpsMenuListDataHeader.add("Test Installation");

// Child Items for Job Card
List<String> jobcard = new ArrayList<>();
jobcard.add("Job Card Sub Item 1");
jobcard.add("Job Card Sub Item 2");
jobcard.add("Job Card Sub Item 3");
jobcard.add("Job Card Sub Item 4");
jobcard.add("Job Card Sub Item 5");

// Child Items fos Sign Off
List<String> signoff = new ArrayList<>();
signoff.add("Sign Off Sub Item 1");
signoff.add("Sign Off Sub Item 2");
signoff.add("Sign Off Sub Item 3");

// Child Items for Test Installation
List<String> testInstallation = new ArrayList<>();
testInstallation.add("Test Installation Sub Item 1");
testInstallation.add("Test Installation Sub Item 2");
testInstallation.add("Test Installation Sub Item 3");
testInstallation.add("Test Installation Sub Item 4");
testInstallation.add("Test Installation Sub Item 5");
testInstallation.add("Test Installation Sub Item 6");
testInstallation.add("Test Installation Sub Item 7");

gpsMenuListDataChild.put(gpsMenuListDataHeader.get(0), jobcard);
gpsMenuListDataChild.put(gpsMenuListDataHeader.get(1), signoff);
gpsMenuListDataChild.put(gpsMenuListDataHeader.get(2), testInstallation);

return gpsMenuListDataChild;
}
}

这是我尝试从 HashMap 中调用并提取 List<> 的方法的 fragment :

gpsMenuListView = (ExpandableListView) findViewById(R.id.gps_menu_drawer);
prepareListData();
gpsMenuListDataHeader = PrepareListData.prepareListData().;
gpsMenuListAdapter = new ExpandableListAdapter(this, gpsMenuListDataHeader, gpsMenuListDataChild);
gpsMenuListView.setAdapter(gpsMenuListAdapter);

堆栈跟踪:

08-26 23:12:25.031  32157-32164/za.co.gpsts.gpsjobcard E/art﹕ Failed writing handshake bytes (-1 of 14): Broken pipe
08-26 23:12:25.039 32157-32157/za.co.gpsts.gpsjobcard E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: za.co.gpsts.gpsjobcard, PID: 32157
java.lang.RuntimeException: Unable to start activity ComponentInfo{za.co.gpsts.gpsjobcard/za.co.gpsts.gpsjobcard.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at za.co.gpsts.gpsjobcard.ExpandableListAdapter.getGroupCount(ExpandableListAdapter.java:45)
at android.widget.ExpandableListConnector.getCount(ExpandableListConnector.java:397)
at android.widget.ListView.setAdapter(ListView.java:487)
at android.widget.ExpandableListView.setAdapter(ExpandableListView.java:603)
at za.co.gpsts.gpsjobcard.MainActivity.onCreate(MainActivity.java:50)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

最佳答案

I am trying to call the method from the other class from where I wish to assign the List<> in the returned HashMap to a List variable. Is there a way to achieve this in Java?

HashMap<String, List<String> map = PrepareListData.prepareListData();
List<String> yourlist = map.get("key");

其中 key 是映射到您需要的列表的键。在您的示例中,这些将是“工作卡”、“签字”、“测试安装”之一。

仅供引用,我建议将这些字符串常量分配给一个变量。如果您需要更换一个怎么办?您将必须找到它用于更改它的每个位置。

关于java - 从返回的 HashMap 中获取列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32236700/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com