2012-08-06 2 views
1

여러 항목을 선택할 수있는 이미지 갤러리를 구현하고 있습니다. 기본 레이아웃은 Adapter을 사용하는 GridView입니다. 표시된 모든 항목은 ImageView + CheckBox입니다. 여러 체크 박스가 선택되어있는 경우 GridView에있는 이미지를 터치하기 만하면 모든 체크 박스를 선택 취소하고 싶습니다. 이 기능을 구현하기 위해 notifyDataSetChanged()으로 전화를 걸었습니다. 불행히도이 "모두 선택 취소"기능이 작동하지 않습니다. 나는 아직 초보자이기 때문에 아마도 이해할 수없는 기본적인 것이있을 것입니다. 누군가 내게 내 Adapter에 무슨 문제가 있다고 말할 수 있습니까? 감사. 나는 uncheckAll 방법이 실패하는 방법을 내 초기 게시물을 정확히 설명하지 않는다는 것을 깨달았다DataSet을 변경 한 후 사용자 정의 ImageAdapter가있는 GridView가 업데이트되지 않습니다.

public class ImageAdapter extends BaseAdapter { 
    private Context mContext; 

    // References to our images 
    public Integer[] mThumbIds = { 
      R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, 
      R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, 
      R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, 
      R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3 
    }; 

    // The layout inflater used to retrieve gallery children layouts 
    private LayoutInflater mInflater; 

    // Array used to update checkboxes 
    public boolean[] checkboxesState = new boolean[getCount()]; 

    public ImageAdapter(Context c) { 
     mContext = c; 
     mInflater = (LayoutInflater) 
       c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     // Define the initial checked/unchecked state of the checkboxes 
     for(int i = 0; i < getCount(); i++) { 
      checkboxesState[i] = false; 
     } 
    } 

    public int getCount() { 
     return mThumbIds.length; 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    /* Create a new View for each cell displayed in the GridView. 
    * In our case a View is an instance of RelativeLayout from which 
    * we can extract the image and checkbox to be displayed in the cell 
    */ 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View cell = convertView; 
     if (cell == null) { 
      // No View passed, create one. 
      cell = mInflater.inflate(R.layout.galleryitem, null); 
      // Setup the View content 
      ImageView imageView = (ImageView)cell.findViewById(R.id.thumbImage); 
      CheckBox checkBox = (CheckBox) cell.findViewById(R.id.itemCheckBox); 
      checkBox.setChecked(checkboxesState[position]); 
      checkBox.setId(position); 
      imageView.setImageBitmap(downsample(mThumbIds[position])); 

      // Setup the View behavior 
      imageView.setOnClickListener(new OnClickListener() { 
       public void onClick(View v) { 
        // Uncheck all checked items 
        uncheckAll(); 
       } 
      }); 

      checkBox.setOnClickListener(new OnClickListener() { 
       public void onClick(View v) { 
        CheckBox cb = (CheckBox) v; 
        checkboxesState[cb.getId()] = cb.isChecked(); 
       } 
      }); 
     } 
     return cell; 
    } 

    private Bitmap downsample(int resId){ 
     // Some code here 
    } 

    private void uncheckAll() { 
     // This code does not work. GridView is not refreshed 
     for(int i = 0; i < checkboxesState.length; i++) { 
      checkboxesState[i] = false; 
     } 
     notifyDataSetChanged(); 
    } 
} 

업데이트. 나는 N은 체크 박스를 확인해야하고 호출 할 때 어떤 일이 일어나는 uncheckAll 방법은 다음과 같습니다

  • 초기 확인란이 선택 취소 확인
  • 이 문제가 생각하는 저를 만든

N 체크 박스의 새로운 그룹은 체크

  • gridview 항목이 예기치 않게 GridView에서 위치를 변경했기 때문에 모든 항목에 고유 한 이름이 TextView 추가되었습니다. 제 생각은 옳았습니다 : 화면을 스크롤 할 때 항목의 위치가 GridView으로 바뀝니다. uncheckAll이 호출 될 때도 마찬가지입니다. 나는 나를 위해 일하는 해결책을 발견했다 in this thread.

    참고 :이 개발 단계에서 모든 GridView 항목에 대해 동일한 ImageView 리소스를 사용했기 때문에 처음부터 이동하는 문제에 대해 알지 못했습니다.

  • 답변

    1

    문제는 주로 getView() 메서드가 새 셀 뷰를 올바르게 초기화하지만 재활용 된 셀 뷰의 상태를 업데이트하지 않을 가능성이 높습니다. 기본적으로 각 셀 뷰는 초기화 된 상태 만 표시합니다. 그것은 다음과 비슷한 모습이 될 것입니다

    public View getView(int position, View convertView, ViewGroup parent) { 
        View cell = convertView; 
    
        if (cell == null) { 
         // No View passed, create one. 
         cell = mInflater.inflate(R.layout.galleryitem, null); 
         // Setup the View content 
         ImageView imageView = (ImageView)cell.findViewById(R.id.thumbImage); 
         CheckBox checkBox = (CheckBox) cell.findViewById(R.id.itemCheckBox); 
         checkBox.setChecked(checkboxesState[position]); 
         checkBox.setId(position); 
         imageView.setImageBitmap(downsample(mThumbIds[position])); 
    
         // Setup the View behavior 
         imageView.setOnClickListener(new OnClickListener() { 
          public void onClick(View v) { 
           // Uncheck all checked items 
           uncheckAll(); 
          } 
         }); 
    
         checkBox.setOnClickListener(new OnClickListener() { 
          public void onClick(View v) { 
           CheckBox cb = (CheckBox) v; 
           checkboxesState[cb.getId()] = cb.isChecked(); 
          } 
         }); 
        } else { 
         ImageView imageView = (ImageView)cell.findViewById(R.id.thumbImage); 
         CheckBox checkBox = (CheckBox) cell.findViewById(R.id.itemCheckBox); 
         checkBox.setChecked(checkboxesState[position]); 
         checkBox.setId(position); 
         imageView.setImageBitmap(downsample(mThumbIds[position])); 
        } 
    
        return cell; 
    } 
    

    은 또한이 tutorial 살펴보고 당신의 getView() 메소드의 성능을 개선 할 수 있습니다.

    +0

    답변 해 주셔서 감사합니다. 그것은 내 포스트 업데이트에서 언급 한 스레드의 동일한 방향을 가리키며 잘 작동합니다. 튜토리얼을 알지 못했습니다. 나는 그것을 코드에 적용하려고 노력할 것이다. – Vicent

    +0

    4 년 후에 나는 너에게 upvote를 주겠지? 그럼 뭐라 할 수 있니? 이 하나가 나를 미쳤어! 감사! – ZengoTim

    관련 문제