Answer the question
In order to leave comments, you need to log in
How to pass ArrayList from regular class to Activity?
there is a class
public class FilmsModel {
private int postCount = 5;
private int offset = 0;
private List filmsData;
private Integer video_id;
private String video_photo_130;
private String video_photo_320;
private String text;
private String film_name;
private String film_genre;
private String film_desc;
private JSONArray films;
private List<FilmsModel> filmsModels = new ArrayList<>();
public FilmsModel(){}
public FilmsModel(String desc){
this.film_desc = desc;
}
public String getDescrtion(){
return film_desc;
}
public void getFilmsFromAPI(){
VKRequest request = VKApi.wall().get(VKParameters.from(
VKApiConst.OWNER_ID, -133127588,
VKApiConst.COUNT, postCount,
VKApiConst.OFFSET, offset,
VKApiConst.EXTENDED, 1));
request.executeWithListener(new VKRequest.VKRequestListener() {
@Override
public void onComplete(VKResponse response) {
super.onComplete(response);
parseResponse(response);
Log.d("Wall POSTS", response.json.toString());
}
@Override
public void attemptFailed(VKRequest request, int attemptNumber, int totalAttempts) {
super.attemptFailed(request, attemptNumber, totalAttempts);
}
@Override
public void onError(VKError error) {
super.onError(error);
}
@Override
public void onProgress(VKRequest.VKProgressType progressType, long bytesLoaded, long bytesTotal) {
super.onProgress(progressType, bytesLoaded, bytesTotal);
}
});
}
private void parseResponse(VKResponse response){
JSONObject json = response.json;
try{
JSONObject responseArr = json.getJSONObject("response");
JSONArray items = responseArr.getJSONArray("items");
Log.d("Items ==> ", items.toString());
for (int i=0; i < items.length(); i++){
try{
JSONObject film = items.getJSONObject(i);
filmsModels.add(new FilmsModel(film.getString("text")));
Log.d("ITEMS BY i", i + " => " + filmsModels.get(i).film_desc);
}catch(JSONException e){
e.printStackTrace();
}
}
getFilms();
}catch (JSONException e){
e.printStackTrace();
}
Log.d("PARSERRR ", json.toString());
}
public List<FilmsModel> getFilms(){
for(int i=0; i < filmsModels.size(); i++){
Log.d("LIST ARRAY ==> TEXT", filmsModels.get(i).film_desc);
}
return filmsModels;
}
@Override
public String toString() {
return film_desc;
}
}
public FilmsModel(String desc){
this.film_desc = desc;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.films_layout);
recyclerView = (RecyclerView)findViewById(R.id.films_rv);
verticalLinearLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(verticalLinearLayoutManager);
adapter = new RecyclerAdapter();
recyclerView.setAdapter(adapter);
filmsModel = new FilmsModel();
filmsModel.getFilmsFromAPI();
for(FilmsModel txt : filmsModel.getFilms()){
Log.d("LIST ARRRR ", txt.getDescrtion());
}
adapter.addAll(filmsModel.getFilms());
}
Answer the question
In order to leave comments, you need to log in
Yes, your requests go asynchronously, so you need some kind of callback so that at the moment when you have already received the movies, you can say about this activity.
Roughly speaking, you can do this: let your FilmsModel have a built-in interface with one method
public interface FilmsReadyCallback {
filmsReady(List<FilmsModel> films);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question