jandroid jandroid - 7 months ago 47
Java Question

How to disable onclickListener on listview item while doing pull to refresh on listview in Android?

I have implemented SwipeRefreshLayout, so i want to disable user interaction while layout is refreshing. I have implemented OnClickListener for ListView item. I am getting following error


java.lang.RuntimeException: Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead
at android.widget.AdapterView.setOnClickListener(AdapterView.java:791)
at this line listView.setOnClickListener(null); in onRefresh() method.


How to solve this issue.

Here is my swipeRefreshLayout in onCreate()

swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
swipeRefreshLayout.setColorSchemeColors(Color.GRAY, Color.GREEN, Color.BLUE, Color.RED, Color.CYAN);
swipeRefreshLayout.setDistanceToTriggerSync(20);// in dips
swipeRefreshLayout.setSize(SwipeRefreshLayout.DEFAULT);// LARGE also can be used

swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
//Refreshing data on server
listView.setOnClickListener(null);
GetDataTask asyncTask2 = new GetDataTask();
asyncTask2.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
});


Here is my AsyncTask whichis call in onRefresh() method

private class GetDataTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();

}
@Override
protected Void doInBackground(Void... arg0)
{
currentPage = 1;
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
GetAllActivityList(currentPage);
Loaddata();
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);

if (swipeRefreshLayout.isRefreshing())
{
swipeRefreshLayout.setRefreshing(false);
populateList(OFFSET);
}
}
}


Here is Error Log information.

java.lang.RuntimeException: Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead
at android.widget.AdapterView.setOnClickListener(AdapterView.java:791)
at com.example.tazeen.classnkk.CustomActionActivity$7.onRefresh(CustomActionActivity.java:563)
at android.support.v4.widget.SwipeRefreshLayout$1.onAnimationEnd(SwipeRefreshLayout.java:168)
at android.support.v4.widget.CircleImageView.onAnimationEnd(CircleImageView.java:107)
at android.view.ViewGroup.finishAnimatingView(ViewGroup.java:6104)
at android.view.View.draw(View.java:16122)
at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
at android.view.View.draw(View.java:16181)
at android.view.View.updateDisplayListIfDirty(View.java:15174)
at android.view.View.draw(View.java:15948)
at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
at android.view.View.updateDisplayListIfDirty(View.java:15169)
at android.view.View.draw(View.java:15948)
at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
at android.view.View.updateDisplayListIfDirty(View.java:15169)
at android.view.View.draw(View.java:15948)
at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
at android.view.View.updateDisplayListIfDirty(View.java:15169)
at android.view.View.draw(View.java:15948)
at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
at android.view.View.updateDisplayListIfDirty(View.java:15169)
at android.view.View.draw(View.java:15948)
at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
at android.view.View.draw(View.java:16181)
at com.android.internal.policy.PhoneWindow$DecorView.draw(PhoneWindow.java:2690)
at android.view.View.updateDisplayListIfDirty(View.java:15174)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:281)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:287)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:322)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2615)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2434)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2067)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)


enter image description here

Answer

As others have suggested remove setOnClickListener() as it is not meant for AdapterView and regarding disabling ListView click, apart from above solution, you can also do this , inside your onListItemClick() method you can use if (swipeRefreshLayout.isRefreshing()) for e.g.

    @Override
    public void onItemClick (AdapterView < ? > adapter, View view,int position, long arg){
        // TODO Auto-generated method stub
        if (!swipeRefreshLayout.isRefreshing()){
            // Handle Clicks
        } 
    }

Edit

as to disable button click, inside your onClick() method of Button you can use the same above condition,

for e.g

 @Override
 public void onClick(View v) {
    if (!swipeRefreshLayout.isRefreshing()){
        // Handle Clicks
    } 
 }