如何将一个arraylist从一个扩展arryadapter的类传递给一个activity

时间:2012-04-04 02:25:55

标签: android

我正在创建一个自定义listview,其中我给了一个复选框。我已经创建了一个扩展适配器的自定义适配器类。我将这些复选框的值存储在适配器类的数组中。 arraylist的名称是itemname(您可以在customadapter类的底部看到它)。我想将这个arraylist发送到一个活动(为了将这些值存储到数据库中)。由于我正在扩展数组适配器类,因此我无法使用sharedpreferences。可以解决这个问题吗?我的适配器类如下。

    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;
        }
    }

我从活动

中调用此适配器类如下所示
ArrayAdapter<Model> settingsadapter = new CustomArrayAdapter(this, getModel()); setListAdapter(settingsadapter);

1 个答案:

答案 0 :(得分:0)

首先为您的活动制作空构造函数。像

public class MyActivity extends Activity{
    public MyActivity ()
        {

        }

然后使用ArrayList参数在此活动中创建一个函数,该参数可以将ArrayList值转储到数据库中。

public void save_to_database(ArrayList<String> itemname)
{
 // your code for save values in database...
}

从您的活动中调用此构造函数。

settingsadapter = new settingsadapter(getBaseContext(), list, MyActivity.this);

现在,在适配器中创建活动的对象。

public class CustomArrayAdapter extends ArrayAdapter<Model> {

MyActivity mMyActivity;

然后创建适配器的构造函数,其中包含您的活动,如

public CustomArrayAdapter(Activity context, List<Model> list, MyActivity mActivity)
    {
      this.mMyActivity = mActivity;
    }

现在,您可以在适配器中访问活动的所有方法。现在,当您想要

时,将ArrayList传递给activity的方法(将该列表转储到数据库中)
mMyActivity.save_to_database(Your ArrayList);

希望这会对你有所帮助。

...谢谢