I get this error when implementing the refresh update

advertisements

i am getting below error

setOnRefreshListener (android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener) in SwipeRefreshLayout cannot be applied to (in.com.goalert.activity.MainActivity)

at

swipeRefreshLayout.setOnRefreshListener(this);

full code is below

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();
    private DrawerLayout drawerLayout;
    private Toolbar toolbar;
    private SessionManager pref;
    private SQLiteHandler db;
    private ListView listView;
    private FeedListAdapter listAdapter;
    public static List<FeedItem> feedItems =new ArrayList<>();
    private SwipeRefreshLayout swipeRefreshLayout;
    private String URL_FEED = "http://api.androidhive.info/feed/feed.json";
    static int count = 0;
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.list);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        initNavigationDrawer();
        listAdapter = new FeedListAdapter(this, feedItems);
        listView.setAdapter(listAdapter);
        pref = new SessionManager(getApplicationContext());
        db = new SQLiteHandler(getApplicationContext());
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
        swipeRefreshLayout.setOnRefreshListener(this);

        // We first check for cached request
        Cache cache = AppController.getInstance().getRequestQueue().getCache();
        Entry entry = cache.get(URL_FEED);

        if (entry != null) {
            // fetch the data from cache
            try {
                String data = new String(entry.data, "UTF-8");
                try {
                    parseJsonFeed(new JSONObject(data));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

        }else {
            // making fresh volley request and getting json
            JsonObjectRequest jsonReq = new JsonObjectRequest(Method.GET,
                    URL_FEED, null, new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    VolleyLog.d(TAG, "Response: " + response.toString());
                    if (response != null) {
                        parseJsonFeed(response);
                    }
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                }
            });

            // Adding request to volley request queue
            AppController.getInstance().addToRequestQueue(jsonReq);
        }

    }

    public void onRefresh() {
        Log.i("onRefresh","onRefresh");
        fetchfeed();
    }

    private void fetchfeed() {
        Log.i("fetchfeed","fetchfeed");
        String URL_FEED = "http://www.goalert.in/feed/engineering.json";
        swipeRefreshLayout.setRefreshing(true);

        JsonObjectRequest jsonReq = new JsonObjectRequest(Request.Method.GET,
                URL_FEED, null, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                VolleyLog.d("Response",response.toString());
                if (response != null) {
                    swipeRefreshLayout.setRefreshing(false);

                    parseJsonFeed(response);
                }
                swipeRefreshLayout.setRefreshing(false);

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d("", "Error: " + error.getMessage());
            }
        });
        AppController.getInstance().addToRequestQueue(jsonReq);

        // Adding request to volley request queue
        //AppController.getInstance().addToRequestQueue(jsonReq);
        // showing refresh animation before making http call

    }

    private void parseJsonFeed(JSONObject response) {
        try {
            JSONArray feedArray = response.getJSONArray("feed");

            for (int i = 0; i < feedArray.length(); i++) {
                JSONObject feedObj = (JSONObject) feedArray.get(i);

                FeedItem item = new FeedItem();
                item.setId(feedObj.getInt("id"));
                item.setName(feedObj.getString("name"));

                // Image might be null sometimes
                String image = feedObj.isNull("image") ? null : feedObj
                        .getString("image");
                item.setImge(image);
                item.setStatus(feedObj.getString("status"));
                item.setProfilePic(feedObj.getString("profilePic"));
                item.setTimeStamp(feedObj.getString("timeStamp"));

                // url might be null sometimes
                String feedUrl = feedObj.isNull("url") ? null : feedObj
                        .getString("url");
                item.setUrl(feedUrl);

                feedItems.add(item);
            }

            // notify data changes to list adapater
            listAdapter.notifyDataSetChanged();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


You can try to use below code for swipeRefreshLayout This code is work for me.

Here is SwipeRefreshLayout XML code snippiest.

  <android.support.v4.widget.SwipeRefreshLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_refresh_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:divider="@null"
        android:dividerHeight="0dp" />
</android.support.v4.widget.SwipeRefreshLayout>

Here is the java code snippiest look like as per your requirement or code

 final SwipeRefreshLayout swipeView = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);

 swipeView.setEnabled(false);
 swipeView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            swipeView.setRefreshing(true);
            (new Handler()).postDelayed(new Runnable() {
                @Override
                public void run() {
                    swipeView.setRefreshing(false);
                    try {
                        onRefresh();
                    } catch (Exception e) {
                       e.printStackTrace();
                    }
                }
            }, 3000);
        }
    });
  // user defined on-refresh method
 private void onRefresh() {
    Log.i("onRefresh","onRefresh");
    fetchfeed();
}

private void fetchfeed() {
   Log.i("fetchfeed","fetchfeed");
   String URL_FEED = "http://www.goalert.in/feed/engineering.json";

   JsonObjectRequest jsonReq = new JsonObjectRequest(Request.Method.GET,
            URL_FEED, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            VolleyLog.d("Response",response.toString());
            if (response != null) {
                parseJsonFeed(response);
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d("", "Error: " + error.getMessage());
        }
    });
    AppController.getInstance().addToRequestQueue(jsonReq);
}

Remains you can use your code as per your requirement.

I suggest to try to use this solution. It will work