samedi 20 juin 2015

How to implement custom list view with check box nad edit box search?

//setting the texwatcher on search editbox
searchView = (EditText)findViewById(R.id.search);
                

                // Capture Text in EditText
                searchView.addTextChangedListener(new TextWatcher() {

                        @Override
                        public void afterTextChanged(Editable arg0) {
                                // TODO Auto-generated method stub
                                String text = searchView.getText().toString().toLowerCase(Locale.getDefault());
                                if(EditGroupContacts.edit_contacts == false){
                                        c_adapter.filter(text);
                                }
                                else{
                                        test_adapter.filter(text);      
                                }
                        }

                        @Override
                        public void beforeTextChanged(CharSequence arg0, int arg1,
                                        int arg2, int arg3) {
                                // TODO Auto-generated method stub
                        }

                        @Override
                        public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                                        int arg3) {
                                // TODO Auto-generated method stub
                        }
                });
//Adapter class

public class TestAdapter extends BaseAdapter
{


        // Declare variables
        static SparseBooleanArray mCheckStates = new SparseBooleanArray();
        static SparseBooleanArray duplicate_mCheckStates = new SparseBooleanArray();
        HashSet<String> selectid_hash_list = new HashSet<String>();
        static Bitmap bitmap;

        ViewHolder holder=null;
        String foldername;
        static ArrayList<String> items_list = new ArrayList<String>();
        ArrayList<String> number = new ArrayList<String>();

        static ArrayList<String> duplicate_items_list = new ArrayList<String>();
        ArrayList<String> duplicate_number_list = new ArrayList<String>();

        ArrayList<String> grp_name = new ArrayList<String>();
        ArrayList<String> grp_number = new ArrayList<String>();

        private LayoutInflater inflater = null;

        public TestAdapter(ArrayList<String> items,ArrayList<String> numbers) 
        {
                items_list = items;
                number = numbers;

                this.duplicate_items_list = new ArrayList<String>();
                this.duplicate_items_list.addAll(items_list);

                this.duplicate_number_list = new ArrayList<String>();
                this.duplicate_number_list.addAll(number);


                mCheckStates=new SparseBooleanArray(items_list.size());

                System.out.println("testAdapter");
        }

        public TestAdapter(ArrayList<String> names,ArrayList<String> numbers,ArrayList<String> grp_names,ArrayList<String> grp_numbers,SparseBooleanArray mCheckStates1) 
        {
                items_list = names;
                number = numbers;

                this.duplicate_items_list = new ArrayList<String>();
                this.duplicate_items_list.addAll(items_list);

                this.duplicate_number_list = new ArrayList<String>();
                this.duplicate_number_list.addAll(number);

                grp_number =grp_numbers;
                grp_name = grp_names;
                System.out.println("testAdapter");
                mCheckStates=new SparseBooleanArray(items_list.size());
                mCheckStates=mCheckStates1;
                System.out.println("mCheckStates"+mCheckStates);

                this.duplicate_mCheckStates = new SparseBooleanArray(items_list.size());

                for(int i=0;i<mCheckStates.size();i++){
                        this.duplicate_mCheckStates.put(mCheckStates.keyAt(i),mCheckStates.get(mCheckStates.keyAt(i)));
                }
                System.out.println("duplicate_mCheckStates"+duplicate_mCheckStates);
        }       

        public void setInflater(LayoutInflater inflater, Activity activity) {
                this.inflater = inflater;
        }

        public int getCount() {
                return items_list.size();

        }

        public Object getItem(int position) 
        {
                return position;
        }

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

