how to pass an arraylist from a class extending arryadapter to an activity

Go To


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;

        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(;
                viewHolder.mCheckbox = (CheckBox) view.findViewById(;

                Activity activity = new SettingsActivity();
                        .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                            public void onCheckedChanged(CompoundButton buttonView,
                                    boolean isChecked) {
                                Model element = (Model) viewHolder.mCheckbox


                for (String myValue : itemname) {
            } else {
                view = convertView;
                ((ViewHolder) view.getTag()).mCheckbox.setTag(list.get(position));
            ViewHolder holder = (ViewHolder) view.getTag();
            return view;

I am calling this adapter class as below from an activity

ArrayAdapter<Model> settingsadapter = new CustomArrayAdapter(this, getModel()); setListAdapter(settingsadapter);
2012-04-04 02:25
by Sanal Varghese


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.


2012-04-04 05:44
by Never Quit