Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android實現便於批量操作可多選的圖片ListView實例

Android實現便於批量操作可多選的圖片ListView實例

編輯:關於Android編程

本文實例講述了Android實現便於批量操作可多選的圖片ListView。分享給大家供大家參考,具體如下:

之前項目需要實現一個可多選的圖片列表,用戶選中一到多張圖片後,批量上傳。但是網上有可多選普通列表的代碼、也有單純圖片列表的代碼,卻沒有兩者合並的代碼,只好自己實現一個。

廢話不說,直接上代碼。

先是兩個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"
    android:layout_height="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"
    android: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">
    </CheckedTextView>
    <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>

接著是代碼:

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 when the activity is first created. */
  @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<?> parent, View view, int position, long id) {
        CheckedTextView checkedTextView = (CheckedTextView) view.findViewById(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;
    }
  }
  //獲取圖片列表
  private ArrayList<Map<String, String>> GetImageList(){
    ArrayList<Map<String, String>> imageList = new ArrayList<Map<String,String>>();
    HashMap<String, String> imageMap;
    //讀取SD卡中所有圖片
    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= new HashMap<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;
  }
  //可多選圖片列表適配器
  class MultipleChoiceImageListAdapter extends SimpleAdapter {
    private Map<Integer, Boolean> map;
    private List<Integer> state;
    private List<? extends Map<String, ?>> mList;
    LayoutInflater mInflater;
    public MultipleChoiceImageListAdapter(Context context, 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;
    }
    //設置條目選中狀態
    public void setCheckItem(int position, Boolean isChecked){
      map.put(position, isChecked);
      if (state.contains(position))
        state.remove((Object)position);
      if (isChecked){
        state.add(position);
      }
    }
    //獲取列表中已選中條目
    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));
      //顯示圖片縮略圖
      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;
    }
  }
}

下面是模擬器上的效果:

由於是初學java和android,代碼寫得比較糙,請見諒,歡迎指正,不勝感激。

更多關於Android相關內容感興趣的讀者可查看本站專題:《Android圖形與圖像處理技巧總結》、《Android開發入門與進階教程》、《Android調試技巧與常見問題解決方法匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結》

希望本文所述對大家Android程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved