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?
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'm using RecyclerView with a custom Adapter but it's not showing data.
Adapter downloads some thumbnail images from database and get titles and infos from Api key.
Code of the Adapter Class:
public class mostViewedAdapter extends RecyclerView.Adapter<mostViewedAdapter.ViewHolder> {
private List<String> fTitle;
private List<String> fInfos;
private List<String> thumbs;
public mostViewedAdapter(List<String> titoli, List<String> infos, List<String>thumbsUrl){
fTitle = titoli;
fInfos = infos;
thumbs = thumbsUrl;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView titleTextView;
public TextView infoTextView;
public ImageView thumbImageView;
public ViewHolder(View itemView) {
super(itemView);
titleTextView = (TextView) itemView.findViewById(R.id.title);
infoTextView = (TextView) itemView.findViewById(R.id.infos);
thumbImageView = (ImageView) itemView.findViewById(R.id.thumb);
}
}
#Override
public mostViewedAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View mvView = inflater.inflate(R.layout.most_viewed, parent, false);
ViewHolder viewHolder = new ViewHolder(mvView);
return viewHolder;
}
#Override
public void onBindViewHolder(mostViewedAdapter.ViewHolder holder, int position) {
String titolo = fTitle.get(position);
String info = fInfos.get(position);
String thumb = thumbs.get(position);
UrlGenerator url = new UrlGenerator(thumb);
Picasso.with(holder.thumbImageView.getContext()).load(url.generateImgUrl())
.resize(50,70).centerCrop().into(holder.thumbImageView);
holder.infoTextView.setText(info);
holder.titleTextView.setText(titolo);
TextView tvTitolo = holder.titleTextView;
TextView tvInfo = holder.infoTextView;
ImageView ivImage = holder.thumbImageView;
}
#Override
public int getItemCount() {
return 0;
}
}
Recycler view:
public void initUI() {
RecyclerView recList = (RecyclerView) findViewById(R.id.rvCinema);
recList.hasFixedSize();
mostViewedAdapter adapter = new mostViewedAdapter(titoli, info, thumbsUrl);
recList.setAdapter(adapter);
RecyclerView.LayoutManager lm = new LinearLayoutManager(this);
recList.setLayoutManager(lm);
}
try like this:
MainActivity
public class MainActivity extends AppCompatActivity {
RecyclerView mRecyclerView;
mostViewedAdapter mViewAdapter;
//.... other elements
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView_ID);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
mViewAdapter= new mostViewedAdapter (listName, listInfo, listThumbs);
mRecyclerView.setAdapter(mViewAdapter);
}
}
Adapter Class
Instead of returning 0 as a count return the size of array of of any one of the three lists.
#Override
public int getItemCount() {
return fTitle.size();
//return fInfos.size();
//return thumbs.size();
}
Hope i helped, if still facing any issue, kindly post the logcat.
Please return fTitle.size();
#Override
public int getItemCount() {
return fTitle.size();
}
I have made a RecyclerView adapter and using that adapter to inflate data in recycler view , I previously used the general Method of ListView onItemClickListner but it doesn't seems to work with RecyclerView , Plese anyone help me out with this
My RecyclerView Adapter class is :
public class TimeAdaptra extends
RecyclerView.Adapter<TimeAdaptra.RecyclerViewHolder> {
ArrayList<String> time, status;
public TimeAdaptra(ArrayList<String> title, ArrayList<String> dec) {
this.time = title;
this.status = dec;
}
#Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.time_slice, parent, false);
RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(view);
return recyclerViewHolder;
}
#Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
holder.myTitle.setText(time.get(position));
holder.mydec.setText(status.get(position));
}
#Override
public int getItemCount() {
return time.size();
}
public static class RecyclerViewHolder extends RecyclerView.ViewHolder {
TextView myTitle;
TextView mydec;
public RecyclerViewHolder(View view) {
super(view);
myTitle = (TextView) view.findViewById(R.id.timee);
mydec = (TextView) view.findViewById(R.id.status);
}
}
}
And I am doing inflation like this :
public class Slots extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager manager;
private ArrayList<String> Time = new ArrayList<String>();
private ArrayList<String> Status = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_slots);
Time.add("18:00");
Status.add("Available");
recyclerView = (RecyclerView) this.findViewById(R.id.timeslice);
manager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(manager);
recyclerView.setHasFixedSize(true);
adapter = new TimeAdaptra(Time, Status);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
Make the RecyclerViewHolder implement View.OnClickListener like this:
public static class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView myTitle;
TextView mydec;
public RecyclerViewHolder(View view) {
super(view);
myTitle = (TextView) view.findViewById(R.id.timee);
mydec = (TextView) view.findViewById(R.id.status);
myTitle.setOnClickListener(this);
mydec.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.timee:
// do task
break;
case R.id.status:
//do task
break;
}
}
}
If you want to click on each item of the list then you should get the id of the root layout of the xml your are inflating and setOnClickListener() to it.
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
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);