title: Android ListView With Toggle Button Web Service
body: <p>The problem is,<code>onCheckedChange()</code> methods get called repeatedly and the phone runs out of memory. When I take the calls to the <code>AsyncTask</code> out of the <code>checkedChange</code> method it works fine. Or if I take the <code>setChecked()</code> calls out it works fine as well. But I have to have those methods in there for this portion of the application to make sense. The <code>WebService</code> calls are super short but have to be toggled from the <code>Toggle</code> button.</p>
  
  <p>I would like you to share some of your thoughts and opinions. </p>
  
  <pre><code>private class ItemAdapter extends ArrayAdapter&lt;FavItem&gt; {
  
          private final ArrayList&lt;FavItem&gt; cats;
          public int count;
  
          // private Context m;
          public ItemAdapter(Context context, int textViewResourceId,
                  ArrayList&lt;FavItem&gt; myItems) {
              super(context, textViewResourceId, myItems);
              this.cats = myItems;
              // this.m = context;
          }
  
          @Override
          public View getView(int position, View convertView, ViewGroup parent) {
              View v = convertView;
  
              if (v == null) {
                  LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                  // LayoutInflater vi = getLayoutInflater();
                  v = vi.inflate(R.layout.favitem, null);
              }
              final FavItem o = cats.get(position);
              if (o != null) {
  
                  TextView title = (TextView) v.findViewById(R.id.textViewNear1);
                  TextView sub = (TextView) v.findViewById(R.id.textViewNear2);
                  final ToggleButton text = (ToggleButton) v
                          .findViewById(R.id.texttoggle);
                  final ToggleButton email = (ToggleButton) v
                          .findViewById(R.id.emailtoggle);
                  text.setChecked(o.getFav_sms() == 1 ? true : false);
                  email.setChecked(o.getFav_email() == 1 ? true : false);
                  title.setText(o.getBusiness_name());
                  sub.setText(o.getDeal_name());
  
                  text.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  
                      @Override
                      public void onCheckedChanged(CompoundButton cText,
                              boolean textCheck) {
                          count++;
                          int uText = text.isChecked() ? 1 : 0;
                          int uEmail = email.isChecked() ? 1 : 2;
                          int id = o.getObject_id();
                          String catURL = "https://example.com/?fn=myfavs&amp;a=add&amp;ot=2&amp;oid="
                                  + id + "&amp;f1=" + uText + "&amp;f2=" + uEmail;
                          UpdateFavs u = new UpdateFavs();
                          u.execute(catURL);
                          log("URL" + catURL);
  
                          log("count: " + count);
                      }
                  });
                  email.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  
                      @Override
                      public void onCheckedChanged(CompoundButton cEmail,
                              boolean emailChecked) {
                          int id = o.getObject_id();
                          count++;
                          int uText = text.isChecked() ? 1 : 0;
                          int uEmail = email.isChecked() ? 1 : 2;
                          String catURL = "https://example.com/?fn=myfavs&amp;a=add&amp;ot=2&amp;oid="
                                  + id + "&amp;f1=" + uText + "&amp;f2=" + uEmail;
                          UpdateFavs u = new UpdateFavs();
                          u.execute(catURL);
                          log("URL" + catURL);
  
                          log("count: " + count);
                      }
  
                  });
  
              }
              return v;
          }
  
      }
  
  private class UpdateFavs extends AsyncTask&lt;String, Void, String&gt; {
          InputStream is;
          String result;
  
          @Override
          protected void onPreExecute() {
              pBar.setVisibility(View.VISIBLE);
          }
  
          @Override
          protected String doInBackground(String... id) {
              try {
  
                  // final String catURL =
                  // "https://example.com/?fn=myfavs&amp;a=add&amp;ot=2&amp;oid="
                  // + "&amp;f1=" + uText + "&amp;f2=" + uEmail;
                  String catURL = id[0];
  
                  DefaultHttpClient httpclient = new DefaultHttpClient();
                  HttpPost httppost = new HttpPost(catURL);
                  ArrayList&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;();
  
                  nameValuePairs.add(new BasicNameValuePair("ID",
                          getCookie()));
  
                  httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                  httpclient.execute(httppost);
  
              } 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();
              }
  
              return result;
          }
  
          @Override
          protected void onPostExecute(String result) {
              pBar.setVisibility(View.GONE);
  
          }
  
      }
  </code></pre>