I have worked with listviews for some time now and as I began to understand them, they come out with recyclerview. I have a simple working example containing all of the items. I would like to create sections within the recyclerview using the same layout. For example, each section is filtered by data (Breakfast, lunch, dinner). How would I create sections within the recyclerview? If you need more information, or believe it is a bad question, I will update my question or supply more code.
How I would filter the adapters by breakfast, lunch and dinner:
FoodAdapter mAdapterBreakfast = new FoodAdapter(LogSet.filterBy("Breakfast"), R.layout.row, this);
FoodAdapter mAdapterLunch = new FoodAdapter(LogSet.filterBy("Lunch"), R.layout.row, this);
FoodAdapter mAdapterDinner = new FoodAdapter(LogSet.filterBy("Dinner"), R.layout.row, this);
How I get all of the items in the adapter:
FoodAdapter mAdapter = new FoodAdapter(LogSet.all(), R.layout.row, this);
Here is a working example of all:
Adapter
public class FoodAdapter extends RecyclerView.Adapter<FoodAdapter.ViewHolder> {
private List<LogSet> meals;
private int rowLayout;
private Context mContext;
View v;
public FoodAdapter(List<LogSet> mMeals, int rowLayout, Context context) {
this.meals = mMeals;
this.rowLayout = rowLayout;
this.mContext = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
v = LayoutInflater.from(viewGroup.getContext()).inflate(rowLayout, viewGroup, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
LogSet meal = meals.get(i);
viewHolder.countryName.setText(meal.getLiftName());
}
#Override
public int getItemCount() {
return meals == null ? 0 : meals.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView countryName;
public ViewHolder(View itemView) {
super(itemView);
countryName = (TextView) itemView.findViewById(R.id.foodName);
}
}
}
MainActivity
public class MainActivity extends ActionBarActivity {
RecyclerView mRecyclerView;
Date mDate = new Date();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_testing_rec);
mRecyclerView = (RecyclerView) findViewById(R.id.list);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
FoodAdapter mAdapter = new FoodAdapter(LogSet.all(), R.layout.row, this);
mRecyclerView.setAdapter(mAdapter);
/*
FoodAdapter mAdapterBreakfast = new FoodAdapter(LogSet.filterBy("Breakfast"), R.layout.row, this);
FoodAdapter mAdapterLunch = new FoodAdapter(LogSet.filterBy("Lunch"), R.layout.row, this);
FoodAdapter mAdapterDinner = new FoodAdapter(LogSet.filterBy("Dinner"), R.layout.row, this);
*/
}
}
Here is how I would send each List value to the adapter:
Adapter
public class FoodAdapter extends RecyclerView.Adapter<FoodAdapter.ViewHolder> {
private List<LogSet> meals;
private List<LogSet> breakfast;
private List<LogSet> lunch;
private List<LogSet> dinner;
private int rowLayout;
private Context mContext;
View v;
public FoodAdapter(List<LogSet> mMeals, List<LogSet> mBreakfast, List<LogSet> mLunch, List<LogSet> mDinner, int rowLayout, Context context) {
this.meals = mMeals;
this.breakfast = mBreakfast;
this.lunch = mLunch;
this.dinner = mDinner;
this.rowLayout = rowLayout;
this.mContext = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
v = LayoutInflater.from(viewGroup.getContext()).inflate(rowLayout, viewGroup, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
LogSet meal = meals.get(i);
viewHolder.countryName.setText(meal.getLiftName());
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView countryName;
public ViewHolder(View itemView) {
super(itemView);
countryName = (TextView) itemView.findViewById(R.id.foodName);
}
}
#Override
public int getItemCount() {
return meals == null ? 0 : meals.size();
}
}
MainActivity minus the all()
public class MainActivity extends ActionBarActivity {
RecyclerView mRecyclerView;
Date mDate = new Date();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_testing_rec);
mRecyclerView = (RecyclerView) findViewById(R.id.list);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
FoodAdapter mAdapter = new FoodAdapter(LogSet.all(), LogSet.filterBy("Breakfast"), LogSet.filterBy("Lunch"), LogSet.filterBy("Dinner"), R.layout.row, this);
mRecyclerView.setAdapter(mAdapter);
/*
FoodAdapter mAdapterBreakfast = new FoodAdapter(LogSet.filterBy("Breakfast"), R.layout.row, this);
FoodAdapter mAdapterLunch = new FoodAdapter(LogSet.filterBy("Lunch"), R.layout.row, this);
FoodAdapter mAdapterDinner = new FoodAdapter(LogSet.filterBy("Dinner"), R.layout.row, this);
*/
}
}
Again, How would I create sections within the RecyclerView with the Breakfast, Lunch and Dinner?
You can achieve it with only one adapter from this library: SectionedRecyclerViewAdapter.
First create a Section class to group your items:
class MySection extends StatelessSection {
String title;
List<LogSet> list;
public MySection(String title, List<LogSet> list) {
// call constructor with layout resources for this Section header, footer and items
super(R.layout.section_header, R.layout.section_item);
this.title = title;
this.list = list;
}
#Override
public int getContentItemsTotal() {
return list.size(); // number of items of this section
}
#Override
public RecyclerView.ViewHolder getItemViewHolder(View view) {
// return a custom instance of ViewHolder for the items of this section
return new MyItemViewHolder(view);
}
#Override
public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
MyItemViewHolder itemHolder = (MyItemViewHolder) holder;
// bind your view here
itemHolder.tvItem.setText(list.get(position).getLiftName());
}
#Override
public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
return new SimpleHeaderViewHolder(view);
}
#Override
public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
MyHeaderViewHolder headerHolder = (MyHeaderViewHolder) holder;
// bind your header view here
headerHolder.tvItem.setText(title);
}
}
Then you set up the RecyclerView with 3 instances of your section class (Breakfast, Lunch and Dinner):
// Create an instance of SectionedRecyclerViewAdapter
SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter();
// Create your sections with the list of data
MySection breakfastSection = new MySection("Breakfast", LogSet.filterBy("Breakfast"));
MySection lunchSection = new MySection("Lunch", LogSet.filterBy("Lunch"));
MySection dinnerSection = new MySection("Dinner", LogSet.filterBy("Dinner"));
// Add your Sections to the adapter
sectionAdapter.addSection(breakfastSection);
sectionAdapter.addSection(lunchSection);
sectionAdapter.addSection(dinnerSection);
// Set up your RecyclerView with the SectionedRecyclerViewAdapter
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(sectionAdapter);
Related
I am using Recyclerview. I added a menu with Recyclerview. I want to make a switch case in Recyclerview. So when I click on item 1, when I click on item 2, I click on step 3. When I click on my item .. I want to call a function. Currently I tried to read the position of the items with getAdapterPosition, but the output is always -1. For example, when I click on model, when I click on model2, I want to call a function.
Main2Activity
public class Main2Activity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
RecyclerView navRecyclerView;
LinearLayoutManager layoutManager;
ArrayList<NavigationDataModel> arrayList = new ArrayList<>();
NavigationAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
navigationView.setNavigationItemSelectedListener(this);
navRecyclerView = findViewById(R.id.navRecyclerView);
navRecyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
navRecyclerView.setLayoutManager(layoutManager);
DividerItemDecoration divider = new DividerItemDecoration(
navRecyclerView.getContext(),
DividerItemDecoration.VERTICAL
);
divider.setDrawable(ContextCompat.getDrawable(getBaseContext(), R.drawable.diveder_recycler));
navRecyclerView.addItemDecoration(divider);
initArray();
adapter = new NavigationAdapter(this, arrayList);
navRecyclerView.setAdapter(adapter);
private void initArray() {
NavigationDataModel model = new NavigationDataModel();
model.setIcon(R.mipmap.avatar);
model.setTitle("Hesabım");
arrayList.add(model);
NavigationDataModel model2 = new NavigationDataModel();
model2.setIcon(R.mipmap.sss3);
model2.setTitle("S.S.S");
arrayList.add(model2);
}
NavigationAdapter
public class NavigationAdapter extends RecyclerView.Adapter<NavigationAdapter.ViewHolder> {
Context context;
ArrayList<NavigationDataModel> arrayList = new ArrayList<>();
public NavigationAdapter(Context context, ArrayList<NavigationDataModel> arrayList) {
this.context = context;
this.arrayList = arrayList;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.navigationrecyclerview_adapter11, parent, false);
final ViewHolder holder = new ViewHolder(view);
holder.navTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = holder.getAdapterPosition();
System.out.println("asdasdadasd" + position);
}
});
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.navIcon.setImageResource(arrayList.get(position).getIcon());
holder.navIconNew.setImageResource(arrayList.get(position).getIcon());
holder.rootView.setBackgroundColor(arrayList.get(position).getColor());
holder.navTitle.setText(arrayList.get(position).getTitle());
}
#Override
public int getItemCount() {
return arrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView navIcon, navIconNew;
TextView navTitle;
LinearLayout rootView;
public ViewHolder(View itemView) {
super(itemView);
rootView = itemView.findViewById(R.id.rootView);
navIcon = itemView.findViewById(R.id.navIcon);
navIconNew = itemView.findViewById(R.id.navIconNew);
navTitle = itemView.findViewById(R.id.navTitle);
Toast.makeText(context, String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
}
}
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.navIcon.setImageResource(arrayList.get(position).getIcon());
holder.navIconNew.setImageResource(arrayList.get(position).getIcon());
holder.rootView.setBackgroundColor(arrayList.get(position).getColor());
holder.navTitle.setText(arrayList.get(position).getTitle());
holder.navTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// use position herer
}
});
}
I made a simple RecyclerView, but it do not load images!
Here are my codes. RecyclerView has 11 raws and each raw has 2 textviews and 1 imageview,
This is the important part of main activity,
MainActivity.java:
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager layoutManager;
String[] F_Name, D_Name;
int[] Img_res = {R.drawable.a_beautiful_mind,
R.drawable.a_seperation,
R.drawable.casablanca,
R.drawable.daei_jan_napelon,
R.drawable.game_of_thrones,
R.drawable.life_of_pie,
R.drawable.the_dictator,
R.drawable.the_godfather,
R.drawable.the_martian,
R.drawable.the_revenant,
R.drawable.v_for_vendetta};
ArrayList<DataProvider> arrayList = new ArrayList<DataProvider>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.myRecyclerView);
F_Name = getResources().getStringArray(R.array.Film_Names);
D_Name = getResources().getStringArray(R.array.Director_Names);
int i = 0;
for (String name : F_Name){
DataProvider dataProvider = new DataProvider(Img_res[i], name, D_Name[i]);
arrayList.add(dataProvider);
i++;
}
adapter = new RecyclerAdapter(arrayList);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
}
Here is my Adapter, I added 11 raws, and each raw contains 2 textviews and 1 ImageView,
Adapter:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder> {
private ArrayList<DataProvider> arrayList = new ArrayList<DataProvider>();
public RecyclerAdapter(ArrayList<DataProvider> arrayList){
this.arrayList = arrayList;
}
#Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_recyclerview_raws, parent, false);
RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(view);
return recyclerViewHolder;
}
#Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
DataProvider dataProvider = arrayList.get(position);
holder.imageView.setImageResource(dataProvider.getImg_res());
holder.f_name.setText(dataProvider.getF_name());
holder.d_name.setText(dataProvider.getD_name());
}
#Override
public int getItemCount() {
return arrayList.size();
}
public static class RecyclerViewHolder extends RecyclerView.ViewHolder{
ImageView imageView;
TextView f_name, d_name;
public RecyclerViewHolder(View view){
super(view);
imageView = (ImageView)view.findViewById(R.id.myRecyclerViewImage);
f_name = (TextView)view.findViewById(R.id.myTextVFilmName);
d_name = (TextView)view.findViewById(R.id.myTextVDirectorName);
}
}
}
Can anyone help me please?
in onLoadFinsih i get back from the server List of movies i want to
add this list to my RecyclerView adapter
i tried to create List of movies and pass it to my RecyclerView adapter
then when i get the list of movies from the server in onLoadFinish method i add
this list to the list that i passed in the adapter then notify the adapter
but not working
public class MainActivity extends AppCompatActivity
implements LoaderManager.LoaderCallbacks<List<Movie>>{
private RecyclerView.Adapter movieRecyclerViewAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private List<Movie> mListOfMovies;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
progressBar = (ProgressBar) findViewById(R.id.progress_bar);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mListOfMovies = new ArrayList<>();
movieRecyclerViewAdapter = new MovieItemAdapter(this, mListOfMovies);
mLayoutManager = new LinearLayoutManager(this);
recyclerView.setAdapter(movieRecyclerViewAdapter);
}
#Override
public void onLoadFinished(Loader<List<Movie>> loader, List<Movie> movies) {
//hide progress bar after fetch data from server
progressBar.setVisibility(View.GONE);
mListOfMovies.clear();
mListOfMovies.addAll(movies);
movieRecyclerViewAdapter.notifyItemRangeChanged();
}
public class MovieItemAdapter extends RecyclerView.Adapter<MovieItemAdapter.ViewHolder> {
private List<Movie> mListOfMovies;
private Context mContext;
public MovieItemAdapter(Context context, List<Movie> movies) {
this.mListOfMovies = movies;
this.mContext = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext)
.inflate(R.layout.movie_list_item, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Movie movieItem = mListOfMovies.get(position);
holder.moviePoster.setImageBitmap(movieItem.getBitmap());
holder.movieName.setText(movieItem.getTitle());
DecimalFormat format = new DecimalFormat("0.0");
holder.voteAverage.setText(format.format(movieItem.getVoteAverage()));
holder.releaseDate.setText(movieItem.getReleaseDate());
}
#Override
public int getItemCount() {
return mListOfMovies.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView moviePoster;
public TextView movieName;
public TextView voteAverage;
public TextView releaseDate;
public ViewHolder(View itemView) {
super(itemView);
moviePoster = (ImageView) itemView.findViewById(R.id.movie_poster);
movieName = (TextView) itemView.findViewById(R.id.movie_name);
voteAverage = (TextView) itemView.findViewById(R.id.vote_average);
releaseDate = (TextView) itemView.findViewById(R.id.release_date);
}
}
}
In onLoadFinish() you should update the list in your adapter with the received data.
Then you should call the adapter's adapter.notifyDataSetChanged().
EDIT:
Create a method in adapter like
setMovieList(List<Movie> movies){
mListOfMovies = movies;
this.notifyDataSetChanged();
}
and call it from onLoadFinished():
movieRecyclerViewAdapter.setMovieList(mListOfMovies)
Make your ViewHolder class non static in adapter class. Then check again.
I have one android fragment that have a listView. Now I want implement onItemClickListener and show a Modal, if the user click on one item of ListView. Then, if I click on one items, I should to see a Modal with another listView. So with my code, if I try to click on one items, I can display the items of the last element of my principal listView, and it is not good.
This is my Fragment:
public class AlertsFragment extends Fragment {
private AlertAdapter pAdapter;
AlertXAgentAdapter agentAdapter;
AlertXReactionAdapter reactionAdapter;
private RecyclerView recyclerView,recyclerAgentView,recyclerReactionView;
private List<Alert> lista= new ArrayList<Alert>();
View v;
ExpandableListView expandableListView;
ExpandableListAdapter expandableListAdapter;
private static final int ARG_SECTION_NUMBER =6;
#Override
public void onAttach(Context context) {
super.onAttach(context);
((AndroidNavDrawerActivity) context).onSectionAttached(
ARG_SECTION_NUMBER);
}
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
v=inflater.inflate(R.layout.alerts_activity, container, false);
recyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
//recupero la lista delle medication
AlertDAO manager = new AlertDAO(this.getContext());
lista = manager.getAlerts();
pAdapter = new AlertAdapter(lista, new AlertAdapter.OnItemClickListener() {
#Override
public void onItemClick(Alert item) {
try{
final Dialog dialog = new Dialog(v.getContext());
LayoutInflater inflater = LayoutInflater.from(v.getContext());
View dialogView = inflater.inflate(R.layout.alert_agent_reaction_modal, null);
//agent
recyclerAgentView = (RecyclerView) dialogView.findViewById(R.id.recycler_agent_view);
//reaction
recyclerReactionView = (RecyclerView) dialogView.findViewById(R.id.recycler_reaction_view);
dialog.setContentView(dialogView);
dialog.setTitle("Agent and Reaction");
//agent
agentAdapter = new AlertXAgentAdapter(item.getListaAgent());
//reaction
reactionAdapter = new AlertXReactionAdapter(item.getListaReaction());
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(v.getContext());
recyclerAgentView.setLayoutManager(mLayoutManager);
recyclerAgentView.setItemAnimator(new DefaultItemAnimator());
recyclerAgentView.setAdapter(agentAdapter);
RecyclerView.LayoutManager mLayoutManager2 = new LinearLayoutManager(v.getContext());
recyclerReactionView.setLayoutManager(mLayoutManager2);
recyclerReactionView.setItemAnimator(new DefaultItemAnimator());
recyclerReactionView.setAdapter(reactionAdapter);
dialog.show();
}catch(Exception e){
Log.e("","");
}
}
});
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(v.getContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(pAdapter);
/**
* Inflate the layout for this fragment
*/
return v;
}
}
This is my Adapter
public class AlertAdapter extends RecyclerView.Adapter<AlertAdapter.MyViewHolder> {
private List<Alert> list;
public Alert alert;
public OnItemClickListener listener;
public interface OnItemClickListener {
void onItemClick(Alert item);
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView startDate, endDate,description,status;
public MyViewHolder(View view) {
super(view);
startDate = (TextView) view.findViewById(R.id.startDate);
endDate = (TextView) view.findViewById(R.id.endDate);
description = (TextView) view.findViewById(R.id.description);
status = (TextView) view.findViewById(R.id.status);
view.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(listener != null){
listener.onItemClick(alert);
}
}
}
public AlertAdapter(List<Alert> list,OnItemClickListener listener) {
this.list = list;
this.listener=listener;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.alert_list_row, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
alert = list.get(position);
holder.startDate.setText(alert.getDateStart()!=null ? alert.getDateStart() : "");
holder.endDate.setText(alert.getDateEnd()!=null ? alert.getDateEnd() : "");
holder.description.setText(alert.getAlertInfo().getDisplayName());
//holder.type.setText(planOfCare.getDateEnd()!=null ? planOfCare.getDateEnd() : "");
holder.status.setText(alert.getAlarmAlertInfo().getDisplayName());
}
#Override
public int getItemCount() {
return list.size();
}
}
Your Alert variable is not getting assigned to proper object as you are assigning it in onBindViewHolder method. In your ViewHolder's onclick method try using this instead of passing the Alert variable
listener.onItemClick(list.get(this.getAdapterPosition()));
I think you should do something like this
public class AlertsFragment extends Fragment implements OnClickListener {
personRewardAdapter =new PersonRewardAdapter(PersonRewardSelectionActivity.this, Utilities.R_entities);
rewardList.setAdapter(personRewardAdapter);
rewardList.setOnclickListener(getActivity());
}
public void OnClick(){
}
do this in onCreateView. I am doing this in my code and maybe It helps you, so i am sharing to you
I am battling with the android Recyclerviewer. I followed most tutorials and also read posts here on StackOverflow, but i am still not coming right.
MainActivity
public class MainActivity extends ActionBarActivity {
RecyclerView myrecyclerView;
MyAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myrecyclerView = (RecyclerView)findViewById(R.id.myRecycleView);
adapter = new MyAdapter(MainActivity.this, getData());
final LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
myrecyclerView.setLayoutManager(layoutManager);
myrecyclerView.setAdapter(adapter);
myrecyclerView.setItemAnimator(new DefaultItemAnimator());
//
}
//the Dataset
public static List<MyDataModel> getData()
{
List<MyDataModel> myData = new ArrayList<>();
String[] postText = {"ibm", "apple", "google", "microsoft"};
int[] postImgUrl = {R.drawable.google, R.drawable.ibm, R.drawable.microsoft, R.drawable.apple};
for(int x=0; x<postText.length && x<postImgUrl.length; x++)
{
MyDataModel myModel = new MyDataModel();
myModel.postImgUrl = postImgUrl[x];
myModel.postText = postText[x];
myData.add(myModel);
}
return myData;
}
MyAdapter
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{
List<MyDataModel> MyDataset;
private LayoutInflater inflater;
public MyAdapter(Context cont, List<MyDataModel> MyDataset)
{
inflater = LayoutInflater.from(cont);
}
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int ViewType)
{
//get ItemLayoutView from xml
View itemLayoutView = inflater.inflate(R.layout.my_items_row, parent, false);
//pass it to the ViewHolder
ViewHolder myViewHolder = new ViewHolder(itemLayoutView);
return myViewHolder;
}
public void onBindViewHolder(ViewHolder viewHolder, int position)
{
viewHolder.postText.setText(MyDataset.get(position).postText);
viewHolder.postImage.setImageResource(MyDataset.get(position).postImgUrl);
}
public int getItemCount()
{
return MyDataset.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder
{
public TextView postText;
public ImageView postImage;
public ViewHolder(View v)
{
super(v);
postText = (TextView)v.findViewById(R.id.myPostText);
postImage = (ImageView)v.findViewById(R.id.myPostImage);
}
}
}
My model Data
public class MyDataModel {
String postText;
int postImgUrl;
}
I am testing on real device through android studio, other apps i that i am also testing are fine.
Please help thank you.
You didn't initialize MyDataset inside MyAdapter class