user1234555
user1234555

Reputation: 209

how do i disable onTouch()?

I am trying to disable onTouch() when my LoadPreview().execute is being launched because if the user taps on the button which triggles my LoadPreview(), there will be duplicate preview buttons. So I want to disable onTouch() during the loading process.

is there a way?

This is my ontouch method:

@Override
                public boolean onTouch(View arg0, MotionEvent arg1) {
                    // TODO Auto-generated method stub

                    switch(arg1.getAction())
                    {
                    case MotionEvent.ACTION_DOWN:
                        adapter.clear();
                        new LoadPreview().execute();
                    break;
                    } 

                    return true;
                }   

             }
                );

             }

this is my loadpreview()

/**
     * Background Async Task to Load all product by making HTTP Request
     * */
    class LoadPreview extends AsyncTask<String, String, String> {

        /**
         * getting preview url and then load them
         * */
        protected String doInBackground(String... args) {


            // Building Parameters
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                // getting JSON string from URL
                JSONObject json = jParser.makeHttpRequest(url_magazine, "GET", params);

                // Check your log cat for JSON reponse
                Log.d("All Products: ", json.toString());

                try {
                    // Checking for SUCCESS TAG
                    int success = json.getInt(TAG_SUCCESS);

                    if (success == 1) {
                        // products found
                        // Getting Array of Products
                        mag = json.getJSONArray(TAG_MAGAZINE);

                        for (int i = 0; i < mag.length(); i++) {
                            JSONObject c = mag.getJSONObject(i);

                            // Storing each json item in variable
                            String magazinePreview = c.getString(TAG_MAGAZINE_PREVIEW);

                            previewList.add(magazinePreview);
                        }                   
                    } 
                    else {

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            for (int a = 0; a <= previewList.size(); a++)
            {           
                if(pos == a)
                {
            // Building Parameters
            List<NameValuePair> param = new ArrayList<NameValuePair>();
            // getting JSON string from URL
            JSONObject json1 = jParser.makeHttpRequest(previewList.get(a), "GET", param);

            // CHECKING OF JSON RESPONSE
            Log.d("All guide: ", json.toString());

            try {
                preview = json1.getJSONArray(TAG_PREVIEWS);

                for (int i = 0; i < preview.length(); i++) {
                    JSONObject c = preview.getJSONObject(i);

                    String image = c.getString(TAG_IMAGE);

                    previewImagesList.add(image);
                    //System.out.println(guideList);
                }   

                // STOP THE LOOP
                break;

            } catch (JSONException e) {
                e.printStackTrace();
            }


            }

            }


            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {

        /**
         *  Updating parsed JSON data into ListView
        * */            
            adapter.notifyDataSetChanged();

        }
    }

onItemClick method:

coverFlow.setOnItemClickListener(new OnItemClickListener()
    {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {

            adapter.clear();
            File sdCard1 = Environment.getExternalStorageDirectory();
            File dir1 = new File (sdCard1.getAbsolutePath() + "/Futsing/issue"+issueNumber+"/"); 

                    /** IF FILE EXISTS **/
                    if(dir1.exists())
                    {
                        Intent intent = new Intent();
                        intent.setClass(CoverFlowExample.this, Reader.class);
                        intent.putExtra("issue", issueNumber);
                        startActivityForResult(intent, GET_INTENT_CODE);
                    }
                    else
                    {
                        if(process==false)
                        {
                            new LoadPreview().execute();
                            process = true;
                        }

                        else
                        {
                            process = false;
                        }
                    }
        }

    }

);

Upvotes: 1

Views: 1832

Answers (3)

Shreyash Mahajan
Shreyash Mahajan

Reputation: 23596

do as Gabe said. You can also do the thing like here i have explained. 1. define one boolean like:

private boolean process = false;
  1. Override the method like onPreExecution in the AsyncTask.
  2. Put boolean on the above method an define its value to true.

    process = true;

  3. Now, onPostExecution method put this:

    process = false

  4. And also implement your onTouch method like below:

    @Override
    public boolean onTouch(View arg0, MotionEvent arg1) {
    // TODO Auto-generated method stub
    
    if(process==false){
    
        switch(arg1.getAction())
        {
            case MotionEvent.ACTION_DOWN:
               adapter.clear();
               new LoadPreview().execute();
               break;
        } 
    
        return true;
        }   
        else{
          return false;
    }
    
    }                
    

    );

** FOR onItenClick **

You can use below code for onItemClick to work as you want.

@Override
    public void onItemClick(AdapterView<?> arg0, View arg1,
            int arg2, long arg3) {

       if(process==false)
       {

        adapter.clear();
        File sdCard1 = Environment.getExternalStorageDirectory();
        File dir1 = new File (sdCard1.getAbsolutePath() + "/Futsing/issue"+issueNumber+"/"); 

                /** IF FILE EXISTS **/
                if(dir1.exists())
                {
                    Intent intent = new Intent();
                    intent.setClass(CoverFlowExample.this, Reader.class);
                    intent.putExtra("issue", issueNumber);
                    startActivityForResult(intent, GET_INTENT_CODE);
                }
                else
                {

                        new LoadPreview().execute();


                }
          }else{
                // nothing to do here it means the process is running
          }
    }

}

Upvotes: 1

Siddharth Lele
Siddharth Lele

Reputation: 27748

Use a boolean flag to check if data is loading to prevent duplicate calls.

Declare a global boolean, for example: boolean loadingData;

Then, in your onTouch() method, set the loadingPreview = true;

And finally, in the onPostExecute() method of your AsyncTask loadingPreview, toggle the loadingPreview to loadingPreview = false;

This will ensure no duplicate class will be made to fetch data while it is still doing so. And toggling the state will ensure the app will be ready again.

EDIT: Don't forget to check the state in the onTouch() method before calling the AyncTask loadingPreview

if (loadingPreview == false) {
    new LoadPreview().execute();
}

Upvotes: 0

Gabe Sechan
Gabe Sechan

Reputation: 93569

Use a flag. Put a boolean allowTouch in your class. Default it to true. When you want to stop touches set it to false. When you're ready to take touches again, set it to true. Change your onTouch method so that it only runs the code in there now if allowTouch == true.

Upvotes: 0

Related Questions