問題是,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);
}
}