如何在简单适配器中添加复选框

时间:2013-04-29 05:29:42

标签: android

这是我下面的代码,它在网格视图中使用了简单的适配器。每件事都很好但我想在我的代码中添加复选框,这样我该怎么办?我如何在我的简单适配器中添加复选框?我的代码检查文件是否是目录显示图标其他明智的显示图像。我只想添加复选框,以便用户选择复选框并删除复选框选中多个图像

 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.myexample.folder/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.folder
   /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);

2 个答案:

答案 0 :(得分:0)

您需要使用自定义适配器。使用SimpleAdapter填充gridview不会。

答案 1 :(得分:0)

R.layout.line.xml中添加复选框视图。然后你必须扩展ArrayAdapter类(不使用SimpleAdapter)来覆盖getView()方法,如下所示:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    CheckBox cb = (CheckBox) convertView.findViewById(R.id.your_checkbox);
    cb.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            ...
        }
    });

    return convertView;
}