在对话框中的单击事件的线程内显示Toast消息

时间:2013-12-09 09:28:06

标签: android multithreading dialog

我正在开发一个应用程序,用于显示从Web服务到列表视图的数据。实际上根据我的应用程序:当用户从列表视图中单击某个项目时,应该询问带有对话框的消息。当用户按下"是"发生了一个Web服务调用过程。所以..我想显示一个进度对话框,直到完成Web服务调用过程(用于调用Web服务的方法返回一个字符串message.i想要在此进度对话框被解除后显示该消息)。

我正在我的适配器类的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;



}

我试图发出祝酒消息,知道天气是否有效。进展对话显示,但在我等待的时间之后,它就被解雇而没有显示我的祝酒消息。

请某人帮帮我

我尝试过使用assync任务

这是我的编码..我没有做任何复杂的事情..只是想C它工作与否..但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();

    }





}

6 个答案:

答案 0 :(得分:0)

您无法在单个主题中显示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);

答案 1 :(得分:0)

您无法在任何线程内执行与UI相关的任何工作。为此,您必须使用Hanlder或只使用AsyncTask来调用web-servicee并在AsyncTask的onPostExecute()中显示Toast。

答案 2 :(得分:0)

您可以使用runOnUiThread()例如

this.runOnUiThread(show_toast);

和show_toast

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

即可

答案 3 :(得分:0)

使用RunOnUiThread显示来自其他线程的吐司

runOnUiThread(new Runnable() {
    public void run() {
        //Toast
    }
});

答案 4 :(得分:0)

使用处理程序类显示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();

答案 5 :(得分: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();

}