I am creating a custom listview
in which I have given a checkbox. I have created a custom adapter class extending the adapter. I am storing the values of those checked box in an array in the adapter class. The name of the arraylist is itemname (you can see it in the bottom of the customadapter class). I want to send this arraylist into an activity(for the purpose of storing those values into database). Since I am extending the array adapter class, I cannot use sharedpreferences
. Can solve this issue? My adapter class is as below.
public class CustomArrayAdapter extends ArrayAdapter<Model> {
protected static final String MODE_PRIVATE = null;
private final List<Model> list;
private final Activity context;
public CustomArrayAdapter(Activity context, List<Model> list) {
super(context, R.layout.customlistlayout, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView mText;
protected CheckBox mCheckbox;
public string getItemAtPosition(int i) {
// TODO Auto-generated method stub
return null;
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
final ArrayList<String> itemname = new ArrayList<String>();
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.customlistlayout, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.mText = (TextView) view.findViewById(R.id.settings_label);
viewHolder.mCheckbox = (CheckBox) view.findViewById(R.id.check);
Activity activity = new SettingsActivity();
viewHolder.mCheckbox.setOnCheckedChangeListener(activity);
viewHolder.mCheckbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Model element = (Model) viewHolder.mCheckbox
.getTag();
element.setSelected(buttonView.isChecked());
**itemname.add(element.getName());**
}
});
for (String myValue : itemname) {
System.out.println(myValue);
}
view.setTag(viewHolder);
viewHolder.mCheckbox.setTag(list.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).mCheckbox.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.mText.setText(list.get(position).getName());
holder.mCheckbox.setChecked(list.get(position).isSelected());
return view;
}
}
I am calling this adapter class as below from an activity
ArrayAdapter<Model> settingsadapter = new CustomArrayAdapter(this, getModel()); setListAdapter(settingsadapter);
First make empty constructor for your activity. like
public class MyActivity extends Activity{
public MyActivity ()
{
}
Then make one function in this activity with ArrayList argument that can dump ArrayList value into database.
public void save_to_database(ArrayList<String> itemname)
{
// your code for save values in database...
}
Call this constructor from your activity.
settingsadapter = new settingsadapter(getBaseContext(), list, MyActivity.this);
Now, make object of your activity in the adapter.
public class CustomArrayAdapter extends ArrayAdapter<Model> {
MyActivity mMyActivity;
Then create constructor of your adapter that have your activity like
public CustomArrayAdapter(Activity context, List<Model> list, MyActivity mActivity)
{
this.mMyActivity = mActivity;
}
Now you have access of all methods of your activity in the adapter. Now pass the ArrayList to the activity's method (which will dump that list into database) when you want like
mMyActivity.save_to_database(Your ArrayList);
Hope this will help you.
Thanks...