Implement a listview with checkboxes, store&load to sharedpreferences.

JavaScript:
//Frag_Settings.java
public class Frag_Settings extends Fragment implements OnItemClickListener {
// ////////////listview
ListView lstv;
Frag_Settings_Categories_Adapter lstv_adapter;
List<Frag_Settings_Categories> Frag_Settings_Categories_LIST = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.frag_settings, container, false);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//button save - store *checked* listview row IDs to SharedPreferences
Button btn_date = (Button) getActivity().findViewById(R.id.frag_settings_btn_save);
btn_date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Set<String> set = new HashSet<String>();
for (Frag_Settings_Categories item : Frag_Settings_Categories_LIST) {
if (item.getIs_selected())
set.add(String.valueOf(item.getId()));
}
SharedPreferences.Editor prefs = General.get_pref(getActivity()).edit();
prefs.putStringSet("user_categories", set);
prefs.commit();
}
});
//connect design listview to code
lstv = (ListView) getActivity().findViewById(R.id.frag_settings_row02_categories_lstv);
Frag_Settings_Categories_LIST = new ArrayList<Frag_Settings_Categories>();
lstv.setOnItemClickListener(this);
lstv_adapter = new Frag_Settings_Categories_Adapter(getActivity(), Frag_Settings_Categories_LIST);
lstv.setAdapter(lstv_adapter);
//fill listview by dbase records
CategoriesDatasource categories_datasource = new CategoriesDatasource(getActivity());
for (Categories d : categories_datasource.getAllCategoriess()) {
Frag_Settings_Categories_LIST.add(new Frag_Settings_Categories(d.getid(),d.getcategory_name(),false));
}
lstv.setAdapter(lstv_adapter);
//load user prefs - retrieve *checked* listview row IDs
SharedPreferences prefs= General.get_pref(getActivity());
Set<String> user_categories_set = prefs.getStringSet("user_categories", null);
if (user_categories_set!=null)
{
List<String> user_categories = new ArrayList<String>(user_categories_set);
for(int i = 0 ; i < user_categories.size() ; i++){
for (Frag_Settings_Categories item : Frag_Settings_Categories_LIST) {
if (user_categories.get(i).equals(String.valueOf(item.getId())))
item.setIs_selected(true);
}
}
//notify view
lstv_adapter.notifyDataSetChanged();
}
}
JavaScript:
//Frag_Settings_Categories.java
public class Frag_Settings_Categories {
private int id;
private String title;
private Boolean is_selected;
public Frag_Settings_Categories(long id,String title, Boolean is_selected){
this.id=(int) id;
this.title=title;
this.is_selected=is_selected;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Boolean getIs_selected() {
return is_selected;
}
public void setIs_selected(Boolean is_selected) {
this.is_selected = is_selected;
}
}
JavaScript:
//Frag_Settings_Categories_Adapter.java
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
public class Frag_Settings_Categories_Adapter extends BaseAdapter {
private List<Frag_Settings_Categories> data;
private Context context;
public Frag_Settings_Categories_Adapter(Context context, List<Frag_Settings_Categories> items) {
this.context =context;
this.data = items;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.frag_settings_row_detail_02_categories_row, null);
ViewHolder holder;
holder = new ViewHolder();
holder.rowText = (TextView) convertView.findViewById(R.id.frag_settings_row_detail_02_categories_row_title);
holder.rowChk = (CheckBox) convertView.findViewById(R.id.frag_settings_row_detail_02_categories_row_check);
//http://stackoverflow.com/questions/12647001/listview-with-custom-adapter-containing-checkboxes
holder.rowChk.setTag(Integer.valueOf(position)); //store the List<>position in TAG
holder.rowChk.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
data.get((Integer)buttonView.getTag()).setIs_selected(isChecked); //use of List<>position
}
});
holder.rowText.setText(data.get(position).getTitle());
if (data.get(position).getIs_selected())
holder.rowChk.setChecked(true);
else
holder.rowChk.setChecked(false);
return convertView;
}
class ViewHolder {
TextView rowText;
CheckBox rowChk;
}
}
JavaScript:
//frag_settings.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#f4f4f4"
android:orientation="vertical" >
<ListView
android:id="@+id/frag_settings_row02_categories_lstv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dip" />
<!-- save button -->
<Button
android:id="@+id/frag_settings_btn_save"
style="@style/ButtonText"
android:layout_width="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/btn_red"
android:layout_gravity ="center_horizontal"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="save" >
</Button>
</LinearLayout>
JavaScript:
//frag_settings_row_detail_02_categories_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:layout_marginLeft="5dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/frag_settings_row_detail_02_categories_row_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp" />
</LinearLayout>
<CheckBox
android:id="@+id/frag_settings_row_detail_02_categories_row_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:gravity="right" />
</RelativeLayout>