lundi 29 février 2016

check box checked values getting only last value in android

When I toast value of assignbranch getting multiple elements in arraylist and checkbox is checked for last element only.

values in assignbranch are [a,b,c]. When toasting assignbranch why am I getting

[a,b,c]
[a,b,c,a,b,c]
[a,b,c,a,b,c,a,b,c]   

Code

public class BranchUserAdapter extends ArrayAdapter<BranchModel> {
    public ArrayList<BranchModel> EmployeesList;
    boolean[] itemChecked;
    LayoutInflater vi;
    int Resource;
    ViewHolder holder;
    private Context mContext;
    ArrayList<String>  assignbranch=new ArrayList<String>();;
    ArrayList<String>  branch=new ArrayList<String>();

    public BranchUserAdapter(Context context, int resource,    ArrayList<BranchModel> objects) {
        super(context, resource, objects);
        vi = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        Resource = resource;
        EmployeesList = objects;
        this.mContext = context;

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // convert view = design
        View v = convertView;

        if (v == null) {
            holder = new ViewHolder();
            v = vi.inflate(Resource, null);
            // new DownloadImageTask(holder.imageview).execute(actorList.get(position).getImage());
            holder.branchcheck = (CheckBox) v.findViewById(R.id.checkbox);
            holder.branch = (TextView) v.findViewById(R.id.branchuser);
            holder.branchcheck.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    CheckBox cb = (CheckBox) v;
                    BranchModel planet = (BranchModel) cb.getTag();
                    planet.setSelected(cb.isChecked());
                }
            });
            v.setTag(holder);
            holder.branchcheck.setTag(EmployeesList.get(position));
    } else {
        holder = (ViewHolder) v.getTag();
        ((ViewHolder) v.getTag()).branchcheck.setTag(EmployeesList.get(position));
        holder.branchcheck.setOnCheckedChangeListener(null);
    }
    BranchModel Employees = EmployeesList.get(position);
    final String sample=EmployeesList.get(position).getName();
    SessionManager session;
    session = new SessionManager(getContext());
    session.checkLogin();
    HashMap<String, String> user = session.getUserDetails();
    final String id = user.get(SessionManager.KEY_EMAIL);
    String tag_json_obj = "json_obj_req";
    String url = "http://ift.tt/1QmAgZD?"+id;
    StringRequest req = new StringRequest(Request.Method.GET,url,
    new Response.Listener<String>() {
        // final JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
        //"http://ift.tt/1LOXbYr", new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONObject obj = new JSONObject(response);
                    JSONArray empjson = obj.getJSONArray("DDL");
                    for (int i = 0; i < empjson.length(); i++) {
                        //Getting json object
                        JSONObject json = empjson.getJSONObject(i);
                        //Adding the name of the student to array list
                        assignbranch.add(json.getString("name"));
                        if (sample.contains(assignbranch.get(i))){
                            holder.branchcheck.setChecked(true);
                        }*/
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                for(int i=0;i<assignbranch.size();i++){
                    for(int j=0;j<EmployeesList.size();j++){
                         if(assignbranch.get(i).equals(EmployeesList.get(j).getName())){
                        //that means object in second response exists in first response
                        holder.branchcheck.setChecked(true);
                    }
                }
            }
            Toast.makeText(getContext(), "hello"+assignbranch.toString(), Toast.LENGTH_SHORT).show();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.getMessage();
            // hide the progress dialog
        }
    }) {

    };
    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(req, tag_json_obj);
    //   assignedBranch();

    holder.branch.setText(EmployeesList.get(position).getName());
    return v;
}
static class ViewHolder {
    public CheckBox branchcheck;
    public TextView branch;
}




Aucun commentaire:

Enregistrer un commentaire