1

I have a main activity with a list view, an onItemClickListener() event is associated to the list view and when I touch an element I start an AsyncTask that does some work. I would like to disable the other touches that the user could make to the other elements of the list during the async task execution and re-enable them when the async task ends. How could I do?

This is my activity

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{

    private ListView newList;
    private ArrayAdapter<String> newArrayAdapter;

    private MyAsyncTask asyncTask;

    private ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        progressBar = findViewById(R.id.progressbar);
        newArrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
        newList = findViewById(R.id.pairedDevicesListView);
        newList.setOnItemClickListener(this);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        newList.setOnItemClickListener(null); //need something like this
        asyncTask = new MyAsyncTask(this);
        asyncTask.execute();

    }

    @Override
    public void onBackPressed() {
        asyncTask.cancel(true);
        super.onBackPressed();

    }


    private class MyAsyncTask extends AsyncTask<Void,Void,Void>{
        Context context; 
        public MyAsyncTask(Context context){
            this.context = context.getApplicationContext();
        }

        @Override
        protected Void doInBackground(Void... voids) {
            // do some stuff
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            progressBar.setVisibility(View.GONE);
            newList.setOnItemClickListener();
            //does not work or I don't know what to pass
        }
    }
}
JayJona
  • 469
  • 1
  • 16
  • 41
  • 1
    try android:splitMotionEvents="false" attribute of your ListView also check https://stackoverflow.com/questions/12777435/disable-multi-finger-touch-in-my-app/15641505 – Developer Jul 09 '19 at 10:08
  • Start loading the progress wheel in `onPreExecute()` and stop loading the Progress wheel in `onPostExecute()`. When starting the progress use `progressbar.setCancelable(false)` to disable user interaction while ProgressBar is visible. – Gowtham Subramaniam Jul 09 '19 at 10:11

2 Answers2

0

You could write something like this in your onItemClickListener:

newList.setEnabled(false);

And then in your onPostExecute method write:

newList.setEnabled(true);
prashant17
  • 1,520
  • 3
  • 14
  • 23
0

Try this:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    if (asyncTask == null || asyncTask.getStatus() == AsyncTask.Status.FINISHED) {
        asyncTask = new MyAsyncTask(this);
        asyncTask.execute();
    }
}

Be careful with onBackPressed, or you may call a function on a null object.

@Override
public void onBackPressed() {
    if (asyncTask != null) {
        asyncTask.cancel(true);
    }
    super.onBackPressed();
}
binhgreat
  • 982
  • 8
  • 13