开发者

problem setting progress bar before alert dialog which has a spinner

public class classified  extends Activity
{
    private ArrayAdapter<String> aaagency ;
        String strdata="";
        String strerrormess="";
        public void onCreate(Bundle savedInstanceState) 
    {
        setTitle("Classified Ad. Booking");
                super.onCreate(savedInstanceState);
                this.setContentView(R.layout.classified);
        }
        public void srcAgency(View view) throws IOException
    {   
        Log.i("Classified Ad","srcAgency");
        new srcAgency().execute();
        srcAgency srcagen = new srcAgency();
        strdata = srcagen.strtempdata;

        Log.i("AgencyData2", strdata);
        Log.i("AgencyData3", strerrmess);
        if(strerrmess.equals(""))
        {
            strarr= fun1.split(strdata, "^");
            aaagency = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item , strarr); 
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Agency");
            builder.setAdapter(aaagency, new DialogInterface.OnClickListener() 
            {
                public void onClick(DialogInterface dialog, int item) 
             开发者_StackOverflow   {
                    txtAgency.setText(strarr[item]);
                }
            });
            builder.show();
        }
    }

        class srcAgency extends AsyncTask<Void, String, Void> 
    {
        String strtempdata="";
        ProgressDialog dialog;
        @Override
        protected void onPreExecute() 
        {
            strerrmess="";
            super.onPreExecute();
            dialog = ProgressDialog.show(classified.this, "", "Please wait...", true);
            dialog.show();
        }

        @Override
        protected Void doInBackground(Void... unused) 
        {
            try
            {
                stragency = txtAgency.getText().toString().trim();
                intagencyBrac1= stragency.lastIndexOf('(');
                intagencyBrac2= stragency.lastIndexOf(')');

                if (stragency.length() < 3)
                {strerrmess="Please Enter atleast 3 Characters for Agency Searching";}
                else if(intagencyBrac1>=0||intagencyBrac2>=0)
                {strerrmess="Please Enter Characters for Agency Searching";}
                else
                {
                    if(stragency.indexOf(' ')!=-1)
                    {stragency = stragency.replace(' ', '!');}


                    Log.i("AgencyUrl",strurl);
                    strdata = "Client1^Client2^Client3^Client4^Client5^Client6^Client1";
                    Log.i("AgencyData",strdata);
                    strtempdata = strdata;
                    if(!strdata.equals(""))
                    {

                    }
                    else
                    {strerrmess ="No Data Available";}
                }
            }
            catch(Exception e)
            {

            }
            return null;

        }

        @Override
        protected void onPostExecute(Void unused) 
        {
            dialog.dismiss();
            if (strerrmess.equals("Please Enter atleast 3 Characters for Agency Searching"))
            {Toast(strerrmess);intflag=1;}
            else if(strerrmess.equals("Please Enter Characters for Agency Searching"))
            {Toast(strerrmess);intflag=1;}
            else if(strerrmess.equals("Your Session Got Expired. Please login again."))
            {
                Intent intent = new Intent(classified.this, loginscreen.class);
                startActivity(intent);
                Toast(strerrmess);
                intflag=1;
            }
            else
            {intflag=0;}
        }
    }
}

I am unable to get the value of strdata which i have initialized in asynctask function in the srcagency function. What should I do? Even though strdata is a global variable.

I have also tried this but I think you can't initialize array adapter in onpostexecute function...

@Override
        protected void onPostExecute(Void unused) 
        {
            dialog.dismiss();
            if (strerrmess.equals("Please Enter atleast 3 Characters for Agency Searching"))
            {Toast(strerrmess);intflag=1;}
            else if(strerrmess.equals("Please Enter Characters for Agency Searching"))
            {Toast(strerrmess);intflag=1;}
            else if(strerrmess.equals("Your Session Got Expired. Please login again."))
            {
                Intent intent = new Intent(classified.this, loginscreen.class);
                startActivity(intent);
                Toast(strerrmess);
                intflag=1;
            }
            else
            {strarr= fun1.split(strdata, "^");
            aaagency = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item , strarr); 
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Agency");
            builder.setAdapter(aaagency, new DialogInterface.OnClickListener() 
            {
                public void onClick(DialogInterface dialog, int item) 
                {
                    txtAgency.setText(strarr[item]);
                }
            });
            builder.show();}
        }

Any help or comments would be appreciated.

Thanks


 Log.i("Classified Ad","srcAgency");
 new srcAgency().execute();
 srcAgency srcagen = new srcAgency();
 strdata = srcagen.strtempdata;

This does not work. You are saying, start an AsyncTask that will set your strdata at some point in the future but also immediately return and after creating a new AsyncTask have it know what the last AsyncTask did.

Try this:

void srcAgency(View v){ 
//We only want to start the AsyncTask here, nothing else.
// Whatever you did before and whatever triggered the srcAgency(View) method
srcAgency srcagen = new srcAgency();
srcagen.execute();
return;
}



public void realSrcAgency(View v) {
... // The rest of original srcAgency(View)
}

// Inside of asyncTask srcAgency ... 
public void postExecute() {
   // Call the new method we just had, but after our asyncTask is done.
  realSrcAgency(null);
}

Basically you can't expect all these things to happen simultaneously. It would be easy to help you if you trimmed down the specifics of your code. It looks like you just want a button or some click to start an async task that fills a strings. However after that string is filled do something else with it. Also I don't believe you need an async task for any of this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