본문 바로가기

Android/ListView, CheckBox item

Android ListView with CheckBox item

안드로이드 ListView의 아이템으로 CheckBox를 사용하는 예

layout/main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#cccccc" android:text="" /> <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout>


layout/row.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" /> </LinearLayout>


TestActivity.java

package com.example.androidapp; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; public class TestActivity extends Activity { String [] name = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}; ArrayAdapter<String> adapter; TextView tv; ListView listView; public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); adapter = new MyAdapter ( this, R.layout.row, R.id.textView2, name); listView = (ListView) findViewById(R.id.listView1); listView.setAdapter(adapter); tv = (TextView) findViewById(R.id.textView1); } }


MyAdapter.java

package com.example.androidapp; import android.app.Activity; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.ImageView; import android.widget.TextView; public class MyAdapter extends ArrayAdapter<String> implements OnClickListener, OnCheckedChangeListener { Context context; boolean[] checked; public MyAdapter(Context context, int textViewResourceId, String[] objects) { super(context, textViewResourceId, objects); this.context = context; init(); } public MyAdapter(Context context, int resource, int textViewResourceId, String[] objects) { super(context, resource, textViewResourceId, objects); this.context = context; init(); } private void init() { checked = new boolean[getCount()]; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = convertView; if(view==null) { Activity ctx = (Activity) this.context; LayoutInflater inflater =ctx.getLayoutInflater(); view = inflater.inflate(R.layout.row, null); } view.setTag(position); ImageView iv = (ImageView) view.findViewById(R.id.imageView1); TextView tv = (TextView) view.findViewById(R.id.textView2); CheckBox cb = (CheckBox) view.findViewById(R.id.checkBox1); tv.setText(getItem(position)); cb.setOnCheckedChangeListener(null); cb.setChecked(checked[position]); cb.setOnCheckedChangeListener(this); cb.setTag(position); view.setOnClickListener(this); return view; } /* ListView Item Click Listener */ public void onClick(View v) { CheckBox cb = (CheckBox)v.findViewById(R.id.checkBox1); //cb.setOnCheckedChangeListener(null); cb.toggle();//CheckBox를 직접 클릭하지 않고도 ListView의 한 행을 클릭하면 CheckBox가 ON/OFF 되도록 함 //cb.setOnCheckedChangeListener(this); //int position = (Integer)v.getTag(); //checked[position] = cb.isChecked(); } /* CheckBox changed Listener */ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { int position = (Integer) buttonView.getTag(); checked[position] = isChecked; } }