마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

리스트뷰를 클릭시 체크박스에 체크가 되게 하려고 합니다.

0 추천
public class ListString {

 String _title;
 boolean _check = false;

 public ListString(String title, boolean check) {

  this._title = title;
  this._check = check;
 }

 public String getTitle() {
  return _title;
 }

 public void setTitle(String title) {
  this._title = title;
 }

 public boolean ismCheck() {
  return _check;
 }

 public void setmCheck(boolean check) {
  this._check = check;
 }

}

=====================================
public class ListViewAdapter extends BaseAdapter{
 
 private Context _context;
 private List<ListString> _itemList;
 private int _layout;
 ViewHolder holder;
 private boolean checking;
 
 
 public ListViewAdapter(Context context, int layout, List<ListString> itemList){
  
  
  this._context = context;
  this._layout = layout;
  this._itemList = itemList;
 }
 
 
 @Override
 public int getCount() {
  // TODO Auto-generated method stub
  return _itemList.size();
 }

 @Override
 public Object getItem(int position) {
  // TODO Auto-generated method stub
  return _itemList.get(position);
 }

 @Override
 public long getItemId(int position) {
  // TODO Auto-generated method stub
  return _itemList.indexOf(getItem(position));
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  // TODO Auto-generated method stub
  LayoutInflater vi = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  
  if(convertView == null){
   
   convertView = vi.inflate(_layout, null);
   holder = new ViewHolder();
   holder.title = (TextView) convertView.findViewById(R.id.code);
   holder.chb = (CheckBox) convertView.findViewById(R.id.checkBox1);
   
   convertView.setTag(holder);
  } else {
   holder = (ViewHolder) convertView.getTag();
  }
  
  ListString rowItem = (ListString) getItem(position);
  
  holder.title.setText(rowItem.getTitle());
  holder.chb.setText(null);
  holder.chb.setChecked(false);
  holder.chb.setChecked(rowItem.ismCheck());
//  holder.chb.setTag(rowItem);
  
//  holder.chb.setChecked(((ListView)parent).isItemChecked(position));
  
  return convertView;
 }
 

}

어떻게 설명을 해야 될지... 일단 위와 같이 각각의 소스를 해보았습니다.

일단 하고자 하는 것은 listView를 클릭을 하면 체크박스가 체크가 되고 다지 않고를 하려고 합니다.

리스트뷰가 있는 class 에 onItemClick 부분에

 

ListString list = (ListString) parent.getItemAtPosition(position);

if(list.ismCheck() == false){

   list.setmCheck(true);

} else {

   list.setmCheck(false);

}

 

이런 구문을 넣었습니다... 하지만 제대로 되지 않더군요....생각나는 방법을 모두 동원 해보았지만...

어떠한 방법을 하는 것이 좋은지...부탁 드립니다 ㅜㅜ

 

 

안왕초보 (1,390 포인트) 님이 2014년 8월 20일 질문

1개의 답변

+1 추천
 
채택된 답변
notifyDataSetChanged()를 불러야 리스트뷰에 반영되겠죠
alkyne (22,960 포인트) 님이 2014년 8월 20일 답변
안왕초보님이 2014년 8월 21일 채택됨
감사합니다 해결 봤습니다 ㅎㅎ
...