In my fragment, I have 4 ListViews (all 4 containing custom rows with a radio button) where a user needs to select a row for each ListView. Initially I was having trouble with getting the radio button to be checked whenever the user selects a particular row, however I came across this solution which worked well for me https://stackoverflow.com/a/12823457/3739918
Now everything is good, except I'm unable to retain the checked radio buttons after a configuration change (eg. phone changes from portrait to landscape or vice versa). I have tried to using setRetainInstance(true) which works to retain the selections prior to the configuration change (I just use an integer array to keep track of the user selections in the different list views), however I have been unsuccessful in using this information to recheck the corresponding radio button after the configuration change.
Here is the code of my fragment containing the ListViews (minus irrelevant lines):
public class FourBandFrag extends Fragment implements
AdapterView.OnItemClickListener {
private ArrayList<ListView> lists = new ArrayList<ListView>();
private ArrayList<CustomAdapter> adapters = new ArrayList<CustomAdapter>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
View result = inflater.inflate(R.layout.fourbandlayoutfrag, container,
false);
return result;
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
lists.add((ListView) getActivity().findViewById(R.id.FourListView1));
lists.add((ListView) getActivity().findViewById(R.id.FourListView2));
lists.add((ListView) getActivity().findViewById(R.id.FourListView3));
lists.add((ListView) getActivity().findViewById(R.id.FourListView4));
adapters.add(new CustomAdapter(getActivity(), generateData(1)));
adapters.add(new CustomAdapter(getActivity(), generateData(1)));
adapters.add(new CustomAdapter(getActivity(), generateData(2)));
adapters.add(new CustomAdapter(getActivity(), generateData(3)));
for (int i = 0; i < lists.size(); i++) {
lists.get(i).setAdapter(adapters.get(i));
}
for (int q = 0; q < lists.size(); q++) {
lists.get(q).setOnItemClickListener(this);
}
for (int i = 0; i < lists.size(); i++) {
lists.get(i).setAdapter(adapters.get(i));
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//This checks the corresponding radio button for the corresponding listview
((CustomAdapter) parent.getAdapter()).setSelectedIndex(position);
((CustomAdapter) parent.getAdapter()).notifyDataSetChanged();
}
}
The CustomAdapter.java looks like (minus irrelevant lines):
public class CustomAdapter extends ArrayAdapter<Model> {
private final Context context;
private final ArrayList<Model> modelsArrayList;
private int selectedIndex = -1;
public CustomAdapter(Context context, ArrayList<Model> modelsArrayList) {
super(context, R.layout.listitem, modelsArrayList);
this.context = context;
this.modelsArrayList = modelsArrayList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.listitem, parent, false);
RadioButton radiobutton = (RadioButton) rowView
.findViewById(R.id.radioButton);
// Check the correct radio button when person selects a listview item
if (selectedIndex == position) {
radiobutton.setChecked(true);
} else {
radiobutton.setChecked(false);
}
return rowView;
}
public void setSelectedIndex(int index) {
selectedIndex = index;
}
}
Anyone have ideas?
Thank you
Update 1
Because a radio button is only checked when the user clicks on a row, I decided to simulate a click using the information retained regarding the user selections prior to the configuration change. So now my fragment looks like this:
public class FourBandFrag extends Fragment implements
AdapterView.OnItemClickListener {
private ArrayList<ListView> lists = new ArrayList<ListView>();
private ArrayList<CustomAdapter> adapters = new ArrayList<CustomAdapter>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
View result = inflater.inflate(R.layout.fourbandlayoutfrag, container,
false);
return result;
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
lists.add((ListView) getActivity().findViewById(R.id.FourListView1));
lists.add((ListView) getActivity().findViewById(R.id.FourListView2));
lists.add((ListView) getActivity().findViewById(R.id.FourListView3));
lists.add((ListView) getActivity().findViewById(R.id.FourListView4));
adapters.add(new CustomAdapter(getActivity(), generateData(1)));
adapters.add(new CustomAdapter(getActivity(), generateData(1)));
adapters.add(new CustomAdapter(getActivity(), generateData(2)));
adapters.add(new CustomAdapter(getActivity(), generateData(3)));
for (int i = 0; i < lists.size(); i++) {
lists.get(i).setAdapter(adapters.get(i));
}
for (int q = 0; q < lists.size(); q++) {
lists.get(q).setOnItemClickListener(this);
}
for (int i = 0; i < lists.size(); i++) {
lists.get(i).setAdapter(adapters.get(i));
}
// Retain radio button selections after configuration change
for (int w = 0; w < bandRowNumber.length; w++) {
if (bandRowNumber[w] != unselected) {
lists.get(w).performItemClick(
lists.get(w).getAdapter()
.getView(bandRowNumber[w], null, null),
bandRowNumber[w],
lists.get(w).getAdapter().getItemId(bandRowNumber[w]));
Log.d(TAG, "Rechecking radio button " + bandRowNumber[w] + " from ListView " + w);
}
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//This checks the corresponding radio button for the corresponding listview
((CustomAdapter) parent.getAdapter()).setSelectedIndex(position);
((CustomAdapter) parent.getAdapter()).notifyDataSetChanged();
}
}
This certainly does attempt to recheck the correct radiobuttons judging from the logcat I'm getting, however the radiobuttons remain unchecked after the config change. Here is what it looks like: In portrait mode I make a selection in each of the listviews.
http://s17.postimg.org/nm4wyve4v/portrait.png
I change the phone to landscape mode and the radio button selections dissapear.
http://s10.postimg.org/459rrgc9l/landscape.png
The logcat after changing from portrait to landscape is:
07-02 12:56:14.407: D/FourBandFrag(30090): Rechecking radio button 0 from ListView 0
07-02 12:56:14.417: D/FourBandFrag(30090): Rechecking radio button 1 from ListView 1
07-02 12:56:14.427: D/FourBandFrag(30090): Rechecking radio button 2 from ListView 2
07-02 12:56:14.427: D/FourBandFrag(30090): Rechecking radio button 3 from ListView 3
Any suggestions for a fix?