我收到以下错误
setOnRefreshListener (android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener) 在SwipeRefreshLayout中无法应用 至 (in.com.goalert.activity.MainActivity)
在
swipeRefreshLayout.setOnRefreshListener(this);
完整代码位于
之下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();
}
}
答案 0 :(得分:0)
您必须使用android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener
答案 1 :(得分:0)
我的问题解决了 做
implements SwipeRefreshLayout.OnRefreshListener
答案 2 :(得分:0)
您可以尝试使用以下代码进行swipeRefreshLayout此代码对我有用。
这是SwipeRefreshLayout XML代码最流行的。
<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>
这是根据您的要求或代码
的java代码 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);
}
仍然可以根据您的要求使用您的代码。
我建议尝试使用此解决方案。它会起作用