Reputation: 39
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.
Upvotes: 0
Views: 167
Reputation: 1217
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:
filtered_list.add(getResources().getStringArray(R.array.firstarray));
adds String array (not String) as an item to the List.Upvotes: 2