2012-08-30 4 views
0

의 모든 CheckedTextView의 선택을 취소 :확인/I는 사용자 정의 어댑터가리스트 뷰 사용자 정의 어댑터

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; 

     } 
    }; 

은 내가 부울 필드 체크를 생성하는 클래스 항목이 : +

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() 메소드에서이를 시도한다. 모든 항목을 확인할 수 있지만 문제는 선택 취소되어 있습니다. 'select all'항목의 체크 상태가 거짓 인 경우 문에 fileList[i].setItemCheck(false);을 호출 할 때 클릭하여 모든 항목을 확인할 수 없기 때문입니다. 더 나은 방법으로 이것을 어떻게 구현할 수 있습니까?

답변

0

setOnItemClickListener 메서드를 변경하여 해결했습니다. 문제는 모든 checkable 상태를 결정하는 내 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)); 
      } 
    } 

}); 
관련 문제