Search code examples
androidcheckboxandroid-custom-viewcustom-adapter

I have two BaseAdapter which contain checkboxes, how to check the checkbox of Adapter2 based on the values from the Adapter1


I am having two BaseAdapter Adapter1 and Adapter2, after checking the checkbox from adapter1 it is saved in the Arraylist and which is passed to Adapter2. I want to check the checkbox of Adapter2 which are selected in the Adapter1. Both Adapter will have the same values.

I have tried but it is giving

java.lang.IndexOutOfBoundsException: Index:

as the passed values from the Adapter1.

can anyone please help in this case, please. here is the two adapter Adapter1 is

public class RouterSpinnerAdapter extends BaseAdapter implements Filterable{
Context context;
LayoutInflater layoutInflater;
ArrayList<RouterSelectModel> routermodeldata;
ArrayList<RouterSelectModel> rowterselectedmodeldata;
ArrayList<RouterSelectModel>orig;
boolean[] rowtercheckBoxState;
boolean flag;
public RouterSpinnerAdapter(Context context, ArrayList<RouterSelectModel>routermodeldata, boolean flag){
    this.context=context;
    this.routermodeldata=routermodeldata;
    this.flag=flag;
    rowtercheckBoxState = new boolean[routermodeldata.size()];
    rowterselectedmodeldata=new ArrayList<>();

}
@Override
public int getCount() {
    return routermodeldata.size();
}

@Override
public Object getItem(int position) {
    return routermodeldata.get(position);
}

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

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder=null;

    if (convertView==null){
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView =layoutInflater.inflate(R.layout.rowter_spinner_row, null);
        viewHolder = new ViewHolder();
        viewHolder.ch_select_rowter =(CheckBox) convertView.findViewById(R.id.ch_select_rowter);
        viewHolder.t_rowtername=(TextView)convertView.findViewById(R.id.t_rowtername);

        convertView.setTag(viewHolder);

    }else{
        viewHolder = (ViewHolder) convertView.getTag();
    }
    final RouterSelectModel model=(RouterSelectModel)routermodeldata.get(position);

    viewHolder.t_rowtername.setText(model.getRoutername());
    viewHolder.ch_select_rowter.setChecked(rowtercheckBoxState[position]);

    viewHolder.ch_select_rowter.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            try {
                if(((CheckBox)v).isChecked()){
                    rowtercheckBoxState[position]=true;
                    // selectedManagerHashMap.put(model.getManagername(),model.getManagername());
                   // saveToCustom(model.getDominaname(),sessionManager.getUserID());
                    rowterselectedmodeldata.add(model);
                }else
                {
                    rowtercheckBoxState[position]=false;
                    // selectedManagerHashMap.remove(model.getManagername());
                    //removeFromCustom(model.getDominaname(),sessionManager.getUserID());
                    rowterselectedmodeldata.remove(model);
                }
            }catch (ArrayIndexOutOfBoundsException e){

            }
        }

    });

    return convertView;
}

@Override
public Filter getFilter() {
    return new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {
            final FilterResults oReturn = new FilterResults();
            final ArrayList<RouterSelectModel> results = new ArrayList<RouterSelectModel>();
            if (orig == null)
                orig = routermodeldata;
            if (charSequence != null) {
                if (orig != null && orig.size() > 0) {
                    for (final RouterSelectModel g : orig) {
                        if (g.getRoutername().toLowerCase().contains(charSequence.toString()))
                            results.add(g);
                    }
                }
                oReturn.values = results;
            }
            return oReturn;
        }

        @Override
        protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
            routermodeldata = (ArrayList<RouterSelectModel>) filterResults.values;
            notifyDataSetChanged();
        }
    };
}
 public void selectAllRowter(boolean flag){
if (flag==true){
    for (int i = 0; i <rowtercheckBoxState.length ; i++) {

        rowtercheckBoxState[i]=flag;
        rowterselectedmodeldata.add(routermodeldata.get(i));
        notifyDataSetChanged();
    }
}if (flag==false){
    for (int i = 0; i <rowtercheckBoxState.length ; i++) {

        rowtercheckBoxState[i]=flag;
        rowterselectedmodeldata.remove(routermodeldata.get(i));
        notifyDataSetChanged();
    }
}


 }
 public ArrayList<RouterSelectModel> getseletedItems(){
    return rowterselectedmodeldata;
 }
static class ViewHolder{

    TextView t_rowtername;
    CheckBox ch_select_rowter;
}

 }

