2013-10-19 3 views
2

에서 RESULT_OK를 받고 후 listvew의 확인란을 선택합니다. 내 listview 문자열 옆에 확인란이 있습니다. 현재 arraylist에는 총 15 개의 항목이 있습니다. 목록 항목을 클릭하면 onclicklistener은 결과로 camera activity을 시작합니다. 사진을 찍는 데 성공하면 클릭 한 행의 ID와 RESULT_OK의 목록으로 돌아갑니다. 나는 체크 박스가 사진이 성공적으로 찍혔다 고 표시된다는 것을 제외하고는 지금 당장 모든 것이 작동한다.어떻게리스트 뷰에 체크 해제 목록을 달성하기 위해 노력하고 다른 활동

나는 코드가 onActivityResult 방법에 배치 될 필요가 있다는 가정입니다. 나는 정말로 여기서 막 잃어 버렸다. 여기

는 내가 지금까지 가지고있는 것입니다.

이 내 사용자 지정 ArrayAdapter와의 onListItemClickonActivityResult의 시작입니다.

private class GrassAdapter extends ArrayAdapter<Grass> { 
    public GrassAdapter(ArrayList<Grass> grasss) { 
     super(getActivity(), 0, grasss); 
    } 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // If we weren't given a view, inflate one 
    if (convertView == null) { 
     convertView = getActivity().getLayoutInflater() 
      .inflate(R.layout.fragment_grasslist, null); 
    } 

    // Configure the view for this Crime 
    Grass c = getItem(position); 

    TextView titleTextView = 
     (TextView)convertView.findViewById(R.id.photo_titleTextView); 
    titleTextView.setText(c.getName()); 

    CheckBox solvedCheckBox = 
     (CheckBox)convertView.findViewById(R.id.photo_solvedCheckBox); 
    solvedCheckBox.setChecked(c.isChecked()); 

    return convertView; 
} 
} 

public void onListItemClick(ListView l, View v, int position, long id) { 
    Grass selectedValue = (Grass) getListAdapter().getItem(position); 
    //Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show(); 

    // Start CameraActivity 
    Intent i = new Intent(getActivity(), CameraActivity.class); 
    i.putExtra("ID", id); 
    i.putExtra("FILE_NAME", selectedValue.getName()); 
    startActivityForResult(i, POSITION_CHECKED); 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 

    if (requestCode == POSITION_CHECKED) { 
     if (resultCode == Activity.RESULT_OK) { 
      int id = data.getData(); 
      .setChecked(true); 
     } 
    } 

} 

지금 나는 인 텐트 데이터에서 ID를 얻는 데 주저하고 있습니다.

이클립스 Type mismatch cannot covert from Uri to int을 말한다. 내가 ID를 다시 일단

그럼 어떻게 setChecked()에있는 확인란을 설정합니까. 이 코드를 사용

답변

0

에서 번들을 얻을 필요가있다. CameraActivity 그들이 INT를 저장하고이 자사의 의도에 다시 반환해야 :

당신의 onActivityResult를 (에 다음
Intent i = new Intent(); 
i.putExtra("RETURN_POSITION", mSavedInt; 

)이이 일을 위치를 얻을 :

int position = data.getIntExtra("RETURN_POSITION"); 

는 그런 다음을 설정할 수 있습니다 당신의 잔디 수집에 액세스하여 체크 박스 :

mGrass.get(position).setIsChecked(true); 
mGrassAdapter.notifyDataSetChanged(); 

귀하의 getView 호출 얻을 것이다는 항목을 확인하고 너무 검사로 목록보기 항목을 설정 한 것을 알 수있다.

+0

감사합니다. 고맙습니다. 난 그 위치를 확실히 전달하고받을 수있는 위치에 추가하고 싶습니다. ID는 목록의 첫 번째 항목 만 확인하고 다른 항목은 확인하지 않습니다. 나는 ((GrassAdapter) getListAdapter())를 캐스팅해야했다. notifyDataSetChanged(); 나는 파편을 사용하기 때문에 생각합니다. – Mathias

0

시도 :

id = Integer.parseInt(data.getData().toString()); 
0

당신이하고있는 일이 잘못되었습니다. 이를 수행하는 방법이 있습니다. CameraClass 활동에서 당신이 CheckBox 확인을 표시하려면이

Intent intent = new Intent(); 
    Bundle bundle = new Bundle(); 
    bundle.putInt("PhotoTaken",id); 
    intent.putExtras(bundle); 
    setResult(RESULT_OK,intent); 
    finish(); 

처럼 id을 추가해야합니다, 당신은 특정 행의보기에 액세스해야합니다. 이것은 클래스의 시작 부분에 글로벌 View을 선언함으로써 수행 할 수 있습니다. vView intitialize onListItemClick에서 다음

View view_at_position=null; 

. 이것은 당신에게 그 특별한 위치의 시야를 가져다 줄 것입니다.

view_at_position=v; 

onActivityResult에 대신 ID의 의도 데이터 항목의 위치를 ​​통과 Intent data

Bundle bundle = data.getExtras(); 
int id = bundle.getInt("PhotoTaken"); 
CheckBox cb = (CheckBox)view_at_position.findViewById(R.id.photo_solvedCheckBox); 
cb.setChecked(true); 
관련 문제