ProtectorAdpater.java
package com.example.customviewtest;
import java.util.ArrayList;
import java.util.Hashtable;
import android.R.integer;
import android.content.Context;
import android.content.DialogInterface.OnClickListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.TextView;
public class ProtectorAdapter extends BaseAdapter {
Context context;
LayoutInflater Inflater;
ArrayList<ProtectorItem> arrayList = new ArrayList<ProtectorItem>();
TextView tvProtector, tvContactNum;
CheckBox cb;
private int layout;
private boolean[] isCheckedConfrim;
public ProtectorAdapter(Context context, int layout,
ArrayList<ProtectorItem> arrayList) {
this.context = context;
this.layout = layout;
this.arrayList = arrayList;
// CheckBox의 true/false를 구별 하기 위해 ArrayList Size 만큼의 boolean 배열을 만든다.
this.isCheckedConfrim = new boolean[this.arrayList.size()];
// LayoutInflater 객체를 Inflater로 반환받는다.
Inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
// CheckBox를 모두 선택하는 메서드
public void setAllChecked(boolean ischeked) {
int tempSize = isCheckedConfrim.length;
for (int a = 0; a < tempSize; a++) {
isCheckedConfrim[a] = ischeked;
}
}
public void setChecked(int position) {
isCheckedConfrim[position] = !isCheckedConfrim[position];
}
public void setUnChecked(int position) {
isCheckedConfrim[position] = false;
}
public int[] getChecked() {
int tempSize = isCheckedConfrim.length;
int[] checkArray = new int[tempSize]; // Insert check = true
int i = 0;
for (int b = 0; b < tempSize; b++) {
if (isCheckedConfrim[b]) {
checkArray[i] = b;
i++;
}
}
return checkArray;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return arrayList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return arrayList.get(position).getProtector();
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final int finalPosition = position;
View v = convertView;
// 반환받은 LayoutInflater 객체로부터 레이아웃 객체를 반환받는다. 첫번째 인수는 inflate할 대상의 xml
// 리소스, 두 번째 인수는 생성된 뷰의 루트로 사용할 뷰 객체. 리소스 내에 루트가 따로 있다면 null.
if (v == null) {
// viewHolder = new ViewHolder();
v = Inflater.inflate(layout, parent, false);
// viewHolder.cBox = (CheckBox) v.findViewById(R.id.check);
// viewHolder.tvContactNum = (TextView)
// convertView.findViewById(R.id.contactNum);
// viewHolder.tvProtector = (TextView)
// convertView.findViewById(R.id.protector);
// v.setTag(viewHolder);
} else {
tvProtector = (TextView) convertView.findViewById(R.id.protector);
tvContactNum = (TextView) convertView.findViewById(R.id.contactNum);
tvProtector.setText(arrayList.get(finalPosition).getProtector());
tvContactNum.setText(arrayList.get(finalPosition).getContactNum());
cb = (CheckBox) convertView.findViewById(R.id.check);
cb.setFocusable(false);
cb.setClickable(false);
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) { // when you check
for (int i = 0; i < isCheckedConfrim.length; i++) {
if (getChecked()[i] == finalPosition) {
return;
}
}
setChecked(finalPosition);
} else {// when left unchecked
for (int i = 0; i < isCheckedConfrim.length; i++) {
if (finalPosition == getChecked()[i]) {
setUnChecked(i);
break;
}
}
}
}
});
for (int i = 0; i < isCheckedConfrim.length; i++) {
if (getChecked()[i] == finalPosition){
cb.setChecked(true);
break;
} else {
cb.setChecked(false);
}
}
}
return v;
}
}
listview에 들어갈 adpater 구성 부분입니다. 질문을 찾아보니 이런식으로 하라고 하시는거 같은데... 도무지 해결이 안되서..ㅠㅠ 도와주세요!
※에고;; 죄송합니다 글을 몇번 수정하다가 에러를 지웠나보네요 ㅠㅠ
foucsable, clickable 둘 다 레이아웃에서 false로 설정도 해줬는데 체크가 안됩니다.
혹시나 체크박스 리스너의 if문에서 문제가 발생한거 같아 이것저것 수정해봤는데 잘 안되네요..고수님들의 도움이 절실합니다!