Adapter2 is

     public class Fun2RouterSpinnerAdapter extends BaseAdapter implements Filterable{
Context context;
LayoutInflater layoutInflater;
ArrayList<Fun2RouterSpinnerModel> fun2routermodeldata;
ArrayList<Fun2RouterSpinnerModel> fun2rowterselectedmodeldata;
ArrayList<Fun2RouterSpinnerModel>orig;
boolean[] fun2rowtercheckBoxState;
boolean flag;
ArrayList<RouterSelectModel> routerlistfrom1;
public Fun2RouterSpinnerAdapter(Context context, ArrayList<Fun2RouterSpinnerModel>routermodeldata,ArrayList<RouterSelectModel> routerlistfrom1 ,boolean flag){
    this.context=context;
    this.fun2routermodeldata=routermodeldata;
    this.flag=flag;
    fun2rowtercheckBoxState = new boolean[routermodeldata.size()];
    fun2rowterselectedmodeldata=new ArrayList<>();
    this.routerlistfrom1=routerlistfrom1;

}
@Override
public int getCount() {
    return fun2routermodeldata.size();
}

@Override
public Object getItem(int position) {
    return fun2routermodeldata.get(position);
}

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

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder=null;

    if (convertView==null){
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView =layoutInflater.inflate(R.layout.fun2_rowter_spinner_row, null);
        viewHolder = new ViewHolder();
        viewHolder.ch_select_rowter2 =(CheckBox) convertView.findViewById(R.id.ch_select_rowter2);
        viewHolder.t_rowtername2=(TextView)convertView.findViewById(R.id.t_rowtername2);

        convertView.setTag(viewHolder);

    }else{
        viewHolder = (ViewHolder) convertView.getTag();
    }
    final Fun2RouterSpinnerModel model=(Fun2RouterSpinnerModel)fun2routermodeldata.get(position);

    viewHolder.t_rowtername2.setText(model.getRouter2name());
    viewHolder.ch_select_rowter2.setChecked(fun2rowtercheckBoxState[position]);




    viewHolder.ch_select_rowter2.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            try {
                if(((CheckBox)v).isChecked()){
                    fun2rowtercheckBoxState[position]=true;
                    // selectedManagerHashMap.put(model.getManagername(),model.getManagername());
                    // saveToCustom(model.getDominaname(),sessionManager.getUserID());
                    fun2rowterselectedmodeldata.add(model);
                }else
                {
                    fun2rowtercheckBoxState[position]=false;
                    // selectedManagerHashMap.remove(model.getManagername());
                    //removeFromCustom(model.getDominaname(),sessionManager.getUserID());
                    fun2rowterselectedmodeldata.remove(model);
                }
            }catch (ArrayIndexOutOfBoundsException e){

            }
        }

    });
     checkSelected(true,routernamefrom1))
    return convertView;
}

@Override
public Filter getFilter() {
    return new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {
            final FilterResults oReturn = new FilterResults();
            final ArrayList<Fun2RouterSpinnerModel> results = new ArrayList<Fun2RouterSpinnerModel>();
            if (orig == null)
                orig = fun2routermodeldata;
            if (charSequence != null) {
                if (orig != null && orig.size() > 0) {
                    for (final Fun2RouterSpinnerModel g : orig) {
                        if (g.getRouter2name().toLowerCase().contains(charSequence.toString()))
                            results.add(g);
                    }
                }
                oReturn.values = results;
            }
            return oReturn;
        }

        @Override
        protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
            fun2routermodeldata = (ArrayList<Fun2RouterSpinnerModel>) filterResults.values;
            notifyDataSetChanged();
        }
    };
}
public void fun2selectAllRowter(boolean flag){
    if (flag==true){
        for (int i = 0; i <fun2rowtercheckBoxState.length ; i++) {

            fun2rowtercheckBoxState[i]=flag;
            fun2rowterselectedmodeldata.add(fun2routermodeldata.get(i));
            notifyDataSetChanged();
        }
    }if (flag==false){
        for (int i = 0; i <fun2rowtercheckBoxState.length ; i++) {

            fun2rowtercheckBoxState[i]=flag;
            fun2rowterselectedmodeldata.remove(fun2routermodeldata.get(i));
            notifyDataSetChanged();
        }
    }


}
public ArrayList<Fun2RouterSpinnerModel> getRoterseletedItems(){
    return fun2rowterselectedmodeldata;
}
public boolean checkSelected(boolean flag,String routernamefrom1){

    for (int i = 0; i <fun2routermodeldata.size() ; i++) {
        if(routernamefrom1.equalsIgnoreCase(fun2routermodeldata.get(i).getRouter2name())){
            fun2rowtercheckBoxState[i]=flag;
            return true;
        }
    }

    return false;
}
static class ViewHolder{

    TextView t_rowtername2;
    CheckBox ch_select_rowter2;
}
  }

