简体   繁体   中英

Showing ProgressDialog OnItemClick

I've a ListView with a list of item retrieving with JSON and i want to show a ProgressDialog on item click.

listNews.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View v, int position,
                    long arg3) {
                ProgressDialog dialog;
                dialog = ProgressDialog.show(v.getContext(), "Please wait..", "Loading data", true);
                dialog.setCancelable(false);
                dialog.show();
                News n = (News)listNews.getItemAtPosition(position);

                Intent myIntent = new Intent(v.getContext(), SingleActivity.class);
                myIntent.putExtra("actu_id", n.ID);
                dialog.dismiss();
                startActivityForResult(myIntent, 0);


            }

        });

ProgressDialog not showing... Same problem when I define this progressDialog in OnCreate in SingleActivity.class

Can you help me ?

Sorry for my english ...

我认为这是因为您立即将其撤消

It's a problem related with your workflow. Since you're doing additional tasks on the UI thread, the ProgressDialog doesn't get to be shown.

You should either spawn a thread for doing the extra work, then dismiss your dialog and launch your new Intent with runOnUiThread() or with a Handler.

Although, the best option would be using AsyncTask, showing your ProgressDialog onPreExecute(), doing your work in doInBackground() and then dismissing it onPostExecute() and launching your new activity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM