lundi 3 avril 2017

Can not get checkbox text from ListView CheckBox?

Firstly,let me tell what I am trying to do...

http://ift.tt/2n3OOqG

Please,see the screenshot.When user select a checkbox,I want to add the digit given in the left and show the total on top.example: if user select 3 boxes named chairman,auditor and jr. officer,it will add 3 digits from the left of checkbox. that is 1+1+1=3 and this total will take place on top instead of 0.

I have implemented using listview with checkbox.But now can't add the digits.This values are coming from database. Let me show you my code.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_group_sms);

    // Sets the Toolbar to act as the ActionBar for this Activity window.
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // Remove default title text
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    // Get access to the custom title view
    TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);

    contactList = new ArrayList<>();

    lv = (ListView) findViewById(R.id.list);


    new GetContacts().execute();
    openInfo();




}

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

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        pDialog = new ProgressDialog(GroupSms.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        HttpHandler sh = new HttpHandler();

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url);

        Log.e(TAG, "Response from url: " + jsonStr);

        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                // Getting JSON Array node
                JSONArray contacts = jsonObj.getJSONArray("all_group");

                // looping through All Contacts
                for (int i = 0; i < contacts.length(); i++) {
                    JSONObject c = contacts.getJSONObject(i);

                    gid = c.getString("group_id");
                    groupName = c.getString("group_name");
                    total = c.getString("total");



                    // tmp hash map for single contact
                    HashMap<String, String> contact = new HashMap<>();

                    contact.put("group_id", gid);
                    contact.put("group_name", groupName);
                    contact.put("total", total);
                    //contact.put("mobile", mobile);

                    // adding contact to contact list
                    contactList.add(contact);

                    Log.d("Contactlist", String.valueOf(contactList));

                }
            } catch (final JSONException e) {
                Log.e(TAG, "Json parsing error: " + e.getMessage());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),
                                "Json parsing error: " + e.getMessage(),
                                Toast.LENGTH_LONG)
                                .show();
                    }
                });

            }
        } else {
            Log.e(TAG, "Couldn't get json from server.");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(),
                            "Couldn't get json from server. Check LogCat for possible errors!",
                            Toast.LENGTH_LONG)
                            .show();
                }
            });

        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        if (pDialog.isShowing())
            pDialog.dismiss();
        /* *
         * Updating parsed JSON data into ListView
         * */
        ListAdapter adapter = new SimpleAdapter(
                GroupSms.this,
                contactList,
                R.layout.groups,
                new String[]{
                        "group_name","total"},
                new int[]{R.id.check,R.id.groupsNo});


        lv.setAdapter(adapter);



        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                CheckBox cb = (CheckBox) view.findViewById(R.id.check);

                cb.setChecked(!cb.isChecked());
                if(cb.isChecked())
                {
                    //TextView cTotal = (TextView) findViewById(R.id.idTotal);
                    Toast.makeText(GroupSms.this,cb.getText(),Toast.LENGTH_SHORT).show();
                }

            }
        });

    }

}




Aucun commentaire:

Enregistrer un commentaire