Make click events on the profile list available to the Activity
authorTobias Brunner <tobias@strongswan.org>
Tue, 17 Jul 2012 17:57:51 +0000 (19:57 +0200)
committerTobias Brunner <tobias@strongswan.org>
Mon, 13 Aug 2012 09:00:09 +0000 (11:00 +0200)
If the Activity this fragment is placed in implements the provided interface
it is notified about clicks on any of the profiles.

src/frontends/android/src/org/strongswan/android/ui/VpnProfileListFragment.java

index 252330b..be0a900 100644 (file)
@@ -53,6 +53,14 @@ public class VpnProfileListFragment extends Fragment
        private VpnProfileDataSource mDataSource;
        private VpnProfileAdapter mListAdapter;
        private ListView mListView;
+       private OnVpnProfileSelectedListener mListener;
+
+       /**
+        * The activity containing this fragment should implement this interface
+        */
+       public interface OnVpnProfileSelectedListener {
+               public void onVpnProfileSelected(VpnProfile profile);
+       }
 
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
@@ -62,6 +70,7 @@ public class VpnProfileListFragment extends Fragment
 
                mListView = (ListView)view.findViewById(R.id.profile_list);
                mListView.setEmptyView(view.findViewById(R.id.profile_list_empty));
+               mListView.setOnItemClickListener(mVpnProfileClicked);
                mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
                mListView.setMultiChoiceModeListener(mVpnProfileSelected);
                mListView.setAdapter(mListAdapter);
@@ -94,6 +103,17 @@ public class VpnProfileListFragment extends Fragment
        }
 
        @Override
+       public void onAttach(Activity activity)
+       {
+               super.onAttach(activity);
+
+               if (activity instanceof OnVpnProfileSelectedListener)
+               {
+                       mListener = (OnVpnProfileSelectedListener)activity;
+               }
+       }
+
+       @Override
        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
        {
                inflater.inflate(R.menu.profile_list, menu);
@@ -138,6 +158,17 @@ public class VpnProfileListFragment extends Fragment
                super.onActivityResult(requestCode, resultCode, data);
        }
 
+       private final OnItemClickListener mVpnProfileClicked = new OnItemClickListener() {
+               @Override
+               public void onItemClick(AdapterView<?> a, View v, int position, long id)
+               {
+                       if (mListener != null)
+                       {
+                               mListener.onVpnProfileSelected((VpnProfile)a.getItemAtPosition(position));
+                       }
+               }
+       };
+
        private final MultiChoiceModeListener mVpnProfileSelected = new MultiChoiceModeListener() {
                private HashSet<Integer> mSelected;
                private MenuItem mEditProfile;