【问题标题】:Adding an Arrays from strings.xml to another ArrayList将数组从 strings.xml 添加到另一个 ArrayList
【发布时间】:2020-03-12 04:22:15
【问题描述】:

我正在尝试根据单选按钮调查创建一个数组。我在strings.xml 中有每个单选按钮的数组。选择单选按钮后,应获取一个数组并将其放入哈希集中以过滤掉重复项,然后放入一个新数组以在单击提交按钮后显示在片段中。以下是活动的主要部分:

    //SUBMIT BUTTON
    Button submit = (Button) findViewById(R.id.submitBtn);
    final Fragment fragment = new ListFragment();
    final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    //On Submit Click show Instrument Fragment
    submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            final ArrayList filtered_list = new ArrayList<>();
            // Radio Button Selection
            if (firstrBtn.isChecked()){
                filtered_list.add(getResources().getStringArray(R.array.firstarray));

            } else if (secondrBtn.isChecked()){
                filtered_list.add(getResources().getStringArray(R.array.secondarray));
            } 

            //Convert filtered_list to final_filtered_list and eliminate duplicates
            final HashSet<String> hashSet = new HashSet<String>();
            hashSet.addAll(filtered_list);
            filtered_list.clear();
            filtered_list.addAll(hashSet);
            ArrayList<String> final_filtered_list = new ArrayList<String>(hashSet);

            //Send Final ArrayList
            Bundle bundle = new Bundle();
            bundle.putStringArrayList("RESULT_LIST", final_filtered_list);
            listFragment.setArguments(bundle);

            //Transaction to Fragment
            transaction.replace(R.id.collect_container, listFragment);
            transaction.addToBackStack(null);
            transaction.commit();
        }
    });

strings.xml 中的数组如下所示:

<array name="firstarray">
    <item> One </item>
    <item> Two </item>
</array>

<array name="secondarray">
    <item> Two </item>
    <item> Three </item>
</array>

所以答案应该是“一”、“二”和“三”的列表。 看起来很简单,但我的模拟器一直在崩溃,我不知道出了什么问题。任何帮助将不胜感激。

以下是一段Logcat:

  019-11-17 16:59:07.354 32291- 
  32291/ E/AndroidRuntime: 
  FATAL EXCEPTION: main
  Process: PID: 32291
  java.lang.ClassCastException: java.lang.String[] cannot be cast to 
  java.lang.String
    at...

上面的代码指向一个列表适配器活动,其中收集了每个项目的位置,并在列表中的项目旁边添加了一张图片。

似乎我混淆了我的数据类型。我为迟到的编辑道歉。我在 java 编码方面相对较新,并且正在阅读兼容的数据类型。

【问题讨论】:

  • "my emulator keeps crashing and I have no idea what's wrong..."。请 1) 在 Android Studio 调试器中运行,2) 清除 LogCat,3) 重现问题,然后 4) 使用堆栈跟踪更新您的帖子!
  • 请显示 logcat,以及您检查的按钮,firstrBtnsecondrBtn

标签: java android arrays list


【解决方案1】:

将您的 onclick 修改为:

submit.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Bundle bundle = new Bundle();
        bundle.putStringArrayList("RESULT_LIST", Arrays.asList(getResultList()));
        listFragment.setArguments(bundle);

        //Transaction to Fragment
        transaction.replace(R.id.collect_container, listFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }
});

添加帮助方法以从资源文件中获取适当的列表:

private String[] getResultList() {
    if (firstrBtn.isChecked()) {
        getResources().getStringArray(R.array.firstarray));
    }
    return getResources().getStringArray(R.array.secondarray));
}

你做错了什么:

  1. filtered_list.add(getResources().getStringArray(R.array.firstarray)); 将字符串数组(不是字符串)作为项目添加到列表中。
  2. 当您可以直接从 String[](从字符串资源)转换为 ArrayList 时,从 HashSet 到 ArrayList 的不必要转换。

【讨论】:

  • 我不知道你为什么被否决 - 你的建议是完全合理的。但是 - OP 应该用他的 logcat 堆栈优雅更新他的帖子。没有stracktrace,我们只是猜测......看起来OP问了他的问题,然后立即逃离了现场......
  • 感谢您的回复。通过您的回复,我能够确定我使用了不兼容的数据类型。我做了以下来解决这个问题: 1. 将 string.xml 更改为字符串数组数据类型。 2. 正如您所建议的,创建了一个辅助方法。 3. 修改 if then 语句:from "filtered_list.add(getResources().getStringArray(R.array.firstarray));"到“filtered_list.addAll(Arrays.asList(getResources().getStringArray(R.array.firstarray)));”
  • 最后,我去掉了代码中的 hashset 部分。事实证明,我需要的是保留仅在每个广播组中通用的结果。在发布不同的线程之前,我会尝试弄清楚这一点。再次感谢您。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-11
  • 2018-08-19
  • 1970-01-01
  • 2021-12-20
  • 2017-05-07
  • 1970-01-01
  • 2013-05-19
相关资源
最近更新 更多