title: ListView Multiselection
body: <p>I tried to implement the multiselection on Android ListView. I want to select/deselect more than one item and these items should keep highlighted or not.
  I don't want to use CheckBoxs for selecting the items.</p>
  
  <p>I put the functionallity in my adapter. Here is the code:</p>
  
  <pre><code>public class MultiSelectionAdapter extends BaseAdapter {
  
  private List&lt;Event&gt; streams;
  private LayoutInflater inflater;
  private Context context;
  
  // I keep the selected items index.
  private static List&lt;Integer&gt; selectedIndexList = new ArrayList&lt;Integer&gt;();
  
  
  public MultiSelectionAdapter(Context context, List&lt;Event&gt; streams) {
      this.streams = streams;
      this.inflater = LayoutInflater.from(context);
      this.context = context;
  
      selectedIndexList.clear();
      Integer i = -1;
      selectedIndexList.add(i);
  }
  
  
  
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
      ViewHolder holder = null;
  
      //if (convertView == null) {
          convertView = inflater.inflate(R.layout.adapter_stream_ins, null);
          holder = new ViewHolder();
          holder.hTitle = (TextView) convertView.findViewById(R.id.adapter_title);
          holder.hProfileImage = (ImageView) convertView.findViewById(R.id.adapter_profileimage);
          holder.hMainImage = (WebImageView) convertView.findViewById(R.id.adapter_mainimage);
          //convertView.setTag(holder);
      //} else {
      //  holder = (ViewHolder) convertView.getTag();
      //}
  
  
      for(Integer i : selectedIndexList){
          if (position == i) {
              // Here I get the view inside the layout and set it to enable (the item has a selector as background) 
              RelativeLayout mainLayout = (RelativeLayout) ((ViewGroup) convertView).getChildAt(1);
              LinearLayout item = (LinearLayout) ((ViewGroup) mainLayout).getChildAt(1);
              item.setSelected(true);
          }
      }
  
  
      Ins eventItem = (Inspiration) getItem(position);
      holder.hTitle.setText(eventItem.getName());
      holder.hMainImage.setImageWithURL(context, WebServiceConfig.getImageUrl(eventItem.getImagePath()));
  
      return convertView;
  }
  
  
  
  public int getCount() {
      return this.streams.size();
  }
  
  public Event getItem(int position) {
      return this.streams.get(position);
  }
  
  public long getItemId(int position) {
      return position;
  }
  
  public void add(Event e) {
      streams.add(e);
  }
  
  public void addAll(List&lt;Event&gt; events) {
      streams.addAll(events);
  }
  
  public void remove(Event e) {
      streams.remove(e);
  }
  
  
  
  public void setSelectdIndexList(List&lt;Integer&gt; l){
      selectedIndexList = l;
      notifyDataSetChanged();
  }
  
  
  class ViewHolder {
      TextView hTitle;
      ImageView hProfileImage;
      WebImageView hMainImage;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I my activity I save the selected items and the indexs in a List</p>
  
  <pre><code>public List&lt;Event&gt; insSelected = new ArrayList&lt;Event&gt;();
  public static List&lt;Integer&gt; selecteditems = new ArrayList&lt;Integer&gt;();
  </code></pre>
  
  <p>Finally in the ListView setOnItemClickListener method I update the list and the adapter.</p>
  
  <pre><code>streamsListView.setOnItemClickListener(new OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView&lt;?&gt; adapter, View view, int position, long arg3) {   
              Ins selectedItem = (Ins) streamsListView.getItemAtPosition(position);
  
  
              if(insSelected.contains(selectedItem)){
                  insSelected.remove(selectedItem);
  
                  selecteditems.remove((Integer)position);
                  eventAdapter.setSelectdIndexList(selecteditems);
                  eventAdapter.notifyDataSetChanged();
              }else{
                  insSelected.add(selectedItem);
  
                  selecteditems.add((Integer)position); 
                  eventAdapter.setSelectdIndexList(selecteditems);
                  eventAdapter.notifyDataSetChanged();
              }
  
          }
      });
  </code></pre>
  
  <p>I know my code in the adapter is totally inefficient because i have to draw the ListView every time I select or deselect an item.</p>
  
  <p>Any ideas about how to improve it?
  Thanks!</p>