0

I'm trying to create an array based on a radio button survey. I have arrays for each radio button in the strings.xml. Once a radio button is selected, an array should be fetched and put in a hashset to filter out duplicates then put in a new array to display in a fragment once a submit button is clicked. The following is the main section of the activity:

    //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();
        }
    });

The arrays in strings.xml look like this:

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

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

So the answer should be a list of "One", "Two", and "Three". Seems simple enough but my emulator keeps crashing and I have no idea what's wrong. Any help would be greatly appreciated.

The following is a section of a 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...

The code above points to a list adapter activity where position of each item is gathered and a picture is added next to the item in the list.

It seems that I'm mixing up my data types. I apologize for the late edit. I'm relatively new at java coding and am reading up on compatible data types.

2
  • 2
    "my emulator keeps crashing and I have no idea what's wrong...". Please 1) run in the Android Studio debugger, 2) Clear LogCat, 3) Reproduce the problem, then 4) Update your post with the stack trace! Commented Nov 16, 2019 at 1:42
  • 1
    please show the logcat, and what button you check, firstrBtn or secondrBtn Commented Nov 16, 2019 at 1:59

1 Answer 1

2

Modify your onclick to this:

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();
    }
});

Add a helper method to get the appropriate list from the resource file:

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

What you are doing wrong:

  1. filtered_list.add(getResources().getStringArray(R.array.firstarray)); adds String array (not String) as an item to the List.
  2. Unnecessary conversion from HashSet to ArrayList when you can directly convert from String[] (from string resource) to ArrayList.
Sign up to request clarification or add additional context in comments.

3 Comments

I have no idea why you were downvoted - your suggestions are entirely reasonable. HOWEVER - the OP should update his post with his logcat stack grace. Without the stracktrace, we're just guessing... It looks like the OP asked his question, then immediately fled the scene....
Thank you for the reply. With your reply I was able to determine that I was using incompatible data types. I did the following to fix the issue: 1. change the string.xml to string-array data types. 2. As you suggested, created a helper method. 3. Modified if then statements: from "filtered_list.add(getResources().getStringArray(R.array.firstarray));" to "filtered_list.addAll(Arrays.asList(getResources().getStringArray(R.array.firstarray)));"
Lastly, I got rid of the hashset part of the code. Turns out, what I need is to retain results that are only common with each radio group. I'll try to figure this out before posting a different thread. Thank you again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.