Kiran Raju Kiran Raju - 3 months ago 19
Java Question

How to arrange the data on RecycleView in android?

I would like to use RecycleView for displaying my array of data.

This is my Adapater class which is setting data on view.

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.MyViewHolder>{

private Context context;
private List<Pojo> dataList;

public DataAdapter(Context context, List<Pojo> dataList) {
this.context = context;
this.dataList = dataList;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_main,parent,false);
return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.categoryTV.setText(dataList.get(position).getGameName());
}

@Override
public int getItemCount() {

return dataList.size();
}

public class MyViewHolder extends RecyclerView.ViewHolder{

public TextView categoryTV;

public MyViewHolder(View itemView) {
super(itemView);
categoryTV = (TextView) itemView.findViewById(R.id.text);
}
}
}


This is my activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:id="@+id/rellayout"
tools:context="com.acknotech.kiran.retrofit_second.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text"/>

<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

</android.support.v7.widget.RecyclerView>
</RelativeLayout>


This is MainActivity..

public class MainActivity extends AppCompatActivity {

private RecyclerView recyclerView;
private DataAdapter dataAdapter;
private List<Pojo> dataArrayList;

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

private void initViews(){
recyclerView=(RecyclerView) findViewById(R.id.my_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
loadJSON();
}

private void loadJSON(){
dataArrayList = new ArrayList<>();
Retrofit retrofit=new Retrofit.Builder().baseUrl("my url").addConverterFactory(GsonConverterFactory.create()).build();
AllGamesAPI requestInterface=retrofit.create(AllGamesAPI.class);

Call<List<Pojo>> call= requestInterface.getJSON();
call.enqueue(new Callback<List<Pojo>>() {
@Override
public void onResponse(Call<List<Pojo>> call, Response<List<Pojo>> response) {
dataArrayList = response.body();
dataAdapter=new DataAdapter(getApplicationContext(),dataArrayList);
recyclerView.setAdapter(dataAdapter);
}

@Override
public void onFailure(Call<List<Pojo>> call, Throwable t) {
Log.e("Error",t.getMessage());
}

});
}
}


When i try to execute it always printing only one item at a time but in dataList i have 11 items.

How to use RecyclerView to get all items in a page?

Thanks & Regards

Answer

You shouldn't inflate the activities layout in onCreateViewHolder, but instead inflate a layout for only one item (e.g. android.R.layout.simple_list_item_1, or a custom one). This layout should not use match_parent for the height of items.