Search code examples
androidcontextual-action-bar

Contextual action bar with Adapter


I am trying to use contextual action bar for the first time and I have this design problem: In my main activity I have set an adapter to my list:

    // Create an adapter
    Adapter = new ToDoItemAdapter(this, 
                                  R.layout.list_date_item, 
                                  toDoListCursor, 
                                  from, 
                                  to,
                                  toDoDBAdapter);
    List.setAdapter(Adapter);

Now I need to setOnLongClickListener to each list item. where can I do that cause if I write it in my activity I don't have access to each 'view' like I have in the Adapter but if I write it in the Adapter class I don't have access to ActionMode.Callback or more important - to my logical methods such as - RemoveItem, ShareItem etc.

I would like to know what is the best practice to do this.


Solution

  • I have Done this in item click of ListView.

    Note: i have used actionbarsherlock library.

    enter image description here

    check this code:
    
    package com.example.testapp;
    
    import android.app.Activity;
    import android.content.Context;
    import android.os.Bundle;
    import android.text.Html;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.View.OnClickListener;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.ListView;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import com.actionbarsherlock.app.SherlockListActivity;
    import com.actionbarsherlock.view.ActionMode;
    import com.actionbarsherlock.view.Menu;
    import com.actionbarsherlock.view.MenuItem;
    
    public class MainActivity extends SherlockListActivity {
    
        private static final String[] COUNTRIES = new String[] { "Belgium",
                "France", "Italy", "Belgium", "France", "Italy", "Belgium",
                "France", "Italy" };
    
        private static final Integer[] img = new Integer[] {
                R.drawable.ic_launcher, R.drawable.dhaval1, R.drawable.ic_launcher,
                R.drawable.ic_launcher, R.drawable.dhaval1, R.drawable.ic_launcher,
                R.drawable.ic_launcher, R.drawable.dhaval1, R.drawable.ic_launcher };
    
        private static final String[] COUNTRIES_Detail = new String[] {
                "Belgium , officially the Kingdom of Belgium, is a federal state in Western Europe.",
                "France (English , officially the French Republic (French: République française ",
                "Italy i/ˈɪtəli/ (Italian: Italia [iˈtaːlja]), officially the Italian Republic ",
                "Belgium , officially the Kingdom of Belgium, is a federal state in Western Europe.",
                "France (English , officially the French Republic (French: République française ",
                "Italy i/ˈɪtəli/ (Italian: Italia [iˈtaːlja]), officially the Italian Republic ",
                "Belgium , officially the Kingdom of Belgium, is a federal state in Western Europe.",
                "France (English , officially the French Republic (French: République française ",
                "Italy i/ˈɪtəli/ (Italian: Italia [iˈtaːlja]), officially the Italian Republic " };
        private MyArrayAdapter adapter;
        Bundle savedInstanceState;
    
        private ActionMode mMode;
    
        private static ListView list;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.activity_main);
    
            list = getListView();
            adapter = new MyArrayAdapter(MainActivity.this, COUNTRIES,
                    getListView());
            getListView().setAdapter(adapter);
            if (COUNTRIES.length <= 0) {
                getListView().setEmptyView(findViewById(android.R.id.empty));
            }
    
        }
    
        protected void onRestart() {
            // adapter.notifyDataSetChanged();
            onCreate(savedInstanceState);
        };
    
        class MyArrayAdapter extends BaseAdapter {
    
            private LayoutInflater mInflater;
            private Context mcon;
            private String[] COUNTRIES_;
            private ListView listView_;
    
            public MyArrayAdapter(Activity con, String[] countries,
                    ListView listView) {
                // TODO Auto-generated constructor stub
                mcon = con;
                COUNTRIES_ = countries;
                listView_ = listView;
                mInflater = LayoutInflater.from(con);
            }
    
            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return COUNTRIES_.length;
            }
    
            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return position;
            }
    
            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return position;
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                final ListContent holder;
                View v = convertView;
                if (v == null) {
                    v = mInflater.inflate(R.layout.my_spinner_style, null);
                    holder = new ListContent();
                    holder.line = (LinearLayout) v.findViewById(R.id.line_);
                    holder.name = (TextView) v.findViewById(R.id.textView1);
                    holder.name1 = (TextView) v.findViewById(R.id.textView2);
                    holder.name2 = (ImageView) v.findViewById(R.id.imageView1);
    
                    v.setTag(holder);
                } else {
    
                    holder = (ListContent) v.getTag();
                }
    
                holder.name.setText("" + Html.fromHtml("" + COUNTRIES_[position]));
                holder.name1.setText("" + Html.fromHtml("" + COUNTRIES_[position]));
                // holder.name2.setImageBitmap();
    
                holder.name.setOnClickListener(mOnTitleClickListener3);
    
                return v;
            }
    
        }
    
        class ListContent {
    
            TextView name;
            TextView name1;
            ImageView name2;
            LinearLayout line;
    
        }
    
        public OnClickListener mOnTitleClickListener3 = new OnClickListener() {
            public void onClick(View v) {
                final int position = list.getPositionForView((View) v.getParent());
    
                mMode = startActionMode(new AnActionModeOfEpicProportions());
                Log.d("you are click on Ratings", "you are click on Ratings");
    
            }
        };
    
        public class AnActionModeOfEpicProportions implements ActionMode.Callback {
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                // Used to put dark icons on light action bar
    
                menu.add("Save").setIcon(
    
                R.drawable.ic_launcher)
                        .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    
                menu.add("Search").setIcon(R.drawable.ic_launcher)
                        .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    
                menu.add("Search").setIcon(R.drawable.ic_launcher)
                        .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    
                menu.add("Refresh").setIcon(R.drawable.ic_launcher)
                        .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    
                return true;
            }
    
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                return false;
            }
    
            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                Toast.makeText(MainActivity.this, "Got click: " + item,
                        Toast.LENGTH_SHORT).show();
                mode.finish();
                return true;
            }
    
            @Override
            public void onDestroyActionMode(ActionMode mode) {
            }
    
        }
    
    }
    

    activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>
    
    </LinearLayout>
    

    listview row file my_spinner_style.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:id="@+id/line_"
        android:orientation="horizontal"
        android:weightSum="3" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:visibility="gone" />
    
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher" />
    
    </LinearLayout>
    

    i have used OnClickListener same way you have to use OnLongClickListener

    public OnLongClickListener longclick = new OnLongClickListener() {
    
            @Override
            public boolean onLongClick(View v) {
                // TODO Auto-generated method stub
                mMode = startActionMode(new AnActionModeOfEpicProportions());
                Log.d("you are click on Ratings", "you are click on Ratings");
                return true;
            }
        };
    

    EDIT: without actionbarsherlock

    using AppCompart Support Lib use

    startSupportActionMode
    

    without AppCompart

    startSupportActionMode