如何在gridview中添加复选框?所以多选是可能的

时间:2013-04-22 22:38:23

标签: android

下面是我的源代码,单个选择工作正常我想添加复选框选择多个图片PLZ帮我怎么办?我该如何更改simpleadapter?或者如何在simpleadapter中添加复选框?请帮助我这么混乱

      public class FileGridActivity extends Activity {

  GridView gridView;
  TextView textView;
  File currentParent;
  File[] currentFiles;
  String[] currentFilePath;
  ImageButton AddPictures;
  SimpleAdapter simpleAdapter;
  final Context context = this;
  DataBase db;
  List<AppPhotosData> picList;
  ProgressDialog myProgressDialog = null;
  File CameraRoll;



  File root;

Handler handle = new Handler(){ 
    public void handleMessage(android.os.Message msg) {

        if (msg.what == 1)
        {
            hideProgress();

            //list.setAdapter(new AppPhotosAdapter(getApplicationContext(),activity,0,picList));
        }
        else if (msg.what == 2)
        {
            hideProgress();
        }
        super.handleMessage(msg);   
    };
};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    File photos = new File(getFilesDir(),"photos");
    photos.mkdirs();

    File root1 = new File("/data/data/com.newsoftwares.folderlock/files/");

    currentParent = root1;
    currentFiles = root1.listFiles();

    currentFilePath = new String[currentFiles.length];
    int count = 0;

    for(File f:currentFiles)
    {   
        currentFilePath[count] = f.getAbsolutePath();
        count++;
    }


    gridView =(GridView)findViewById(R.id.grid);
    gridView.setOnItemClickListener(new OnItemClickListener(){
        public void onItemClick(AdapterView<?> parent, View view, int position,long id) {


        if(currentFiles[position].isDirectory())
        {
            root = new File("/data/data/com.newsoftwares.folderlock/files/"+FileName(currentFilePath[position])+"/");

            Log.e("Root first",root+ " ");

            currentFiles = root.listFiles();

            inflateListView(currentFiles);
        }
        else if(currentFiles[position].isFile())
        {
                     inflateListView(currentFiles);
        }
    }

    });


                     private void inflateListView(File[] files){

List<Map<String,Object>> listItems = new ArrayList<Map<String,Object>>();

for(int i=0;i<files.length;i++)
{       
        Map<String, Object> listItem = new HashMap<String, Object>();

        if(files[i].isDirectory())
        {
            listItem.put("icon", R.drawable.folder);
        }
        else
        {
            listItem.put("icon",  files[i]);
        }

        listItem.put("fileName", files[i].getName());
        listItems.add(listItem);
    }

    simpleAdapter=new SimpleAdapter(this,listItems,R.layout.line,new String[] 
   {"icon","fileName"},new int[]{R.id.icon,R.id.file_name});
    gridView.setAdapter(simpleAdapter);




           <!-----------main.xml------------>

              <?xml version="1.0" encoding="utf-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >




          <!----line.xml------------->


               <?xml version="1.0" encoding="utf-8"?>
       <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"

    android:padding="5dip" >



  <ImageView android:id="@+id/icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="10dp"

    />


    </RelativeLayout>


    <GridView 
        android:id="@+id/grid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:horizontalSpacing="0pt"
        android:verticalSpacing="1pt"
        android:numColumns="3"
        android:gravity="center"
        ></GridView>



    </LinearLayout>

1 个答案:

答案 0 :(得分:1)