        @SuppressLint("InflateParams")
        public View getView( final int position, View convertView, ViewGroup parent) 
        {

                if (convertView == null)
                {
                        convertView = inflater.inflate(R.layout.testitems,null);        
                        holder =new ViewHolder();
                        holder.name = (TextView) convertView.findViewById(R.id.name_tv);
                        holder.number = (TextView) convertView.findViewById(R.id.number_tv);
                        holder.check=(CheckBox) convertView.findViewById(R.id.chk_image);
                        convertView.setTag(holder);
                }

                else
                {
                        holder=(ViewHolder) convertView.getTag();
                }        


                holder.name.setText(items_list.get(position));
                holder.number.setText(number.get(position));

                holder.check.setId(position);
                holder.name.setId(position);
                holder.number.setId(position);

                holder.check.setTag(position);
                holder.number.setTag(position);
                holder.check.setTag(position);


                holder.check.setChecked(mCheckStates.get(position, false));
                holder.check.setOnCheckedChangeListener(null);


                holder.check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                                // TODO Auto-generated method stub
                                System.out.println("mcheck-1");
                                if(buttonView.isChecked()==true){
                                        System.out.println("mcheck-2");

                                        try {
                                                //add images to share functionality

                                                String shareimagepath = items_list.get(buttonView.getId());
                                                String no = number.get(buttonView.getId());

                                                HomeScreen.group_contact_name_list.add(shareimagepath);
                                                HomeScreen.group_contact_number_list.add(no);

                                                selectid_hash_list = new HashSet<String>();
                                                selectid_hash_list.addAll(HomeScreen.group_contact_name_list);
                                                HomeScreen.group_contact_name_list.clear();
                                                HomeScreen.group_contact_name_list.addAll(selectid_hash_list);
                                                
                                        
                                                
                                                mCheckStates.put((Integer) buttonView.getTag(), true);
                                        }
                                        catch (Exception e) {
                                        }
                                }

                                
                                else{

                                        try {

                                                //remove path from deleteitem_list when checkbox is unchecked after checked
                                                String delete_image_path = items_list.get(buttonView.getId());
                                                String no = number.get(buttonView.getId());

                                                HomeScreen.group_contact_name_list.remove(delete_image_path);
                                                HomeScreen.group_contact_number_list.remove(no);

                                                System.out.println(HomeScreen.group_contact_name_list);
                                                //System.out.println(HomeScreen.group_contact_number_list);

                                                mCheckStates.put((Integer) buttonView.getTag(), false);

                                        } catch (Exception e) {
                                        }

                                }

                                System.out.println("size-1"+items_list.size());
                                System.out.println("size-2"+HomeScreen.group_contact_name_list.size());

                                if(EditGroupContacts.edit_contacts==true){
                                        EditGroupContacts.selected_number.setText("Selected: "+HomeScreen.group_contact_name_list.size());
                                }

                        }
                });
                return convertView;
        }


        public boolean isChecked(int position) 
        {
                //System.out.println("ischecked button");
                return mCheckStates.get(position, false);

        }

        public void setChecked(int position, boolean isChecked) 
        {
                mCheckStates.put(position, isChecked);
                //System.out.println("setchecked button ");
        }

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

        //functionality of on check or not the checkbox




        public class ViewHolder
        {
                TextView name,number;
                CheckBox check;
        }


        

        // Filter Class
        public void filter(String charText) {
                charText = charText.toLowerCase(Locale.getDefault());
                items_list.clear();
                number.clear();
                mCheckStates.clear();
                if (charText.length() == 0) {
                        items_list.addAll(duplicate_items_list);
                        number.addAll(duplicate_number_list);

                } 
                else 
                {

                        System.out.println("duplicate_mCheckStates-"+duplicate_mCheckStates);
                        System.out.println("number_list-"+duplicate_number_list);
                        for (int i=0;i<duplicate_items_list.size();i++) 
                        {
                                if (duplicate_items_list.get(i).toLowerCase(Locale.getDefault()).contains(charText)||duplicate_number_list.get(i).toLowerCase(Locale.getDefault()).contains(charText)) {
                                        items_list.add(duplicate_items_list.get(i));
                                        number.add(duplicate_number_list.get(i));
                                        //mCheckStates.put(i, duplicate_mCheckStates.get(i));
                                        //System.out.println("duplicate_mCheckStates"+);
                                }
                        }


                notifyDataSetChanged();
        }
test_adapter = new TestAdapter(name_list,number_list,group_contact_name_list, group_contact_number_list,mcheckstatus);
                test_adapter.notifyDataSetChanged();

                test_adapter.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE), this);
                listView.setAdapter(test_adapter);



Aucun commentaire:

Enregistrer un commentaire