안드로이드 개발 질문/답변
(글 수 45,052)
안녕하세요?
ListView 와 CustomerAdapter 를 써서
List Item 안에 Button 을 넣었습니다.
아래와 같이 코드를 작성 해 보았는데요
listview.setOnItemClickListener
(
new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Log.d("test","test");
}
}
);
근데 item 안에 Button 을 넣으니 이벤트가 발생하지 않네요? ( Button 을 빼면 이벤트가 발생합니다. )
이거 원래 안 되는건가요? 아님 다른 방법이라도 있는지요?
고수님들의 답변 부탁드립니다.
2010.05.04 17:22:06
다른 방법이 있긴합니다.
Adapter를 상속받아서 내부 클래스로 등록 하고 여기 클래스에다가 해당 버튼 id를 bindView나 getView에서 findViewById로 찾아서 OnClickListener 를 달아주면 동작 합니다.
private Class TestAdapter extends CursorAdapter {
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView != null) {
Button btn = (Button) convertView.findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener {
public onClick(View view) {
}
});
}
return super.getView(int position, View convertView, ViewGroup parent);
}
}
정확한 메소드가 맞는지는 모르겠지만 이런 형식으로 구현 하시면 됩니다.