Moayed I. Alayseh Moayed I. Alayseh - 1 month ago 13
Android Question

Cannot send list to Recycler View adapter Android

I have an

ArrayList
and the list gets data from retrofit and its working good. The problem is when I try to send the list to
RecyclerView
adapter, its empty! Any idea ?

This is Activity

public class ToDoRecycler extends AppCompatActivity {
RecyclerView todoRecyclerView;

private RecyclerView.Adapter todoAdapter;
private RecyclerView.LayoutManager todoLayoutManager;
public List<ToDo>results;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_to_do_recycler);


todoRecyclerView = (RecyclerView)findViewById(R.id.todoRecyclerView);
todoRecyclerView.setHasFixedSize(true);
results= new ArrayList<ToDo>();
todoLayoutManager = new LinearLayoutManager(this);
todoRecyclerView.setLayoutManager(todoLayoutManager);
todoRecyclerView.setAdapter(todoAdapter);
todoAdapter = new TodoRecyclerAdapter(this,results);
getRetrofitObject();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(ToDoRecycler.this,AddToDo.class);
startActivity(intent);
}});}

public void getRetrofitObject() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
APIService service = retrofit.create(APIService.class);
Call<Result> call = service.getresults();
call.enqueue(new Callback<Result>() {
@Override
public void onResponse(Call<Result> call, Response<Result> response) {
results = response.body().getResults();
}
@Override
public void onFailure(Call<Result> call, Throwable t) {
Log.d("onFailure", t.toString());
}});}
}


Adapter

public class TodoRecyclerAdapter extends RecyclerView.Adapter<TodoRecyclerAdapter.ViewHolder> {
static List<ToDo> todoResults;
static Context context;
List<ToDo>results;
public TodoRecyclerAdapter(List<ToDo> results) {
this.todoResults = results;
}
@Override
public TodoRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.todo_items, null);
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
@Override
public void onBindViewHolder(final TodoRecyclerAdapter.ViewHolder holder, final int position) {


}

@Override
public int getItemCount() {
return todoResults.size();
}


public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView todoTitle;

public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
todoTitle = (TextView) itemLayoutView.findViewById(R.id.todo_title);
}
}
}

Answer

The issue is that you are creating/calling adapter before the the results coming from retrofit...!

You should wait for the results and when you get the result, initialize and set the adapter to the recyclerview in the callback...!

Use the below code to get some help,

    call.enqueue(new Callback<Result>() {
                @Override
                public void onResponse(Call<Result> call, Response<Result> response) {
                    results = response.body().getResults();
 todoRecyclerView.setAdapter(new TodoRecyclerAdapter(this,results));               
}
                @Override
                public void onFailure(Call<Result> call, Throwable t) {
                    Log.d("onFailure", t.toString());

                }});}

and remove the adapter initialization from onCreate() method

Update: Your activity class should be look like the below one,

public class ToDoRecycler extends AppCompatActivity {
    RecyclerView todoRecyclerView;

    private RecyclerView.Adapter todoAdapter;
    private RecyclerView.LayoutManager todoLayoutManager;
    public List<ToDo>results;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_to_do_recycler);


        todoRecyclerView = (RecyclerView)findViewById(R.id.todoRecyclerView);
        todoRecyclerView.setHasFixedSize(true);
        results= new ArrayList<ToDo>();
        todoLayoutManager = new LinearLayoutManager(this);
        todoRecyclerView.setLayoutManager(todoLayoutManager);
        getRetrofitObject();
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ToDoRecycler.this,AddToDo.class);
                startActivity(intent);
            }});}
}

   public void getRetrofitObject() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        APIService service = retrofit.create(APIService.class);
        Call<Result> call = service.getresults();
        call.enqueue(new Callback<Result>() {
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                results = response.body().getResults();
                todoRecyclerView.setAdapter(new TodoRecyclerAdapter(ActivityName.this,results));    
            }
            @Override
            public void onFailure(Call<Result> call, Throwable t) {
                Log.d("onFailure", t.toString());
            }});}

}
Comments