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
}
}
}