Android enables multiple selection of picture ListView instances for bulk operations _android

Source: Internet
Author: User

This article is an example of the Android implementation for easy bulk operation of the more optional picture listview. Share to everyone for your reference, specific as follows:

Before the project needs to implement a selectable list of pictures, users select one or more pictures, batch upload. But there is more than the choice of ordinary list of code, there is a simple picture list of code, but there is no combination of the two code, had to implement one.

Nonsense not to say, directly on the code.

First two layout:

1, Main.xml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
  android:orientation=" vertical "
  android:layout_width=" fill_parent "
  android:layout_" height= "Fill_parent" >
  <listview
    android:layout_width= "Fill_parent"
    Fill_parent "
    android:focusable=" false "
    android:id=" @+id/lvimagelist ">
  </ListView>
</LinearLayout>

2, Listitem.xml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "android:orientation=" horizontal "android:layout_width=" fill_parent "android:layout_height="? android:attr
    /listpreferreditemheight "> <imageview android:id=" @+id/itemimgimageinfo "android:layout_margintop=" 4dip " Android:layout_marginbottom= "4dip" android:layout_width= "Android:attr/listpreferreditemheight" Android:layout_ height= "? android:attr/listpreferreditemheight" > </ImageView> <twolinelistitem xmlns:android= "http:// Schemas.android.com/apk/res/android "android:layout_width=" fill_parent "android:layout_height=" Fill_parent "an
      droid:paddingleft= "4dip" android:mode= "Twoline" > <checkedtextview android:id= "@+id/itemchkimageinfo" 
      Android:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:gravity= "center_vertical" Android:textappearance= "? Android:atTr/textappearancesmall "android:checkmark=" "Android:attr/listchoiceindicatormultiple" > &LT;/CHECKEDTEXTVIEW&G
    T <textview android:id= "@+id/itemtxtimageinfo" android:layout_width= "Fill_parent" android:layout_height = "Wrap_content" android:gravity= "Center_vertical|top" android:layout_marginbottom= "4dip" Android:layout_
  below= "@+id/itemchkimageinfo" android:textappearance= "Android:attr/textappearancesmall" > </TextView>

 </TwoLineListItem> </LinearLayout>

Followed by code:

