【问题标题】:Check/Uncheck all the CheckedTextView in a ListView custom adapter选中/取消选中 ListView 自定义适配器中的所有 CheckedTextView
【发布时间】:2012-08-30 11:31:08
【问题描述】:

我有这样一个自定义适配器:

adapter = new ArrayAdapter<Item>(this,
            R.layout.file_manager, R.id.checkedTextItem,
            fileList) 
            {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // creates view
            LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.item, null);
            view.setFocusable(false);
            CheckedTextView textView = (CheckedTextView) view
                    .findViewById(R.id.checkedTextItem);
            // put the image on the text view
            textView.setCompoundDrawablesWithIntrinsicBounds(
                    fileList[position].icon, 0, 0, 0);              
            textView.setTextColor(Color.WHITE);
            textView.setText(fileList[position].file);
            if(fileList[position].file.equalsIgnoreCase("select all") & (fileList[position].check == true))
            {   
                for(int i =0;i<fileList.length;i++)
                {
                    fileList[i].setItemCheck(true);
                    chosenFile = fileList[i].file;
                    File sel = new File(path + "/" + chosenFile);
                    if (sel.isFile())
                        resFiles.add(sel);
                }
                resFiles.remove(position);
            }
            else if(fileList[position].file.equalsIgnoreCase("select all") & (fileList[position].check == false))
            {
                { 
                    for(int i =0;i<fileList.length;i++)
                    {

                        fileList[i].setItemCheck(false);
                    }

                }
            }
            if(fileList[position].icon == R.drawable.directory_icon)
                textView.setCheckMarkDrawable(null);
            else if(fileList[position].icon == R.drawable.directory_up)
                textView.setCheckMarkDrawable(null);
            if(fileList[position].check == true)
                textView.setChecked(true);
            else
                textView.setChecked(false);

            // add margin between image and text (support various screen
            // densities)
            int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
            textView.setCompoundDrawablePadding(dp5);

            textView.setFocusable(false);

            return view;

        }
    };

我有一个类 Item,我在其中创建了布尔字段检查:+

public class Item {


    public String file;
    public int icon;
    public boolean check;

    public Item(String file, Integer icon, boolean check) {
        this.file = file;
        this.icon = icon;
        this.check = check;
    }
     public String getItemFile()
     {
         return file;
     }

     public int getItemIcon()
     {
         return icon;
     }
     public boolean getItemCheck()
     {
         return check;
     }


     public void setItemCheck(boolean check)
     {
         this.check = check;
     }

     public void setItemFile(String file)
     {
         this.file = file;
     }
     public void setItemIcon(int icon)
     {
         this.icon = icon;
     }
     public void toggle()
     {
         check = !check;
     }

    @Override
    public String toString() {
        return file + " "+ icon+ " "+ check;
    }

}

还有我的listView setOnItemClickListener,我在其中设置了项目的检查状态,它是用Item类的toggle()方法点击的

lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
          public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) 
          {
            //String selectedFromList = (lv.getItemAtPosition(myItemInt).toString());
            myView.setFocusable(false);
            chosenFile = fileList[myItemInt].file;
            File sel = new File(path + "/" + chosenFile);
            Log.i("path",sel.toString());
            if (sel.isDirectory()) {

                firstLvl = false;

                str.add(chosenFile);

                fileList = null;
                path = new File(sel + "");

                loadFileList();
                lv.setAdapter(adapter);

            }
            else if (chosenFile.equalsIgnoreCase("up") && !sel.exists()) {

                // present directory removed from list
                String s = str.remove(str.size() - 1);

                // path modified to exclude present directory
                path = new File(path.toString().substring(0,
                        path.toString().lastIndexOf(s)));
                fileList = null;
                // if there are no more directories in the list, then
                // its the first level

                if (str.isEmpty()) {
                    firstLvl = true;
                }
                loadFileList();
                lv.setAdapter(adapter);
            }
            else
            {

                fileList[myItemInt].toggle(); 
                if (fileList[myItemInt].check == true)
                    resFiles.add(sel);
                else
                    resFiles.remove(sel);
                adapter.notifyDataSetChanged();
                Log.i(fileList[myItemInt].file,Boolean.toString(fileList[myItemInt].check));
            }   
    }

});

我想实现检查所有 CheckedTextView 的能力。我尝试在适配器的 getView() 方法中执行此操作。我可以检查所有项目,但问题在于取消选中。因为当我在“全选”项目的检查状态为假时在语句中调用fileList[i].setItemCheck(false); 时,我无法通过单击检查所有项目。我怎样才能以更好的方式实现这一点?

【问题讨论】:

    标签: android checkbox adapter onclicklistener checkedtextview


    【解决方案1】:

    我通过更改 setOnItemClickListener 方法解决了这个问题。问题是我试图检查我的 loadFileList() 方法中的所有项目,该方法确定所有可检查状态,但我应该处理单个检查和所有检查代码的不同部分。现在我在 setOnItemClickListener 的 else 语句中执行它...它看起来像这样:

                else
                {
    
                    fileList[myItemInt].toggle(); 
                    if(fileList[myItemInt].file.equalsIgnoreCase("select all") & (fileList[myItemInt].check == true))
                    {
                        String selectAllFile = fileList[myItemInt].file;
                        File all = new File(path + "/" + selectAllFile);
                         for(int i =0;i<fileList.length;i++)
                            {
                                chosenFile = fileList[i].file;
                                File select = new File(path + "/" + chosenFile);
                                 fileList[i].setItemCheck(true);
    
    
                                 if (select.isFile())
                                     {          
                                         resFiles.add(select);
                                         resFiles.remove(all);
    
                                     }
    
                             }
    
                         adapter.notifyDataSetChanged();
                    }
    
                    else if(fileList[myItemInt].file.equalsIgnoreCase("select all") & (fileList[myItemInt].check == false))
                    {
                        for(int i =0;i<fileList.length;i++)
                        {
    
                            fileList[i].setItemCheck(false);
    
                        }
                         adapter.notifyDataSetChanged();
                    }
                    if (fileList[myItemInt].check == true)
                        {
                        resFiles.add(sel);
                         if(fileList[myItemInt].file.equalsIgnoreCase("select all"))
                                 {
                                    String selectAllFile = fileList[myItemInt].file;
                                    File all = new File(path + "/" + selectAllFile);
                                    resFiles.remove(all);
                                 }
                        }
                    else
                    resFiles.remove(sel);
                    adapter.notifyDataSetChanged();
                    Log.i(fileList[myItemInt].file,Boolean.toString(fileList[myItemInt].check));
                }   
        }
    
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-11
      • 1970-01-01
      • 1970-01-01
      • 2014-05-20
      • 2021-03-05
      • 1970-01-01
      相关资源
      最近更新 更多