2012-08-04 4 views
1

문자열 배열에 체크 된 항목 목록을 가져 오려고합니다. 나는 videoList를 listview로 가져 왔고 기본 어댑터를 구현하고 baseadapter를 재정의하고 확인란을 사용하여 사용자 정의했습니다.
나는 이것을 시도했지만 nullpointerexception을 제공합니다.목록보기에서 체크 된 항목을 오버라이드베이스 어댑터로 연결했습니다. android

btnplay.setOnClickListener(new View.OnClickListener() 
    { 
     SparseBooleanArray sparseBooleanArray = videolist.getCheckedItemPositions(); 

     @Override 
     public void onClick(View v) 
     { 

      for(int i =0 ; i<videolist.getCount();i++) 
      { 

       if(sparseBooleanArray.get(i)) 
       { 

        String str = (String) videolist.getItemAtPosition(sparseBooleanArray.keyAt(i)); 
        Toast.makeText(getBaseContext(),str , 3).show(); 
       } 
      } 


     } 
    }); 


public class VideoAdapter extends BaseAdapter 
{ 
    private Context vContext; 
    public VideoAdapter(Context c) 
    { 
     vContext = c; 
    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return count; 
    } 

    @Override 
    public Object getItem(int arg0) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public long getItemId(int arg0) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 

     System.gc(); 
     LayoutInflater inflater = (LayoutInflater) vContext.getSystemService(vContext.LAYOUT_INFLATER_SERVICE); 
     View view; 
     CheckBox cb; 
       String id = null; 
     if (convertView == null) 
     { 
      view = new View(vContext); 

      view = inflater.inflate(R.layout.row, null); 

      cb = (CheckBox) view.findViewById(R.id.checkBox1); 


      video_column_index = videocursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID); 

      videocursor.moveToPosition(position); 


      video_column_index = videocursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME); 

      videocursor.moveToPosition(position); 

      id = videocursor.getString(video_column_index); 

      video_column_index = videocursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE); 

      videocursor.moveToPosition(position); 

      id += " Size(KB):" + videocursor.getString(video_column_index); 

      cb.setText(id); 
     } else 
       view = convertView; 
     return view; 
} 
+0

항상 Android 용 질문/예외를 위해 Logcat을 게시하십시오. – cklab

답변

1

여기서는 많은 중요한 기능을 무시하고 구현하지 않았습니다. 예를 들어

, 당신은 :

@Override 
public Object getItem(int arg0) { 
    // TODO Auto-generated method stub 
    return null; 
} 

당신은 항상 null를 반환 할 때 당신은 어떻게 이제까지 ListView에서 항목을 검색 할 수 있습니다?

아마 어댑터의 훨씬 더 완전한 구현을 확장하려고합니다. 빈 구현이있는 getItem과 같은 메서드를 재정의하지 않고 ArrayAdapter을 사용해보십시오. ArrayAdapter을 사용하는 방법을 쉽게 검색 할 수 있습니다.

1

시도해보십시오.

yourCheckBox.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

       if (cb.isChecked() == true) { 
        String getString = yourCheckBox.get(position).getText; //Here you will get text of checkbox 
        yourList.add(getString); // Here you have to add your string value to list.     

       }     
      } 
     }); 
0

은 기본적으로 목록보기의 선택 모드는 CHOICE_MODE_SINGLE로 설정, 그래서 당신은 getCheckedItemPositoins()가 null 이외 SparseBooleanArray를 반환 할 수 있도록, 단일 또는 복수로 선택 모드를 설정해야합니다.

희망이 있으면 도움이됩니다.

관련 문제