2011-11-23 67 views
3

問題是,onCheckedChange()方法被重複調用,手機的內存不足。當我從checkedChange方法中撥打電話AsyncTask時,它工作正常。或者,如果我將setChecked()調出,它也可以正常工作。但是我必須在那裏爲這部分應用程序提供這些方法纔有意義。 WebService通話超短,但必須從Toggle按鈕切換。安卓ListView切換按鈕Web服務

我想請你分享一下你的想法和意見。所有的

private class ItemAdapter extends ArrayAdapter<FavItem> { 

     private final ArrayList<FavItem> cats; 
     public int count; 

     // private Context m; 
     public ItemAdapter(Context context, int textViewResourceId, 
       ArrayList<FavItem> 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&a=add&ot=2&oid=" 
           + id + "&f1=" + uText + "&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&a=add&ot=2&oid=" 
           + id + "&f1=" + uText + "&f2=" + uEmail; 
         UpdateFavs u = new UpdateFavs(); 
         u.execute(catURL); 
         log("URL" + catURL); 

         log("count: " + count); 
        } 

       }); 

      } 
      return v; 
     } 

    } 

private class UpdateFavs extends AsyncTask<String, Void, String> { 
     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&a=add&ot=2&oid=" 
       // + "&f1=" + uText + "&f2=" + uEmail; 
       String catURL = id[0]; 

       DefaultHttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost(catURL); 
       ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 

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

     } 

    } 

回答

2

首先讓你的ItemAdapter類實現OnCheckedChangeListener。在您的代碼中,每次滾動列表時都會實例化一個新的偵聽器。另外,由於視圖設置了監聽器,因此每次滾動時都會調用監聽器的onCheckedChanged()方法。我建議試試這個:

private class ItemAdapter extends ArrayAdapter<FavItem> implements OnCheckedChangeListener {  
private final ArrayList<FavItem> cats;   
public int count;  
// private Context m; 

私人持有人持有人;公共ItemAdapter(上下文上下文,int textViewResourceId,ArrayList myItems){超級上下文,textViewResourceId,myItems};}}}}}}}}}}}}}}}} this.cats = myItems; // this.m = context; } @覆蓋 公共查看getView(INT位置,查看convertView,ViewGroup以及母公司){

  if (convertView == null) { 
      LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      // LayoutInflater vi = getLayoutInflater(); 
      v = vi.inflate(R.layout.favitem, null); 
holder=new Holder(); 
holder.title= (TextView) v.findViewById(R.id.textViewNear1); 
holder.text= (ToggleButton) v.findViewById(R.id.texttoggle); 
holder.sub= (TextView) v.findViewById(R.id.textViewNear2); 
holder.email= (ToggleButton) v .findViewById(R.id.emailtoggle); 
convertView.setTag(holder) 
     }else{ 
holder=convertView.getTag(); 
     } 
     final FavItem o = cats.get(position); 
     if (o != null) { 
holder.text.setTag(position); 
holder.email.setTag(position); 
       //first remove any listener already present 
      holder. text..setOnCheckedChangeListener(null); 
      holder. email.setOnCheckedChangeListener(null); 
//you can now set checked state. we already removed the listener, so it will not trigger the checkd change call 
       holder. text.setChecked(o.getFav_sms() == 1 ? true : false); 
      holder.email.setChecked(o.getFav_email() == 1 ? true : false); 
      holder.title.setText(o.getBusiness_name()); 
      holder.sub.setText(o.getDeal_name()); 
//set the listener to the views now. (so that check events will only be triggerde manually by user and not by scrolling the lsit 
holder. text.setOnCheckedChangeListener(this); 
holder.email.setOnCheckedChangeListener(this); 
return convertView; 
} 

class Holder{ 
    TextView title ,sub ; 
    ToggleButton text,email; 
    } 

@Override 
    public void onCheckedChanged(CompoundButton b,boolean emailChecked) { 
int position=(Integer)b.getTag(); //now you know position of button in list. 
switch(b.getId(){ 
    case R.id.texttoggle: //toggle button 
     //do whatever you want 
     break; 
    case R.id.emailtoggle: 
     //do whatever you want 
     break; 
} 

    } 

設置檢查就能達到目的的項目之前,每次刪除監聽器。注意使用持證人類。這減少了getView()方法調用findViewById()的次數。希望這可以幫助