2017-04-03 38 views
0

首先,讓我告訴你們,我試圖做...無法從ListView CheckBox中獲取複選框文本?

enter image description here

請參見screenshot.When用戶選擇一個複選框,我想補充左側給出的數字,顯示例如:如果用戶選擇名爲董事長,審計員和Jr的3個框。官員,它會在複選框左側添加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(); 
       } 

      } 
     }); 

    } 

} 

回答

0

嘗試對AdapterClass上的lv.setOnItemClickListener事件。

+0

setOnItemClickListener的代碼沒有執行,我不明白是什麼原因。 – Avijit

+0

因爲此時未創建列表,所以onclick事件無法設置。所以你可以使用後嘗試趕(SetAdapter) – ITSGuru

+0

對不起。我沒有明白你的觀點。請告訴你使用什麼! – Avijit