我正在開發一個應用程序,它正在連接到一個web服務並在列表視圖中顯示數據。我可以成功顯示數據[我已經使用自定義列表視圖並顯示我使用自定義數據由Base Adapter擴展的適配器類]。現在我正在嘗試連接到另一個Web服務,當一個接受按鈕被點擊時[在列表視圖中]。我開發了從服務獲得字符串響應的方法,這些方法運行良好[我已通過我的應用測試過]。異步任務錯誤Android
我的問題是,我想在連接到服務時顯示一些進度對話框,並使用Toast消息顯示響應。我已經使用AsyncTask
類。首先,我剛剛運行一個線程並讓它睡了一段時間。這與我的AsyncTask
完美配合。但是,當我打電話[中doInBackground()
]使用它的網絡服務,我得到了一個錯誤:
Activity com.jsonlist.jsonlist.ViewList has leaked window [email protected] that was originally added here android.view.WindowLeaked: Activity com.jsonlist.jsonlist.ViewList has leaked window [email protected] that was originally added here at android.view.ViewRootImpl.(ViewRootImpl.java:374) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:292)
我的代碼如下:
這個方法是我的適配器類的內部
public void dialogshow(final String Date,final String Start,final String End){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity);
alertDialogBuilder.setTitle("Confirm your Action!");
// set dialog message
alertDialogBuilder
.setMessage("Click yes Confirm!!")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
//Toast.makeText(mContext, "Yes clicked", Toast.LENGTH_LONG).show();
new ShowResponceForAccept().execute();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
alertDialogBuilder.show();
}
時我從對話框中按下「是」按鈕。它應該打電話給我的AsyncTask
。
這裏是我的AsyncTask
public class ShowResponceForAccept extends AsyncTask<String, String, String>{
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pDialog = ProgressDialog.show(activity, "Sending Request", "getting Responce.", true, false);
}
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
// ShortList sendandget = new ShortList();
// String resp = sendandget.getResponceFromServer(Appid, null, Uid);
ShortList sendandget = new ShortList();
String resp = sendandget.getResponceFromServer(Appid,"",Uid);
//Thread.sleep(10000);
//String x="Your Request Accepted";
return resp;
//return x;
}
@Override
protected void onPostExecute(String resp) {
// TODO Auto-generated method stub
pDialog.dismiss();
String x = resp;
Toast.makeText(mContext, x, Toast.LENGTH_LONG).show();
}
}
這也是在同一個適配器類。
任何人都可以幫助我...?
編輯
這裏我完全適配器類
public class NewsRowAdapter extends BaseAdapter {
static Dialog dialogs;
private static final String STIME = "StartTime";
private static final String END = "EndTime";
private static final String DATE = "Date";
private Context mContext;
private Activity activity;
private static LayoutInflater inflater=null;
private ArrayList<HashMap<String, String>> data;
int resource;
public ImageLoader imageLoader;
String Uid = MainActivity.confirm;
String getDate;
String getStartTime;
String getEndTime;
String Appid;
//String response;
//Context context;
//Initialize adapter
public NewsRowAdapter(Context ctx,Activity act, int resource,ArrayList<HashMap<String, String>> d) {
super();
this.resource=resource;
this.data = d;
this.activity = act;
this.mContext = ctx;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public void showFirstDialog(final ArrayList<HashMap<String, String>> list){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity);
alertDialogBuilder.setTitle("Confirm your Action!");
// set dialog message
alertDialogBuilder
.setMessage("You Have Similar Kind of Appoinments!! Do you wanna Show them ?")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
//Toast.makeText(mContext, "Showing", Toast.LENGTH_LONG).show();
dialogpop(list);
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
alertDialogBuilder.show();
}
public void dialogshow(final String Date,final String Start,final String End){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity);
alertDialogBuilder.setTitle("Confirm your Action!");
// set dialog message
alertDialogBuilder
.setMessage("Click yes Confirm!!")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
//Toast.makeText(mContext, "Yes clicked", Toast.LENGTH_LONG).show();
new ShowResponceForAccept().execute();
dialog.dismiss();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
alertDialogBuilder.show();
}
public void dialogshowForReject(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity);
alertDialogBuilder.setTitle("Confirm your Action!");
// set dialog message
alertDialogBuilder
.setMessage("Click yes Confirm!!")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
//Toast.makeText(mContext, "Yes clicked", Toast.LENGTH_LONG).show();
new ShowResponceForReject().execute();
// loadListAgain();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
alertDialogBuilder.show();
}
public void showDuplicateDialog(ArrayList<HashMap<String, String>> list){
//CharSequence[] cs = list.toArray(new CharSequence[list.size()]);
AlertDialog.Builder alertDialogBuilder2 = new AlertDialog.Builder(activity);
LayoutInflater infl = activity.getLayoutInflater();
View view = infl.inflate(R.layout.dialog_list, null);
ListView lv = (ListView) view.findViewById(R.id.dialogList);
//NewsRowAdapter nw = new NewsRowAdapter(mContext, activity, R.layout.dialog_row, list);
SimpleAdapter sim = new SimpleAdapter(mContext, list, R.layout.dialog_row, new String[] { STIME,END, DATE }, new int[] {
R.id.stime2,R.id.etime2, R.id.blank2});
lv.setAdapter(sim);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(mContext, "item clicked ", Toast.LENGTH_LONG).show();
}
});
/*ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.two_line_list_item, android.R.id.text1, Names);*/
alertDialogBuilder2.setView(view)
/*alertDialogBuilder2.setAdapter(sim, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(mContext, "item clicked ", Toast.LENGTH_LONG).show();
}
})
*/
.setPositiveButton("Accept", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(mContext, "Accepted", Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
alertDialogBuilder2.show();
}
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
View vi = convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.row,null);
final TextView firstname = (TextView) vi.findViewById(R.id.fname);
final TextView lastname = (TextView) vi.findViewById(R.id.lname);
final TextView startTime = (TextView) vi.findViewById(R.id.stime);
final TextView endTime = (TextView) vi.findViewById(R.id.etime);
final TextView date = (TextView) vi.findViewById(R.id.blank);
final TextView hidID = (TextView) vi.findViewById(R.id.hidenID);
final TextView hidAppid = (TextView) vi.findViewById(R.id.HidAppoinmentID);
final ImageView img = (ImageView) vi.findViewById(R.id.list_image);
HashMap<String, String> song = new HashMap<String, String>();
song =data.get(position);
firstname.setText(song.get(MainActivity.TAG_PROP_FNAME));
lastname.setText(song.get(MainActivity.TAG_PROP_LNAME));
startTime.setText(song.get(MainActivity.TAG_STIME));
endTime.setText(song.get(MainActivity.TAG_ETIME));
date.setText(song.get(MainActivity.TAG_DATE));
hidID.setText(song.get(MainActivity.TAG_HID));
hidAppid.setText(song.get(MainActivity.TAG_HIDApp));
String theUrl = song.get(MainActivity.TAG_IMG);
if(theUrl.equalsIgnoreCase("null")){
Bitmap bImage = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.propic);
img.setImageBitmap(bImage);
//ViewList v = new ViewList();
//v.handleImage(theUrl, img);
}
else{
/// replace these codes
Profile pc = new Profile();
Bitmap map =pc.downloadImage(theUrl);
img.setImageBitmap(map);
// imageLoader.DisplayImage(theUrl, img);
}
Button accept = (Button) vi.findViewById(R.id.btnAccepted);
accept.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final int x = (int) getItemId(position);
/*Intent zoom=new Intent(mContext, Profile.class);
zoom.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
zoom.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(zoom);*/
// get the intent from the hashmap check if there is similar date and time.
//then store them in a list or array.
getDate = (String) date.getText();
getStartTime = startTime.getText().toString();
getEndTime = endTime.getText().toString();
Appid = hidAppid.getText().toString();
ShortList sh = new ShortList();
ArrayList<HashMap<String, String>> duplicateList;
duplicateList=sh.getDuplicated(getDate, getStartTime, getEndTime);
if(duplicateList.size()>1){
//dialogshow(getDate,getStartTime,getEndTime);
showFirstDialog(duplicateList);
}
else{
dialogshow(getDate, getStartTime, getEndTime);
}
}
});
Button reject = (Button) vi.findViewById(R.id.btnReject);
reject.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// get User ID and get request ID
//then call the method getResponceFromServerForReject()
dialogshowForReject();
//MainActivity ma = new MainActivity();
//
//ma.new JSONParse().execute();
}
});
vi.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String getPname = hidID.getText().toString();
Toast.makeText(parent.getContext(), "view clicked: "+getPname , Toast.LENGTH_SHORT).show();
//get the id of the view
//check the id of the request
//call the web service acording to the id
Intent zoom=new Intent(parent.getContext(), Profile.class);
zoom.putExtra("PatientID", getPname);
parent.getContext().startActivity(zoom);
}
});
return vi;
}
public void dialogpop(ArrayList<HashMap<String, String>> list){
dialogs = new Dialog(activity);
dialogs.setContentView(R.layout.dialog_list);
dialogs.setTitle("Select One");
ListView listView = (ListView) dialogs.findViewById(R.id.dialogList);
//SimpleAdapter sim = new SimpleAdapter(mContext, list, R.layout.dialog_row, new String[] { STIME,END, DATE }, new int[] {
// R.id.stime2,R.id.etime2, R.id.blank2});
Adapter_For_Dialog nw = new Adapter_For_Dialog(mContext,activity, R.layout.dialog_row, list);
listView.setAdapter(nw);
dialogs.show();
}
public class ShowResponceForAccept extends AsyncTask<String, String, String>{
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pDialog = ProgressDialog.show(activity, "Sending Request", "getting Responce.", true, false);
}
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
// ShortList sendandget = new ShortList();
// String resp = sendandget.getResponceFromServer(Appid, null, Uid);
ShortList sendandget = new ShortList();
String resp = sendandget.getResponceFromServer(Appid,"",Uid);
//Thread.sleep(10000);
//String x="Your Request Accepted";
return resp;
//return x;
}
@Override
protected void onPostExecute(String resp) {
// TODO Auto-generated method stub
pDialog.dismiss();
String x = resp;
Toast.makeText(mContext, x, Toast.LENGTH_LONG).show();
}
}
public class ShowResponceForReject extends AsyncTask<String, Void, String>{
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
/*pDialog = new ProgressDialog(activity);
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
*/
pDialog = ProgressDialog.show(activity, "Sending Request", "getting Responce.", true, false);
}
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
// ShortList sendandget = new ShortList();
// String resp = sendandget.getResponceFromServer(Appid, null, Uid);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String x="Successfully Rejected";
// return resp;
return x;
}
@Override
protected void onPostExecute(String x) {
// TODO Auto-generated method stub
pDialog.dismiss();
/*如果(resp.equalsIgnoreCase( 「 - 1」)){ Toast.makeText(mContext, 「接受」,Toast.LENGTH_LONG )。顯示();
//REFRESH THE LIST
}
else{
Toast.makeText(mContext, "Rejected", Toast.LENGTH_LONG).show();
}*/
Toast.makeText(mContext, x, Toast.LENGTH_LONG).show();
}
}
public void loadListAgain(){
ArrayList<HashMap<String, String>> oslist = null;
String TAG_DATA = "d";
String TAG_OBJ_1 = "Appointment";
String TAG_OBJ_2 = "PatientProfile";
String TAG_PROP_FNAME = "FirstName";
String TAG_PROP_LNAME = "LastName";
String TAG_STIME = "StartTime";
String TAG_ETIME = "EndTime";
String TAG_DATE = "Date";
String TAG_HID = "PatientPersonId";
String TAG_HIDApp = "AppointmentId";
String TAG_IMG = "ImageURL";
JsonParser jp = new JsonParser();
String caregiverID = MainActivity.confirm.toString();
JSONObject param = new JSONObject();
JSONObject job = new JSONObject();
try {
param.put("caregiverPersonId", caregiverID);
job = jp.getJSONFromUrl(param, MainActivity.url);
JSONArray androids = job.getJSONArray(TAG_DATA);
//JSONObject appt= new JSONObject(json.getString("d"));
for(int i = 0; i < androids.length(); i++){
JSONObject c = androids.getJSONObject(i);
// Storing JSON item in a Variable
JSONObject jobs = c.getJSONObject(TAG_OBJ_2);
String url = jobs.getString("ImageURL");
String firstName = jobs.getString(TAG_PROP_FNAME);
String lastName = jobs.getString(TAG_PROP_LNAME);
JSONObject job_two = c.getJSONObject(TAG_OBJ_1).getJSONObject("DayTimeSlot");
String start = job_two.getString(TAG_STIME);
String end = job_two.getString(TAG_ETIME);
String Ldate = job_two.getString(TAG_DATE);
String ackwardRipOff = Ldate.replace("/Date(", "").replace(")/", "");
Long Ldat = Long.valueOf(ackwardRipOff);
Date date = new Date(Ldat);
String strdate = (String) DateFormat.format("MM/dd/yy", date);
JSONObject toHid = c.getJSONObject(TAG_OBJ_1);
String Hid = toHid.getString(TAG_HID);
JSONObject job_three = c.getJSONObject(TAG_OBJ_1);
String AppoinmentID = job_three.getString(TAG_HIDApp);
// Adding value HashMap key => value
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_PROP_FNAME, firstName);
map.put(TAG_PROP_LNAME, lastName);
map.put(TAG_STIME , start);
map.put(TAG_ETIME, end);
map.put(TAG_DATE, strdate);
map.put(TAG_HID, Hid);
map.put("ImageURL", url);
map.put("AppointmentId", AppoinmentID);
oslist.add(map);
//duplicate = new JSONObject();
//reult.putStringArrayListExtra("map", oslist);
//startActivity(reult);
/*reult.putExtra("arraylist", oslist);
startActivityForResult(reult, 500);*/
Intent reult = new Intent(activity,ViewList.class);
reult.putExtra("arraylist", oslist);
activity.startActivityForResult(reult, 500);
}
}
catch(JSONException c){
c.printStackTrace();
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int possision) {
// TODO Auto-generated method stub
return possision;
}
@Override
public long getItemId(int possision) {
// TODO Auto-generated method stub
return possision;
}
}
請有人幫助我.... –