vendredi 3 juin 2016

How to remove CheckBox

I have two fields industry and skills. One industry have multiple skills. I have dynamically added checkboxes for industry. If I select a CheckBox for industry, its particular skills will load from server and will display. Skills are shown as CheckBox. The problem is when I deselect the industry checkbox its particular skills should also be removed. Here is my code for getting industry from server

    private class NetCheckIndustry extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        bar.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

        bar.setVisibility(View.GONE);

        LinearLayout featuresTable = (LinearLayout) findViewById(R.id.linearLayoutCheckbox);
        final List selchkboxlist = new ArrayList();
        for (int k = 0; k < catal.size(); k++) {
            final CheckBox chkindustry1 = new CheckBox(MakePaymentsActivity.this);
            chkindustry1.setText("" + catal.get(k).toString());
            featuresTable.addView(chkindustry1);

            chkindustry1.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    String chk = null;
                    chk = Integer.toString(v.getId());
                    //String gfg = catal.get(v.getId());
                    //Log.e("gdfggg",""+gfg);
                    if (((CheckBox) v).isChecked()) {
                        selchkboxlist.add(chkindustry1.getText().toString());
                        //selchkboxlist.add(chk);
                        Log.e("selchkboxlist", "" + selchkboxlist.toString());
                        checkbox_id = spinnerValuesMap.get(chkindustry1.getText().toString());
                        str_cat_id = spinnerValuesMap.get(chkindustry1.getText().toString());
                        Log.e("str_cat_id", "" + str_cat_id);
                        new NetCheckSkills().execute();
                        // Log.e("cityiddddd", "" + checkbox_id);
                        //  catIdCheckbox.add(checkbox_id);
                        // Log.e("catIdCheckbox", "" + catIdCheckbox.toString());


                    } else {
                        selchkboxlist.remove(chkindustry1.getText().toString());
                        catIdCheckbox.remove(chk);

                        Log.e("remove selchkboxlist", "" + selchkboxlist.toString());
                        Log.e("catIdCheckbox removeeee", "" + catIdCheckbox.toString());

                    }
                }
            });

        }

        return;
    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub

            HttpClient httpClient = new DefaultHttpClient();

            HttpPost httpRequest = new HttpPost(
                    url);

            //Log.e("URL", ""+url);
            httpRequest.setHeader("Content-Type", "application/json");

            JSONObject json = new JSONObject();

            Log.e("JSON Object", json.toString());

            StringEntity se = new StringEntity(json.toString());
            se.setContentEncoding("UTF-8");
            se.setContentType("application/json");
            httpRequest.setEntity(se);
            HttpResponse httpRes = httpClient.execute(httpRequest);
            java.io.InputStream inputStream = httpRes.getEntity()
                    .getContent();
            InputStreamReader inputStreamReader = new InputStreamReader(
                    inputStream);
            BufferedReader reader = new BufferedReader(inputStreamReader);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            inputStream.close();
            strServerResponse = sb.toString();
            Log.e("Server Response", "" + strServerResponse.toString());
            if (strServerResponse != null) {

                JSONObject arr = new JSONObject(strServerResponse);
                // for (int i = 0; i < arr.length(); i++) {

                //     JSONObject jobj2 = arr.getJSONObject(i);

                objCategory = arr.getString("category");
                spinnerValuesMap = new HashMap<String, String>();
                JSONArray ff = new JSONArray(objCategory);
                ArrayList<String> tii = new ArrayList<String>();
                ArrayList<String> tii1 = new ArrayList<String>();
                ArrayList<String> tii2 = new ArrayList<String>();
                catal = new ArrayList<String>();
                ArrayList<String> catalid = new ArrayList<String>();
                for (int j = 0; j < ff.length(); j++) {
                    // pojo = new CityStateCategoryPojo();
                    JSONObject jobj3 = ff.getJSONObject(j);
                    String catId = jobj3.optString("id");
                    String imagename = jobj3.optString("image");
                    String catname = jobj3.optString("name");
                    catal.add(catname);
                    spinnerValuesMap.put(catname, catId);

                }

                List<String> CatTem = catal;
                List<String> CatTemId = catalid;
                Set<String> cattem = new HashSet<String>(CatTem);
                Set<String> cattemid = new HashSet<String>(CatTemId);
                SharedPreferences.Editor cateditr = getSharedPreferences("Categoryy", MODE_PRIVATE).edit();
                cattem.addAll(CatTem);
                cattemid.addAll(CatTemId);
                cateditr.putStringSet("catname", cattem);
                cateditr.putStringSet("catid", cattemid);
                cateditr.commit();

            } else {
                Handler handler = new Handler(Looper.getMainLooper());
                handler.post(
                        new Runnable() {
                            @Override
                            public void run() {
                                AlertDialog alertDialog = new AlertDialog.Builder(
                                        MakePaymentsActivity.this).create();
                                alertDialog.setMessage("Error Connecting to Server");
                                alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                    }
                                });

                                alertDialog.show();
                            }
                        });
                 //Log.e("ServiceHandler", "Couldn't get any data from the url");

            }

        } catch (UnsupportedEncodingException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } catch (ClientProtocolException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } catch (IOException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();
            //    }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;

    }

}