Package com.android.MultipleChoiceImageList;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;
Import android.app.Activity;
Import Android.content.Context;
Import Android.database.Cursor;
Import Android.graphics.Bitmap;
Import Android.net.Uri;
Import Android.os.Bundle;
Import Android.provider.MediaStore;
Import Android.provider.MediaStore.Images;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.AdapterView;
Import Android.widget.CheckedTextView;
Import Android.widget.ImageView;
Import Android.widget.ListView;
Import Android.widget.SimpleAdapter;
Import Android.widget.TextView;
Import Android.widget.AdapterView.OnItemClickListener;
  public class Mainactivity extends activity {private ListView lvimagelist;
  Private String imageid= "imageID";
  Private String imagename = "imagename";
  Private String Imageinfo = "Imageinfo"; Private arraylist<string> FileNames = nEW arraylist<string> ();
  Private Multiplechoiceimagelistadapter Madapter; /** called the activity is a.
    * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
    Lvimagelist= (ListView) This.findviewbyid (r.id.lvimagelist);
    Lvimagelist.setitemscanfocus (FALSE); Lvimagelist.setonitemclicklistener (New Onitemclicklistener () {@Override public void Onitemclick (ADAPTERVIEW&L t;? > Parent, view view, int position, long id) {Checkedtextview Checkedtextview = (checkedtextview) view.findview
        Byid (R.id.itemchkimageinfo);
        Checkedtextview.toggle ();
      Madapter.setcheckitem (position, checkedtextview.ischecked ());
    }
    });
      try{string[] from = {imageID, imagename, imageinfo};
      Int[] to = {r.id.itemimgimageinfo, r.id.itemchkimageinfo, r.id.itemtxtimageinfo}; Madapter = new Multiplechoiceimagelistadapter (Mainactivity.this,GetImageList (), R.layout.listitem, from, to);
    Lvimagelist.setadapter (Madapter);
    catch (Exception ex) {return; }//Get Picture List private arraylist<map<string, string>> getimagelist () {arraylist<map<string, Stri
    ng>> imageList = new arraylist<map<string,string>> ();
    Hashmap<string, string> ImageMap;
    Read all images in SD card uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; string[] projection = {mediastore.images.media._id, MediaStore.Images.Media.DISPLAY_NAME,
    MediaStore.Images.Media.DATA, MediaStore.Images.Media.SIZE};
    String selection = MediaStore.Images.Media.MIME_TYPE + "=?";
    String[] Selectionarg ={"Image/jpeg"};
    Cursor mcursor = this.managedquery (URI, projection, selection, Selectionarg, MediaStore.Images.Media.DISPLAY_NAME);
    Imagelist.clear ();
      if (mcursor!= null) {Mcursor.movetofirst (); while (Mcursor.getposition ()!= Mcursor.getcount ()) {imagemap= NewHashmap<string, string> ();
        Imagemap.put (imageID, Mcursor.getstring (Mcursor.getcolumnindex (mediastore.images.media._id)));
        Imagemap.put (ImageName, Mcursor.getstring (Mcursor.getcolumnindex (MediaStore.Images.Media.DISPLAY_NAME)));
        Imagemap.put (Imageinfo, "" + (Mcursor.getlong (Mcursor.getcolumnindex (MediaStore.Images.Media.SIZE))/1024) + "KB");
        Imagelist.add (IMAGEMAP);
        Filenames.add (Mcursor.getstring (Mcursor.getcolumnindex (MediaStore.Images.Media.DATA)));
      Mcursor.movetonext ();
    } mcursor.close ();
  return imageList; //Selectable Picture List Adapter class Multiplechoiceimagelistadapter extends Simpleadapter {private Map<integer, boolean> ma
    P
    Private list<integer> State; Private list<?
    Extends Map<string,?>> mlist;
    Layoutinflater Minflater; Public Multiplechoiceimagelistadapter, list<map<string, string>> data, int resource, String[] From, int[to) {suPer (context, data, resource, from, to);
      Map = new Hashmap<integer, boolean> ();
      Minflater = Layoutinflater.from (context);
      mlist = data;
      for (int i = 0; i < data.size (); i++) {Map.put (I, false);
    state = new arraylist<integer> ();
    @Override public int GetCount () {return mlist.size ();
    @Override public Object getitem (int position) {return position;
    @Override public long getitemid (int position) {return position;
      }//Set entry checked state public void Setcheckitem (int position, Boolean ischecked) {map.put (position, ischecked);
      if (state.contains (position)) State.remove ((Object) position);
      if (ischecked) {state.add (position);
      }//Get list of selected entries public long[] Getcheckitemids () {int count = State.size ();
      long[] ids = new Long[count];
     for (int i = 0; i < count; i++) {ids[i]= (long) state.get (i); } return IDs; @Override public View getview (int position, View Convertview, ViewGroup parent) {if (Convertview = null)
      {Convertview = minflater.inflate (R.layout.listitem, NULL);
      } Checkedtextview Checkedtextview = (checkedtextview) Convertview.findviewbyid (r.id.itemchkimageinfo);
      Checkedtextview.setchecked (Map.get (position));
      Checkedtextview.settext ((String) mlist.get (position). Get (imagename));
      TextView TextView = (TextView) Convertview.findviewbyid (r.id.itemtxtimageinfo);
      Textview.settext ((String) mlist.get (position). Get (Imageinfo));
      Show thumbnail image ImageView image = (ImageView) Convertview.findviewbyid (r.id.itemimgimageinfo); Bitmap BM = MediaStore.Images.Thumbnails.getThumbnail (Getcontentresolver (), Long.parselong ((String) Mlist.get (
      Position). Get (imageID)), Images.Thumbnails.MICRO_KIND, NULL);
      Image.setimagebitmap (BM);
    return convertview;

 }
  }
}

The following are the effects on the emulator:

As a beginner Java and Android, the code is relatively rough, please forgive me, welcome to correct, I appreciate.

For more information on Android-related content readers can view the site: "Android graphics and image processing skills summary", "Android Development introduction and Advanced Course", "Android debugging techniques and common problems solution summary", " Android Multimedia How-to Summary (audio, video, audio, etc), summary of Android Basic components usage, Android View tips Summary, Android layout layout tips and a summary of Android controls usage

I hope this article will help you with the Android program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.