私はアンドロイドスワイプのリフレッシュレイアウトについて学んでいます。私はswipeRefreshLayoutの "setRefreshing(true)"で、次にonRefreshメソッドで私はfetchMovie()を書きました。しかし、fetchMovie()は呼び出されませんでした。それを修正するには? SwipeRefreshLayoutに関するドキュメントで見られるようにonRefresh Android SwipeRefreshLayoutは私のメソッドを呼び出さない
package com.example.zihadrizkyef.belajarswiperefresh;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.example.zihadrizkyef.belajarswiperefresh.app.MyApplication;
import com.example.zihadrizkyef.belajarswiperefresh.helper.Movie;
import com.example.zihadrizkyef.belajarswiperefresh.helper.SwipeListAdapter;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
private String TAG = MainActivity.class.getSimpleName();
private String URL_TOP_250 = "http://api.androidhive.info/json/imdb_top_250.php?offset=";
private SwipeRefreshLayout swipeRefreshLayout;
private ListView listView;
private SwipeListAdapter adapter;
private List<Movie> movieList;
private int offSet = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
movieList = new ArrayList<Movie>();
adapter = new SwipeListAdapter(this, movieList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "aaaaaaaaaa", Toast.LENGTH_SHORT).show();
}
});
swipeRefreshLayout.setOnRefreshListener(this);
swipeRefreshLayout.post(new Runnable() {
@Override
public void run() {
swipeRefreshLayout.setRefreshing(true);
}
});
}
@Override
public void onRefresh() {
fetchMovie();
}
private void fetchMovie() {
swipeRefreshLayout.setRefreshing(true);
String url = URL_TOP_250+offSet;
JsonArrayRequest req = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
System.out.println(TAG+response.toString());
if (response.length() > 0) {
for (int i=0; i<=response.length(); i++) {
try {
JSONObject movieObject = response.getJSONObject(i);
int rank = movieObject.getInt("rank");
String title = movieObject.getString("title");
Movie movie = new Movie(rank, title);
movieList.add(0, movie);
if (rank > offSet) {
offSet = rank;
}
} catch (JSONException e) {
System.out.println(TAG + " : JSON Parsing error : "+e.getMessage());
}
}
adapter.notifyDataSetChanged();
}
swipeRefreshLayout.setRefreshing(false);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println(TAG+" : Server error : "+error.getMessage());
Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
swipeRefreshLayout.setRefreshing(false);
}
});
MyApplication.getInstance().addToRequestQueue(req);
}
}