And this is the code for getting skills

private class NetCheckSkills extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();

        bar.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

        bar.setVisibility(View.GONE);

        LinearLayout featuresTable = (LinearLayout) findViewById(R.id.linearLayoutSkills);
        final List selchkboxlist = new ArrayList();
        Log.e("catall.todyting", "" + catal1.toString());
        for (int k = 0; k < catal1.size(); k++) {
            final CheckBox feature1 = new CheckBox(MakePaymentsActivity.this);
            feature1.setText("" + catal1.get(k).toString());
            featuresTable.addView(feature1);
            //  feature1.setTag(chkindustry.getId(), str_cat_id);
            feature1.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    String chk = null;
                    chk = Integer.toString(v.getId());
                    //String gfg = catal.get(v.getId());
                    //Log.e("gdfggg",""+gfg);
                    if (((CheckBox) v).isChecked()) {
                        selchkboxlist.add(feature1.getText().toString());
                        //selchkboxlist.add(chk);
                        Log.e("selchkboxlist", "" + selchkboxlist.toString());
                        chk_skills = spinnerValuesMap1.get(feature1.getText().toString());
                        // Log.e("cityiddddd", "" + checkbox_id);
                        //  catIdCheckbox.add(checkbox_id);
                        // Log.e("catIdCheckbox", "" + catIdCheckbox.toString());


                    } else {
                        selchkboxlist.remove(feature1.getText().toString());
                        catIdCheckbox.remove(chk);

                        Log.e("remove selchkboxlist", "" + selchkboxlist.toString());
                        Log.e("catIdCheckbox removeeee", "" + catIdCheckbox.toString());

                    }
                }
            });

        }

        return;
    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub


            HttpClient httpClient = new DefaultHttpClient();

            HttpPost httpRequest = new HttpPost(
                    url);

            //Log.e("URL", ""+url);
            httpRequest.setHeader("Content-Type", "application/json");

            JSONObject json = new JSONObject();

            Log.e("JSON Object", json.toString());

            StringEntity se = new StringEntity(json.toString());
            se.setContentEncoding("UTF-8");
            se.setContentType("application/json");
            httpRequest.setEntity(se);
            HttpResponse httpRes = httpClient.execute(httpRequest);
            java.io.InputStream inputStream = httpRes.getEntity()
                    .getContent();
            InputStreamReader inputStreamReader = new InputStreamReader(
                    inputStream);
            BufferedReader reader = new BufferedReader(inputStreamReader);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            inputStream.close();
            strServerResponse1 = sb.toString();
            Log.e("Server Response", "" + strServerResponse1.toString());
            if (strServerResponse1 != null) {

                JSONArray arr1 = new JSONArray(strServerResponse1);

                catal1 = new ArrayList<String>();
                spinnerValuesMap1 = new HashMap<String, String>();
                for (int i = 0; i < arr1.length(); i++) {
                    JSONObject jobj = arr1.getJSONObject(i);


                    String catId = jobj.optString("id");
                    String catname = jobj.optString("name");
                    catal1.add(catname);
                    spinnerValuesMap1.put(catname, catId);
                }

                ArrayList<String> tii = new ArrayList<String>();
                ArrayList<String> tii1 = new ArrayList<String>();
                ArrayList<String> tii2 = new ArrayList<String>();

                ArrayList<String> catalid = new ArrayList<String>();


                List<String> CatTem = catal1;
                List<String> CatTemId = catalid;
                Set<String> cattem = new HashSet<String>(CatTem);
                Set<String> cattemid = new HashSet<String>(CatTemId);
                SharedPreferences.Editor cateditr = getSharedPreferences("Categoryy1", MODE_PRIVATE).edit();
                cattem.addAll(CatTem);
                cattemid.addAll(CatTemId);
                cateditr.putStringSet("catname1", cattem);
                cateditr.putStringSet("catid1", cattemid);
                cateditr.commit();

            } else {

                Handler handler = new Handler(Looper.getMainLooper());
                handler.post(
                        new Runnable() {
                            @Override
                            public void run() {
                                AlertDialog alertDialog = new AlertDialog.Builder(
                                        MakePaymentsActivity.this).create();
                                alertDialog.setMessage("Error Connecting to Server");
                                alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                    }
                                });

                                alertDialog.show();
                            }
                        });


            }

        } catch (UnsupportedEncodingException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } catch (ClientProtocolException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } catch (IOException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();
            //    }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;

    }

}

Please help. How to remove these checkboxes.




Aucun commentaire:

Enregistrer un commentaire