im正在开发一个应用程序,用于将从web服务获得的数据显示到列表视图。实际上,根据我的应用程序:当用户从列表视图中单击项目时,应该会询问它一些带有对话框的消息。当用户按下"是"时,会发生一个web服务调用过程。所以..我想显示一个进度对话框,直到web服务调用过程完成(用于调用web服务的方法返回一条字符串消息。我想在这个进度对话框被取消后显示该消息)。
我正在适配器类的getview方法中编写这些代码。
这是我的代码
public View getView(final int position, View convertView, final ViewGroup parent) {
// TODO Auto-generated method stub
View vi = convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.dialog_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.stime2);
final TextView endTime = (TextView) vi.findViewById(R.id.etime2);
final TextView date = (TextView) vi.findViewById(R.id.blank2);
final TextView uid = (TextView) vi.findViewById(R.id.lname2);
final TextView appid = (TextView) vi.findViewById(R.id.hidenappoinment);
final ImageView img = (ImageView) vi.findViewById(R.id.list_image2);
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));
uid.setText(song.get(ShortList.TAG_UID));
appid.setText(song.get(ShortList.TAG_APOINMENTID));
//imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), img);
vi.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//String getFname = firstname.getText().toString();
Toast.makeText(parent.getContext(), "view clicked: " , Toast.LENGTH_SHORT).show();
//get the id of the view
//check the id of the request
//call the web service acording to the id
AlertDialog.Builder alertbuild = new AlertDialog.Builder(activity);
alertbuild.setMessage("Everything else will be Considerd as Rejected! Are You Sure ?");
alertbuild.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(final DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//Toast.makeText(mContext, "Yes ", Toast.LENGTH_LONG).show();
final String getStime = startTime.getText().toString();
final String getEtime = endTime.getText().toString();
final String getDate = date.getText().toString();
final String getUid = uid.getText().toString();
final String getapid = appid.getText().toString();
final ProgressDialog ringProgressDialog = ProgressDialog.show(activity, "Please wait ...", "Request is in Progress ...", true);
ringProgressDialog.setCancelable(false);
new Thread(new Runnable() {
@Override
public void run() {
try {
// Here you should write your time consuming task...
// Let the progress ring for 10 seconds...
// ShortList sendandget = new ShortList();
// String resp = sendandget.sendDataAndGetResponce(getDate, getStime, getEtime,getUid,getapid);
// if(resp.equalsIgnoreCase("true")){
// }
Thread.sleep(10000);
} catch (Exception e) {
e.printStackTrace();
}
ringProgressDialog.dismiss();
Toast.makeText(mContext, "Your Request Accepted", Toast.LENGTH_LONG).show();
dialog.dismiss();
}
}).start();
// ArrayList<HashMap<String, String>> rfejected = getRejected.getRejectedList(getDate, getStime, getEtime,getUid,getapid);
//ArrayList<HashMap<String, String>> accept = getRejected.getAccept(getDate, getStime, getEtime,getUid,getapid);
// sendaceptedAndReject(accept,getUid,rfejected,getapid);
}
})
.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(mContext, "No ", Toast.LENGTH_LONG).show();
}
});
alertbuild.show();
}
});
return vi;
}
我试着放一条吐司信息,以了解它是否工作。正在显示进度对话框,但在我等待的时间之后,它就被驳回了,没有显示我的吐司信息。
请有人帮我
我试过使用ASSINC任务
这是我的编码。。我没有做任何复杂的事情。。只是想让它工作还是不工作。。但是OnPreExecute()没有显示progressDialog:(
public class ShowResponce 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();
}
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
//ShortList sendandget = new ShortList();
//String resp = sendandget.sendDataAndGetResponce(getDate, getStime, getEtime,getUid,getapid);
String x="a";
return x;
}
@Override
protected void onPostExecute(String x) {
// TODO Auto-generated method stub
pDialog.dismiss();
Toast.makeText(mContext, x, Toast.LENGTH_LONG).show();
}
}
您不能在任何线程中执行任何与UI相关的工作。为此,您必须使用Hanlder或仅使用AsyncTask来调用web服务,并在AsyncTask的onPostExecute()中显示Toast。
您可以使用runOnUiThread()例如
this.runOnUiThread(show_tast);
并且在show_tast 中
private Runnable show_toast = new Runnable()
{
public void run()
{
Toast.makeText(Autoamtion.this, "My Toast message", Toast.LENGTH_SHORT)
.show();
}
};
只需点击~Toast from a Non-UI thread
使用RunOnUiThread
显示来自另一个线程的toast
runOnUiThread(new Runnable() {
public void run() {
//Toast
}
});
不能在单个线程中显示Toast
消息,因为它没有链接到UI线程。
工作完成后,您必须使用Handler
概念或runOnUiThread()
来显示Toast
消息。
检查以下runOnUiThread()
和Handler类的代码,它们可能会对您有所帮助。
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(mContext,"your message",Toast.LENGTH_LONG).show();
}
});
以下是使用Handler的替代方法。
Handler h = new Handler({
public void handleMessage(Message m) {
Toast.makeText(mContext,"your message",Toast.LENGTH_LONG).show();
}
});
然后,当您想要显示消息时,调用下面的语句。
h.sendEmptyMessage(0);
跟随http://developer.android.com/guide/components/processes-and-threads.html
喜欢这个吗
public void onClick(View v) {
new Thread(new Runnable() {
public void run() {
Bitmap b = loadImageFromNetwork("http://example.com/image.png");
mImageView.setImageBitmap(b);
}
}).start();
}
并更改为
public void onClick(View v) {
new Thread(new Runnable() {
public void run() {
final Bitmap bitmap = loadImageFromNetwork("http://example.com/image.png");
mImageView.post(new Runnable() {
public void run() {
mImageView.setImageBitmap(bitmap);
Toast.makeText(mContext,"your message",Toast.LENGTH_LONG).show();
}
});
}
}).start();
}
使用处理程序类显示Toast消息。
dialog = ProgressDialog.show(mContext, "Your Application", "Processing...",
true);
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
dialog.dismiss();
Toast.makeText(mContext, "Your Request Accepted", Toast.LENGTH_LONG).show();
dialog.dismiss();
}
callService = new Thread() {
public void run() {
try {
result = CallWebserice(address);
handler.sendEmptyMessage(0);
} catch (Exception e) {
}
}
};
callService.start();