Reputation: 4747
I have an Android activity which is a map and an ExpandableListView. This ExpandableListView is populated using a collection built in run time and an adapter. This list view contains a label, an image and a checkbox. They should work as filters: if the checkbox is checked, the items related to that label should appear in my map. If the checkbox is not checked. These items should disappear.
This is the code for my activity and adapter (most important parts)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/list_item_child"
android:gravity="center_vertical"
android:background="@android:color/white">
<CheckBox
android:id="@+id/filtro_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"/>
<ImageView
android:id="@+id/filtro_imgView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/list_item_text_child"
android:textSize="20sp"
android:padding="10dp"
android:layout_marginLeft="5dp"/>
</LinearLayout>
Code:
@Override
//in this method you must set the text to see the children on the list
public View getChildView(int i, int i1, boolean b, View view, final ViewGroup viewGroup) {
if (view == null) {
view = inflater.inflate(R.layout.list_item_child, viewGroup,false);
}
TextView textView = (TextView) view.findViewById(R.id.list_item_text_child);
//"i" is the position of the parent/group in the list and
//"i1" is the position of the child
Filtro child = mParent.get(i).getArrayChildren().get(i1);
textView.setText(child.getTitle());
ImageView imageView = (ImageView) view.findViewById(R.id.filtro_imgView);
//"i" is the position of the parent/group in the list
Drawable drawable = view.getResources().getDrawable(child.getImage());
imageView.setImageDrawable(drawable);
CheckBox chkbox = (CheckBox) view.findViewById(R.id.filtro_checkbox);
chkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
View view2 = inflater.inflate(R.layout.list_item_child, viewGroup,false);
// TODO Auto-generated method stub
if (buttonView.isChecked()) {
Toast.makeText(view2.getContext(), "Checked",
Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(view2.getContext(), "UnChecked",
Toast.LENGTH_SHORT).show();
}
}
});
//return the entire view
return view;
}
I am new to Android development, but as far as I understood, this method code will "instantiate" the activity layout for each item. This is working and the items are being populated in the emulator. Then, I added the onCheckedChanged listener. It works, the toast is shown. But now I would like to make the pins show/hide depending on the checkbox selection.
To do that, I would like to
I do not know how to perform these steps, I thought about using a singleton or an observer design pattern to reach the MainActivity in order to be able to call the methods to reload the pins. Would that be an elegant approach? How could I check the state (checked/not checked) for all of my checkboxes?
Thanks
Upvotes: 0
Views: 736
Reputation: 4725
You can use preferences to store the value of checked or unchecked items. Also you can register a listener when the preferences are changed. Otherwise, you can read the preferences in the activity where you want to display the maps.
https://developer.android.com/reference/android/preference/Preference.html
Upvotes: 1