[Android] ListView에서의 버튼 이벤트




안드로이드 리스트를 사용하다보면, 리스트내의 버튼에 대한 이벤트가 먹지 않는 경우가 생긴다.
일반적인 OnClick 이벤트를 사용하면 절대~ 먹혀들지 않는다. 단지 에러메시지가 발생할뿐...
 
리스트내의 뷰를 처리하는 getView() 메서드내에서 LayoutInflater 를 이용해 해당 내부 레이아웃을 가져온뒤
내부레이아웃.findViewById() 등으로 해당 객체를 가져와 이벤트를 맥이면 된다는.. .뭐 그런 이야기...
 
getView()의 코드는 아래와 같다. 물론 첨부된 예제코드와 별반 다를건 없다.
 
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
 
  final ViewHolder holder;
 
  LayoutInflater inf = activity.getLayoutInflater();
  View customcell = inf.inflate(R.layout.layout_list_cell, null);
 
  VO vo = (VO) cellArray.get(position);
  TextView total_list_cell_textView01 = (TextView) customcell.findViewById(R.id.comment_name);
  TextView total_list_cell_textView02 = (TextView) customcell.findViewById(R.id.comment_date);
  TextView total_list_cell_textView03 = (TextView) customcell.findViewById(R.id.comment_content);
 
  total_list_cell_textView01.setText(vo.getContentTitle());
  total_list_cell_textView02.setText(vo.getProfileImg());
  total_list_cell_textView03.setText(vo.getContentText());
 
  holder = new ViewHolder();
  ButtonEvent deleteEvent = new ButtonEvent(this.activity);
  holder.deleteButton = (ImageButton) customcell.findViewById(R.id.deleteButton);
  holder.deleteButton.setOnClickListener(deleteEvent);
 
  return customcell;
 }
 
    static class ViewHolder {
        ImageButton deleteButton;
    }
아래는 검색했던 블로그의 포스트내용...

안드로이드가 설치되면 다음의 경로에서 ApiDemos의 샘플소스들을 확인해 볼 수 있었다.
android-sdk_r04-windows\android-sdk-windows\platforms\android-2.1\samples\ApiDemos\src\com\example
그중에서 List14.java를 참고하여 리스트뷰에서 텍스트 및 버튼의 클릭이벤트(데이터값으로 받아왔음)를 처리가능하였다. 그 값은 logcat에서 확인가능하다.

reference : http://developer.android.com/reference/android/widget/ListView.html

출처 : http://youngik.tistory.com/