2016-07-11 3 views
0

Activity [ListViewButton]을 포함하는 샘플 응용 프로그램을 레이아웃 파일에 만듭니다. ListView은 [Label/Name and CheckBox]가 포함 된 사용자 정의입니다. 목록 아이템 CheckBox check [T/F]에 따라 ListView의 어댑터 클래스에서 Button의 텍스트를 변경하는 코드를 작성하고 싶습니다. 활동에ListView 항목 선택을 기반으로 활동 UI를 업데이트하는 방법은 무엇입니까?

+0

@SathishKumarJ. Button 인스턴스가 Adapter 클래스에서 처리해야하는 Activity 클래스에 있기 때문에. 질문을주의 깊게 읽으십시오. – VVB

+0

인터페이스를 사용할 수 있다고 생각합니다. checkBox 콜백의 onCheck를 기반으로 텍스트를 변경할 수 있습니다 .. – Raghavendra

+0

@Raghavendra 위의 질문을 신중하게 읽으십시오. – VVB

답변

0
 listView.setOnItemClickListener(new OnItemClickListener() 
      { 
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
       { 
        // When clicked, show a toast with the TextView text 
        AppListOfAllApps Selecteditems = (AppListOfAllApps) parent.getItemAtPosition(position); 
        if (view != null) 
        { 
         CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBox1); 
         Selecteditems = (AppListOfAllApps) checkBox.getTag(); 
         //here you will get the selected item you may also get the text from it accordingly and then using using button variable just set text 
button.settext("whatever"); 
        } 
       } 
      }); 
0

: 어댑터 클래스에서

public class Your_Activity extends Activity implements OnCheckListener// Implement your listener here 

@Override 
public void OnCheck(int position) { 
    // TODO Auto-generated method stub 
    // notify your activity component here 
} 

: 위의 방법은 UI를 업데이트하는 데 도움이 비록 어댑터 클래스를 관리 할 와서 어떻게

private OnCheckListener listener; 

public interface OnCheckListener { 
    public void OnCheck(int position); 
} 

public Your_adapter_constructor(OnCheckListener listener) { 
    // TODO Auto-generated constructor stub 
    this.listener = listener; 
} 

// On your getView() 
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
       @Override 
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
        listener.OnCheck(position);// If you want to pass some value add it here 
       } 
      }); 
+0

코드를 사용해 보았습니다. 일하지 않았어. 액티비티의 청취자 메소드 안에 들어가는 것이 아닙니다. – VVB

관련 문제