[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/

[Android] ListView에 내용이 없을때

리스트뷰에 표시할 내용이 없을때에 보통은 텍스트뷰에 '내용이 없다' 라고 출력한다.
참고내용 : http://androidhuman.tistory.com/194

그런데, 이런것때문에 안드로이드가 허접하는둥 뭐 그런말이 자꾸 들리는것 같다.
이쪽 개발업무를 담당하면서 참 듣기 싫은 이야기인데 -_- 말이다.

내용이 없다는것을 'empty'라는 아이디값으로 지정하면, 리스트뷰에 꼽아주는 Adapter의 카운트가 제로일때
이 empty 아이디를 가진놈을 출력해 주더라...

이놈이 아무래도 아이값만 가지고 숨겨져 있던 놈을 그냥 출력해주는것 같았다.
텍스트뷰 말고, 레이아웃에 해당 아이디값을 지정하고 테스트를 해보니 뭐 별거 아닌일이지만 정상작동 하였다.
좀 더 나은 어플을 만들고자 한다면, 조금 더 멋진 레이아웃을 사용자에게 보여줄 의무가 있는것 같다.

아래내용도 허접하지만, 위의 링크에 걸린 '표시할 내용이 없습니다' 보단 조금 더 낫지 않은가?!
가능성이란건 얼마든지 존재한다.

empty 레이아웃
         <LinearLayout  
                 android:id="@android:id/empty"
                 android:layout_width="fill_parent"
                 android:layout_height="120dip"
                 android:gravity="center_horizontal"
                   >
                  <TextView
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layout_gravity="center"
                          android:text="댓글이 없습니다."
                           />
        </LinearLayout>   

결과화면캡처 (일부분은 가림)

사용자 삽입 이미지

[Android] 미디어 갤러리를 이용한 파일첨부

내용은 길게 못 쓰겠다.
어떠한 클릭이나 터치이벤트 발생시, 아래와같은 메서드를 오버라이딩하여 기본 미디어 갤러리로 인텐트를 넘긴다.

    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {           
            ...
            //기본 미디어 갤러리에서 선택 후 사용
            Intent intent = new Intent();
            intent.setAction( Intent.ACTION_GET_CONTENT );
            intent.setType( "image/*" );
            activity.startActivityForResult( intent, this.requestCode );
           
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
             ...
        }
        return false;
    }

그리고 보낸 액티비에서는 콜백메서드를 정의해야 한다.
아래 메서드를 이용하여 정의한다.

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
       
        // requestCode를 이벤트발생시 1로 정의했기때문에 조건문이 포함됨
        if (requestCode == 1) {
           
            //미디어 갤러리를 실행한 뒤의 콜백메서드       
            try {
                if(data!=null){

                    Uri selPhotoUri = data.getData();
                    Bitmap selPhoto = Images.Media.getBitmap( getContentResolver(), selPhotoUri );
   
                    ImageView iView = (ImageView) findViewById(R.id.resultImage);
                    iView.setImageBitmap(selPhoto);
                   
                    TextView filePathTxt = (TextView) findViewById(R.id.filePath);
                    filePathTxt.setText("");
                    filePathTxt.setText("경로 : " + selPhotoUri.getPath());
               
                    ...
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
           
        }
    }

[Android] TableView 모서리 둥글게 처리하기

사용자 삽입 이미지
 
1. res폴더->drawable폴더 에 XML 파일 생성(필자의 경우 table_round_corner)
2. 다음의 코드 입력 후 저장

           <?xml version="1.0" encoding="UTF-8"?>

           <shape xmlns:android="http://schemas.android.com/apk/res/android">

               <solid android:color="#99FFFFFF"/>

               <corners android:radius="15dip"/>

               <padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" /> 

           </shape>


3. 구현하고자 하는 테이블 레이아웃 xml 코드에 다음 줄 추가

android:background="@drawable/table_round_corner"

           android:padding="10dip"

[Android] TextView에서의 줄간격 설정

일반적인 TextView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/aticle_content"
    />
</LinearLayout>

사용자 삽입 이미지


lineSpacingExtra의 설정시

라인의 간격을 더하기로 계산한다.
[Text_height 값] + [LindSpacing 값]

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/aticle_content"
    android:lineSpacingExtra="15dip"
    />
</LinearLayout>

사용자 삽입 이미지


lineSpacingMultiplier의 설정시

라인의 간격을 곱하기로 계산한다.
[ Text_height 값] * [ LindSpacing 값]

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/aticle_content"
    android:lineSpacingMultiplier="2.5"
    />
</LinearLayout>

사용자 삽입 이미지