@
@
@atambalasi2016-11-15 18:04:00
Java
@atambalasi, 2016-11-15 18:04:00

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;
    }
}

In the getFilmsFromAPI method, I make a request to the VK API and get the data.
In the parseResponse(VKResponse response) method, I parse the data received from the API and put them into the ArrayList filmsModels.add(new FilmsModel(film.getString("text")));
field initialization in constructor
public FilmsModel(String desc){
        this.film_desc = desc;
    }

In the getFilms() method, I return the data.
Question. When I call the getFilms() method in the Activity sheet is empty. I suspect that the screen is drawn before I receive data. If it’s right, then how can I pass them on to me in this case. (I read about the Otto Event Bus in Android until I realized I wanted to do it through the android sdk).
@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

1 answer(s)
M
Maxim, 2016-11-18
@z17

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);
}

Your activity can implement it and, in the implementation of the filmsReady method, draw a list of films.
Pass the activity to the FilmsModel object and call it instead of the getFilms() method

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question