无法从ListView CheckBox中获取复选框文本?

时间:2017-04-03 11:35:53

标签: android listview checkbox

首先,让我告诉我要做的事情......

enter image description here

请参阅截图。当用户选中一个复选框时,我想添加左侧给出的数字并在顶部显示总数。例如:如果用户选择3个名为chairman,auditor和jr的框。官员,它将从复选框左侧添加3位数字。这是1 + 1 + 1 = 3,这个总数将发生在顶部而不是0。

我已经使用listview实现了checkbox.But现在无法添加数字。这些值来自数据库。 让我告诉你我的代码。

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();
                }

            }
        });

    }

}

1 个答案:

答案 0 :(得分:0)

在AdapterClass上尝试使用lv.setOnItemClickListener事件。