title: Android: Vogella Contextual action mode for ListViews with Image
body: <p>I have followed the tutorial from Vogella about Contextual action mode for ListViews from here <a href="http://www.vogella.com/articles/AndroidListView/article.html" rel="nofollow">http://www.vogella.com/articles/AndroidListView/article.html</a>. </p>
  
  <p>Anyway, what I want to do is a bit different. The data used in the tutorial is initialized array of Strings. But My Data is a list of objects retrieved from database. How can I do that? Thanks in advance for the help</p>
  
  <p>Here is the coding of the Custom ArrayAdapter named as MyPerformanceArrayAdapter.java</p>
  
  <pre><code>    package de.vogella.android.listactivity;
      import android.app.Activity;
      import android.view.LayoutInflater;
      import android.view.View;
      import android.view.ViewGroup;
      import android.widget.ArrayAdapter;
      import android.widget.ImageView;
      import android.widget.TextView;
  
       public class MyPerformanceArrayAdapter extends ArrayAdapter&lt;String&gt; {
       private final Activity context;
       private final String[] names;
  
        static class ViewHolder {
        public TextView text;
        public ImageView image;
        }
  
        public MyPerformanceArrayAdapter(Activity context, String[] names) {
         super(context, R.layout.rowlayout, names);
         this.context = context;
        this.names = names;
        }
  
         @Override
         public View getView(int position, View convertView, ViewGroup parent) {
         View rowView = convertView;
          if (rowView == null) {
          LayoutInflater inflater = context.getLayoutInflater();
          rowView = inflater.inflate(R.layout.rowlayout, null);
           ViewHolder viewHolder = new ViewHolder();
          viewHolder.text = (TextView) rowView.findViewById(R.id.TextView01);
           viewHolder.image = (ImageView) rowView
        .findViewById(R.id.ImageView01);
         rowView.setTag(viewHolder);
  }
  
  ViewHolder holder = (ViewHolder) rowView.getTag();
  String s = names[position];
  holder.text.setText(s);
  if (s.startsWith("Windows7") || s.startsWith("iPhone")
      || s.startsWith("Solaris")) {
    holder.image.setImageResource(R.drawable.no);
  } else {
    holder.image.setImageResource(R.drawable.ok);
  }
  
  return rowView;
       }
      }   
  </code></pre>
  
  <p>This is the code of the activity where the String data is initialized </p>
  
  <pre><code>    public class MyListActivityActionbar extends ListActivity {
  
      protected Object mActionMode;
      public int selectedItem = -1;
  
      public void onCreate(Bundle icicle) {
       super.onCreate(icicle);
       setContentView(R.layout.main);
        String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
      "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
      "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
      "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2" };
  
       MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(this, values);
       setListAdapter(adapter);
  
        getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
  
       @Override
         public boolean onItemLongClick(AdapterView&lt;?&gt; parent, View view,
        int position, long id) {
  
      if (mActionMode != null) {
        return false;
      }
      selectedItem = position;
  
      // start the CAB using the ActionMode.Callback defined above
      mActionMode = MyListActivityActionbar.this
          .startActionMode(mActionModeCallback);
      view.setSelected(true);
      return true;
    }
  });
        }
  
        private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
  
  // called when the action mode is created; startActionMode() was called
  public boolean onCreateActionMode(ActionMode mode, Menu menu) {
    // Inflate a menu resource providing context menu items
    MenuInflater inflater = mode.getMenuInflater();
    // assumes that you have "contexual.xml" menu resources
    inflater.inflate(R.menu.rowselection, menu);
    return true;
  }
  
  // the following method is called each time 
  // the action mode is shown. Always called after
  // onCreateActionMode, but
  // may be called multiple times if the mode is invalidated.
  public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
    return false; // Return false if nothing is done
  }
  
  // called when the user selects a contextual menu item
  public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menuitem1_show:
      show();
      // the Action was executed, close the CAB
      mode.finish();
      return true;
    default:
      return false;
    }
  }
  
  // called when the user exits the action mode
  public void onDestroyActionMode(ActionMode mode) {
    mActionMode = null;
    selectedItem = -1;
  }
      };
  
       private void show() {
        Toast.makeText(MyListActivityActionbar.this,
          String.valueOf(selectedItem), Toast.LENGTH_LONG).show();
        }
  
       } 
  </code></pre>