如何在gridview中添加复选框?

时间:2013-04-21 09:26:08

标签: android

如何在网格视图中添加复选框,我使用简单的适配器,所以我该怎么办?我如何在我的网格中添加复选框请帮助我如何在我的代码中添加复选框可以有人帮助我吗? 如何在gridview中添加复选框,以便用户选择多张图片

 SimpleAdapter simpleAdapter;
     GridView gridView;

  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.myexample.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", R.drawable.file);
        }

        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);
     sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" 
        + Environment.getExternalStorageDirectory())));

   }

1 个答案:

答案 0 :(得分:1)

你无法在gridview中进行,但你可以在listview

中进行

这是主要活动

package com.example.checkboxingridview;


import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ListView;

public class MainActivity extends Activity {

    ListView checkboxList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        checkboxList = (ListView) findViewById(R.id.listView1);
        checkboxList.setAdapter(new CustomAdapter(context, resource, textViewResourceId, objects));

    }

    // do what you want if this item checked
    void checkBoxChecked(View v) {
        CheckBox checkbox= (CheckBox)v;
        Object rowObject =checkbox.getTag();
        // here we can do what we want in this checkbox
    }


}

和自定义适配器

package com.example.checkboxingridview;

import java.util.List;

import com.example.pos.R;
import com.util.TransactionCustomAdapter.ViewHolder;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.TextView;

public class CustomAdapter extends ArrayAdapter {

    Context context;
    int layoutResourceId;
public CustomAdapter(Context context, int resource, int textViewResourceId,
        List objects) {
    super(context, resource, textViewResourceId, objects);
    // TODO Auto-generated constructor stub
}

@SuppressLint("NewApi")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    ViewHolder holder;

    if (row == null) {
        LayoutInflater inflater = (LayoutInflater) parent.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.list_row_item, null);

        holder = new ViewHolder();



        holder.setCheckBox((CheckBox) row.findViewById(R.id.checkBox1));


        row.setTag(holder);
    } else {
        holder = (ViewHolder) row.getTag();
    }

    // set tag by the object of this list item
    holder.voidBtn.setTag(itemListObject);

    // StrictMode.ThreadPolicy policy = new
    // StrictMode.ThreadPolicy.Builder()
    // .permitAll().build();
    // StrictMode.setThreadPolicy(policy);

    return row;
}

static class ViewHolder {

    CheckBox checkBox;

    public CheckBox getCheckBox() {
        return checkBox;
    }

    public void setCheckBox(CheckBox checkBox) {
        this.checkBox = checkBox;
    }

}

}

main_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true" >
</ListView>