Solution

  • i got answer by doing some R&D here is the solution.

    public class DRRouterSpinnerAdapter extends BaseAdapter implements Filterable{
    Context context;
    LayoutInflater layoutInflater;
    ArrayList<DRRouterSpinnerModel> drroutermodeldata;
    ArrayList<DRRouterSpinnerModel> drrowterselectedmodeldata;
    ArrayList<DRRouterSpinnerModel>orig;
    boolean[] drrowtercheckBoxState;
    boolean flag;
    ArrayList<TeamRouterSelectModel> routerlistfrom1;
    ArrayList<String>datafrom1;
    
    public DRRouterSpinnerAdapter(Context context, ArrayList<DRRouterSpinnerModel>routermodeldata, ArrayList<TeamRouterSelectModel> routerlistfrom1 , boolean flag, ArrayList<String>datafrom1){
        this.context=context;
        this.drroutermodeldata=routermodeldata;
        this.flag=flag;
        drrowtercheckBoxState = new boolean[routermodeldata.size()];
        drrowterselectedmodeldata=new ArrayList<>();
        this.routerlistfrom1=routerlistfrom1;
        this.datafrom1=datafrom1;
    
    }
    @Override
    public int getCount() {
        return drroutermodeldata.size();
    }
    
    @Override
    public Object getItem(int position) {
        return drroutermodeldata.get(position);
    }
    
    @Override
    public long getItemId(int position) {
        return position;
    }
    
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder=null;
    
        if (convertView==null){
            layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView =layoutInflater.inflate(R.layout.fun2_rowter_spinner_row, null);
            viewHolder = new ViewHolder();
            viewHolder.ch_select_rowter2 =(CheckBox) convertView.findViewById(R.id.ch_select_rowter2);
            viewHolder.t_rowtername2=(TextView)convertView.findViewById(R.id.t_rowtername2);
    
            convertView.setTag(viewHolder);
    
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final DRRouterSpinnerModel model=(DRRouterSpinnerModel)drroutermodeldata.get(position);
    
        viewHolder.t_rowtername2.setText(model.getRouter2name());
        viewHolder.ch_select_rowter2.setChecked(drrowtercheckBoxState[position]);
    
        checkSelected(model,position);
    
        viewHolder.ch_select_rowter2.setOnClickListener(new View.OnClickListener() {
    
            public void onClick(View v) {
                try {
                    if(((CheckBox)v).isChecked()){
                        drrowtercheckBoxState[position]=true;
                        // selectedManagerHashMap.put(model.getManagername(),model.getManagername());
                        // saveToCustom(model.getDominaname(),sessionManager.getUserID());
                        drrowterselectedmodeldata.add(model);
    
    
                    }else
                    {
                        drrowtercheckBoxState[position]=false;
                        // selectedManagerHashMap.remove(model.getManagername());
                        //removeFromCustom(model.getDominaname(),sessionManager.getUserID());
                        drrowterselectedmodeldata.remove(model);
    
    
    
    
                    }
                }catch (ArrayIndexOutOfBoundsException e){
    
                }
            }
    
        });
    
        return convertView;
    }
    
    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence charSequence) {
                final FilterResults oReturn = new FilterResults();
                final ArrayList<DRRouterSpinnerModel> results = new ArrayList<DRRouterSpinnerModel>();
                if (orig == null)
                    orig = drroutermodeldata;
                if (charSequence != null) {
                    if (orig != null && orig.size() > 0) {
                        for (final DRRouterSpinnerModel g : orig) {
                            if (g.getRouter2name().toLowerCase().contains(charSequence.toString()))
                                results.add(g);
                        }
                    }
                    oReturn.values = results;
                }
                return oReturn;
            }
    
            @Override
            protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
                drroutermodeldata = (ArrayList<DRRouterSpinnerModel>) filterResults.values;
                notifyDataSetChanged();
            }
        };
    }
    public void fun2selectAllRowter(boolean flag){
        if (flag==true){
            for (int i = 0; i <drrowtercheckBoxState.length ; i++) {
    
                drrowtercheckBoxState[i]=flag;
                drrowterselectedmodeldata.add(drroutermodeldata.get(i));
                notifyDataSetChanged();
            }
        }if (flag==false){
            for (int i = 0; i <drrowtercheckBoxState.length ; i++) {
    
                drrowtercheckBoxState[i]=flag;
                drrowterselectedmodeldata.remove(drroutermodeldata.get(i));
                notifyDataSetChanged();
            }
        }
    
    
    }
    public ArrayList<DRRouterSpinnerModel> getRoterseletedItems(){
       /* HashSet<DRRouterSpinnerModel> hashSet_fun2rowterselectedmodeldata = new HashSet<DRRouterSpinnerModel>();
        hashSet_fun2rowterselectedmodeldata.addAll(fun2rowterselectedmodeldata);
        fun2rowterselectedmodeldata.clear();
        fun2rowterselectedmodeldata.addAll(hashSet_fun2rowterselectedmodeldata);*/
    
        return drrowterselectedmodeldata;
    }
    public void checkSelected(DRRouterSpinnerModel model,int position){
    
        for (int i = 0; i <datafrom1.size() ; i++) {
    
            if (datafrom1.get(i).equalsIgnoreCase(model.getRouter2name())){
                drrowtercheckBoxState[position]=true;
                drrowterselectedmodeldata.add(model);
    
            }
        }
    
    }
    
    static class ViewHolder{
    
        TextView t_rowtername2;
        CheckBox ch_select_rowter2;
    }