gzml
gzml

Reputation: 113

Android AsyncTask runs multiple times

In my app ,there is an one button which get input from database.When I press it more than one in a short time it crashes. How can i avoid this error with using asynctask?

show.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        showinf();
    }
});

}



private String[] columns={"name","surname"};
private void showinf(){

    SQLiteDatabase db=v1.getReadableDatabase();
    Cursor c=db.query("infos",columns,null,null, null,null,null);
    Random mn2=new Random();
    int count=c.getCount();
    String mn=String.valueOf(count);
    int i1=mn2.nextInt(count+1);
    c.move(i1);

    t1.setText(c.getString(c.getColumnIndex("name")));
    t2.setText(c.getString(c.getColumnIndex("surname")));




}

thanks...

Upvotes: 1

Views: 196

Answers (3)

Rustam
Rustam

Reputation: 6515

disable the show button in onPreExecute() and enable it back onPostExecute().

 public class getAsyncDataTask extends AsyncTask<Void, Void, Void>
    {

        @Override
        public void onPreExecute()
        {
           show.setAlpha(0.5);
           show.setEnable(false);

        }

        @Override
        public void doInBackground(Void... params)
        {
           //retrieve the data from db;
        }
        @Override
        public void onPostExecute()
        {
           show.setAlpha(1.0);
           show.setEnable(true);


        }
    }

Upvotes: 1

Eng.Fouad
Eng.Fouad

Reputation: 117597

You can create a boolean flag (let's say bDiscardButtonAction), and set it to true in onPreExecute() and set it to false in onPostExecute(), something like:

public class FooTask extends AsyncTask<Foo, Foo, Foo>
{
    private static boolean bDiscardButtonAction = false;
    private boolean isDiscareded = false;

    @Override
    public void onPreExecute()
    {
        if(bDiscardButtonAction)
        {
            isDiscareded = true;
            return;
        }

        bDiscardButtonAction = true;
    }

    @Override
    public Foo doInBackground(Foo... params)
    {
        if(isDiscareded) return;

        // ...
    }

    @Override
    public void onPostExecute(Void result)
    {
        if(!isDiscareded) bDiscardButtonAction = false;
    }

    @Override
    public void onCancelled(Foo result)
    {
        if(!isDiscareded) bDiscardButtonAction = false;
    }
}

Upvotes: 3

Mahesh Yallure
Mahesh Yallure

Reputation: 29

I hope this code will help u out.

         button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            new AsynchTaskManualLocation().execute();

    });



   public class AsynchTaskGetData extends AsyncTask<String,Void,String>
{
    @Override
    protected String doInBackground(String... url) {
        //showinf(); this method contains operation of getting data from         //database    OR ur logic to getdata
        return showinf();

    }

    @Override
    protected void onPostExecute(String result) {
      //here u get result in resul var and process the result here
        }
    }
}

Upvotes: 0

Related Questions