共享首选项使用上下文菜单保存Listview项目

时间:2015-03-12 20:41:58

标签: android listview android-intent sharedpreferences contextmenu

我有一个上下文菜单栏,有两个图标...当我点击保存图标时,我想存储我在SharedPreferences列表视图中检查过的所有项目,当我点击加载图标时,我希望所有这些选中的项目都突出显示在列表中。

图标在OnActionItemClicked中声明为菜单项。

非常感谢任何想法。

我的班级:

public class UserContacts extends ActionBarActivity {

    SimpleCursorAdapter mAdapter;
    MatrixCursor mMatrixCursor;
    ListView listview;
    int count = 0;
    SharedPreferences prf;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_contacts);

        // The contacts from the contacts content provider is stored in this cursor
        mMatrixCursor = new MatrixCursor(new String[] { "_id","name","photo","details"} );

        // Adapter to set data in the listview
        mAdapter = new SimpleCursorAdapter(getBaseContext(),
                R.layout.lv_layout,
                null,
                new String[] { "name","photo","details"},
                new int[] { R.id.tv_name,R.id.iv_photo,R.id.tv_details}, 0);

        // Getting reference to listview
        final ListView lstContacts = (ListView) findViewById(R.id.lst_contacts);

        // Setting the adapter to listview
        lstContacts.setAdapter(mAdapter);

        // Creating an AsyncTask object to retrieve and load listview with contacts
        ListViewContactsLoader listViewContactsLoader = new ListViewContactsLoader();

        // Starting the AsyncTask process to retrieve and load listview with contacts
        listViewContactsLoader.execute();

        //Selecting and highlighting the elements in the listview
        //Creating the context menu and the options for it
        listview = (ListView) findViewById(R.id.lst_contacts);
        listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
        listview.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
            @Override
            public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
                try
                {
                    final int checkedCount = listview.getCheckedItemCount();
                    mode.setTitle("Contacts: " + checkedCount);

                    if (checked)
                    {
                        count = count+1;
                        listview.getChildAt(position).setBackgroundColor(Color.parseColor("#6DCAEC"));
                    }
                    else
                    {
                        count = checkedCount;
                        listview.getChildAt(position).setBackgroundColor(Color.parseColor("#E7E8E9"));
                    }

                }
                catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {

                MenuInflater inflater = mode.getMenuInflater();
                inflater.inflate(R.menu.contact_context_menu, menu);

                return true;
            }

            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                return false;
            }

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.delete_id:
                        Toast.makeText(getBaseContext(), count + " Contacts Deselected", Toast.LENGTH_SHORT).show();
                        count = 0;
                        mode.finish();
                    case R.id.save_id:
                        Toast.makeText(getBaseContext(), count + " Contacts Saved", Toast.LENGTH_SHORT).show();
                        count = 0;
                        mode.finish();
                    case R.id.load_id:
                        Toast.makeText(getBaseContext(), count + " Contacts Loaded", Toast.LENGTH_SHORT).show();
                        count = 0;
                        mode.finish();

                }
                return false;
            }

            @Override
            public void onDestroyActionMode(ActionMode mode) {

            }
        });

    }

0 个答案:

没有答案