2014-04-04 67 views
1

我已經創建了一個應用程序,在其中我檢查了一些複選框,採取他們的values.But每當我檢查單個複選框默認情況下其他一些複選框也得到勾選在list.i不知道什麼是問題,爲什麼它被檢查。 適配器複選框得到檢查,沒有點擊它們

package com.example.smscampaign; 

import java.util.ArrayList; 
import java.util.List; 

import android.content.Context; 
import android.util.SparseBooleanArray; 
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.Filter; 
import android.widget.Filterable; 
import android.widget.TextView; 


public class SearchableAdapter extends BaseAdapter implements Filterable, OnCheckedChangeListener { 

    public SparseBooleanArray mCheckStates; 
    private List<ProfileBean>originalData = null; 
    private List<ProfileBean>filteredData = null; 
    private LayoutInflater mInflater; 
    private ItemFilter mFilter = new ItemFilter(); 

    public SearchableAdapter(Context context, List<ProfileBean> data) { 
     //mCheckStates = new SparseBooleanArray(filteredData.size()); 
     mCheckStates = new SparseBooleanArray(data.size()); 
     this.filteredData = data ; 
     this.originalData = data ; 
     mInflater = LayoutInflater.from(context); 
    } 

    public int getCount() { 
     return filteredData.size(); 
    } 

    public Object getItem(int position) { 
     return filteredData.get(position); 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 

     ViewHolder holder; 


     if (convertView == null) { 
      convertView = mInflater.inflate(R.layout.row, null); 


      holder = new ViewHolder(); 
      holder.name = (TextView) convertView.findViewById(R.id.textView1); 
      holder.number = (TextView) convertView.findViewById(R.id.textView2); 
      holder.chk = (CheckBox) convertView.findViewById(R.id.checkBox1); 
      holder.chk.setTag(position); 

      holder.chk.setChecked(mCheckStates.get(position, false)); 
      holder.chk.setOnCheckedChangeListener(this); 


      convertView.setTag(R.layout.row,holder); 
     } else { 

      holder = (ViewHolder) convertView.getTag(R.layout.row); 
     } 



     ProfileBean bean = filteredData.get(position); 
     holder.name.setText(bean.getName()); 
     holder.number.setText(bean.getNumber()); 

     convertView.setTag(bean); 

     return convertView; 
    } 

    static class ViewHolder { 
     TextView name; 
     TextView number; 
     CheckBox chk; 
    } 

    public boolean isChecked(int position) { 

     return mCheckStates.get(position, false); 
    } 

    public void setChecked(int position, boolean isChecked) { 
     mCheckStates.put(position, isChecked); 
    } 

    public void toggle(int position) { 
     setChecked(position, !isChecked(position)); 
    } 



    public Filter getFilter() { 
     return mFilter; 
    } 

    private class ItemFilter extends Filter { 
     @Override 
     protected FilterResults performFiltering(CharSequence constraint) { 

      String filterString = constraint.toString().toLowerCase(); 

      FilterResults results = new FilterResults(); 

      final List<ProfileBean> list = originalData; 

      int count = list.size(); 
      final ArrayList<ProfileBean> nlist = new ArrayList<ProfileBean>(count); 

      String filterableString ; 

      for (int i = 0; i < count; i++) { 

       ProfileBean bean = list.get(i); 
       filterableString = bean.getName(); 
       if (filterableString.toLowerCase().contains(filterString)) { 
        nlist.add(bean); 
       } 
      } 

      results.values = nlist; 
      results.count = nlist.size(); 

      return results; 
     } 

     @SuppressWarnings("unchecked") 
     @Override 
     protected void publishResults(CharSequence constraint, FilterResults results) { 
      filteredData = (ArrayList<ProfileBean>) results.values; 
      notifyDataSetChanged(); 
     } 

    } 





    @Override 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
     // TODO Auto-generated method stub 
     mCheckStates.put((Integer) buttonView.getTag(), isChecked); 

    } 




} 
+0

張貼了我的答案檢查這樣的.. –

回答

2

您使用ViewHolder模式,使同樣的觀點將在此,所以你需要改變你的getView()這樣的代碼被重用..

public View getView(int position, View convertView, ViewGroup parent) { 

    ViewHolder holder; 
    if (convertView == null) { 
     convertView = mInflater.inflate(R.layout.row, null); 


     holder = new ViewHolder(); 
     holder.name = (TextView) convertView.findViewById(R.id.textView1); 
     holder.number = (TextView) convertView.findViewById(R.id.textView2); 
     holder.chk = (CheckBox) convertView.findViewById(R.id.checkBox1); 
     holder.chk.setTag(position); 
     convertView.setTag(R.layout.row,holder); 
    } else { 
     holder = (ViewHolder) convertView.getTag(R.layout.row); 
    } 
    holder.chk.setOnCheckedChangeListener(null); 
    holder.chk.setChecked(mCheckStates.get(position, false)); 
    holder.chk.setOnCheckedChangeListener(this); 

    ProfileBean bean = filteredData.get(position); 
    holder.name.setText(bean.getName()); 
    holder.number.setText(bean.getNumber()); 
    convertView.setTag(bean); 
    return convertView; 
} 
+0

@ kalyan pvs非常感謝 – Himani

+0

@Himani很高興幫助你.. –