samedi 12 mars 2016

how to set the check boxes to be checked in custom adapater android

in my android application i am getting the list of some value from the json and making them as a check box list using custom base adapter. i need to select the check box ,i can select it later if if i want to see which are the option of selected the check boxes, check boxes are not selected properly which i have selected it is checking some random option. i need to make the selection to be as it is until i reopen the particular selection option in my application. please help me my adapter class is as

public class CategoriesAdapter extends BaseAdapter{
    public List<SelectCategoryModel > listData;
    private ArrayList<SelectCategoryModel > arraylist;
    ArrayList<String> selected_id_list=new ArrayList<String>();
    private LayoutInflater layoutInflater;
    private ImageLoader imageLoader;
    private DisplayImageOptions options;
    private Context prova;
     boolean[] checkBoxState;
    String[] sections;
    private boolean state;
    boolean firsttime=false;
    Activity contex_a;
    private int mSelectedPosition = -1;
     private  int selected_position;
    public CategoriesAdapter(Activity contex_a,List<SelectCategoryModel > listData, ArrayList<SelectCategoryModel> result) {
        // TODO Auto-generated constructor stub
        this.listData = listData;
        this.arraylist = new ArrayList<SelectCategoryModel >();
        this.arraylist.addAll(listData);
        imageLoader = ImageLoader.getInstance();
        this.contex_a = contex_a;
        prova=contex_a;
        arraylist=new ArrayList<SelectCategoryModel >();
         checkBoxState=new boolean[listData.size()];


    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return listData.size();
    }

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

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

    @Override
    public View getView( final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final ViewHolder holder;
        Log.i("inside get view",""+ selected_id_list);
        /*if (selected_id_list.isEmpty()) {
        uncheckAllChildrenCascade(parent);

        }*/
        final SelectCategoryModel  connectionsItem = (SelectCategoryModel) listData.get(position);
        LayoutInflater mInflater = (LayoutInflater) contex_a.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        int count = SelectCategoryActivity.myList.size();
        if(count>0)
        {
            for(int i=0;i<count;i++)
            {

                if(SelectCategoryActivity.myList.get(i).trim().equalsIgnoreCase(connectionsItem.getName()))
                {
                    connectionsItem.setSelected(true);
                }
            }
        }
        if(convertView == null){
            convertView = mInflater.inflate(R.layout.new_create_broadcast_add_members_custom, null);
            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.broad_name);
            holder.checkBox=(CheckBox)convertView.findViewById(R.id.broad_checkBox_i);

            convertView.setTag(holder);     

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

        // addind Font 
        Typeface typeFace=Typeface.createFromAsset(prova.getAssets(),"fonts/BentonSans-Regular.otf");
        holder.name.setText(connectionsItem.getName());
        holder.name.setTypeface(typeFace);

        holder.checkBox.setChecked(checkBoxState[position]);
        holder.checkBox.setOnClickListener( new View.OnClickListener() { 
            public void onClick(View v) { 
                CheckBox cb = (CheckBox) v ;
                SelectCategoryModel  connItem = (SelectCategoryModel ) cb.getTag();
                connItem.setSelected(cb.isChecked());
                 if(((CheckBox)v).isChecked()){
                     checkBoxState[position]=true;
                     firsttime=true;
                 }
                    else
                     checkBoxState[position]=false;

                 Log.i("inside on click", checkBoxState.toString());
            } 
        }); 


        holder.checkBox.setChecked(connectionsItem.isSelected());
        holder.checkBox.setTag(connectionsItem);

        //setSelectStateForAll();
        //r

        notifyDataSetChanged();
        return convertView;
    }
    static class ViewHolder{

        TextView name;
        CheckBox checkBox;

    }




Aucun commentaire:

Enregistrer un commentaire