I am developing an android app with multiple tabs using viewpager. However, one of the tabs which contain recyclerview just keep on loading non stop as shown in the attached screenshot. The recyclerview contains a list of images. I am able to load imageview and textview in other tabs successfully, only this tab which contain recyclerview has issue.
Adapter:
public class MasterListAdapter extends RecyclerView.Adapter<MasterListAdapter.Holder> {
private Context mContext;
private List<Integer> mImageIds;
private LayoutInflater inflater;
#BindView(R.id.images_list_view) ImageView imageView;
public MasterListAdapter(Context context, List<Integer> imageIds) {
mContext = context;
mImageIds = imageIds;
inflater = LayoutInflater.from(context);
}
#Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
return new Holder(inflater.inflate(R.layout.list_view, parent, false));
}
#Override
public void onBindViewHolder(Holder holder, int position) {
Glide.with(mContext).load(mImageIds.get(position)).apply(RequestOptions.fitCenterTransform()).into(imageView);
}
#Override
public int getItemCount() {
return mImageIds.size();
}
public class Holder extends ViewHolder {
public Holder(View itemView) {
super(itemView);
}
}
}
Fragment:
public class ListFragment extends BaseFragment {
#BindView(R.id.recyclerView)
RecyclerView recyclerView;
#Override
int getLayoutId() {
return R.layout.fragment_list;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
init();
}
private void init() {
recyclerView.setAdapter(new MasterListAdapter(getContext(),ImageAssets.getImages()));
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
}
}
ImageAssets:
public class ImageAssets {
private static final List<Integer> images = new ArrayList<Integer>() {{
add(R.drawable.user);
//add(R.drawable.img2);
//add(R.drawable.img3);
//add(R.drawable.img4);
//add(R.drawable.img5);
//add(R.drawable.img6);
//add(R.drawable.img7);
//add(R.drawable.img8);
//add(R.drawable.img9);
//add(R.drawable.img10);
//add(R.drawable.img11);
//add(R.drawable.img12);
//add(R.drawable.img13);
}};
public static List<Integer> getImages() {
return images;
}
}
Viewpager:
static class ViewPagerAdapter extends FragmentPagerAdapter {
ViewPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
#Override
public Fragment getItem(int position) {
return Page.values()[position].getFragment();
}
#Override
public CharSequence getPageTitle(int position) {
return Page.values()[position].getTitle();
}
#Override
public int getCount() {
return Page.values().length;
}
}
enum Page {
First("View") {
#Override
Fragment getFragment() {return new ListFragment();}
};
private String title;
Page(String title) {
this.title = title;
}
String getTitle() {
return title;
}
abstract Fragment getFragment();
}
list_view.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:foreground="?attr/selectableItemBackground"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/images_list_view"
android:clipToPadding="false"
android:src="#drawable/user">
</ImageView>
</android.support.v7.widget.CardView>
Update:
Hi all, thank you for all your value responses to my post. I have figured out the cause of the infinite loading issue. It was because I imported ListFragment in MainActivity accidentally which has the exact same name as my fragment class. Removing the import solved the issue.
Instead of
#BindView(R.id.images_list_view) ImageView imageView;
try this :-
public class Holder extends ViewHolder {
public ImageView imageView;
public Holder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.images_list_view);
}
and in onBind
#Override
public void onBindViewHolder(Holder holder, int position) {
Glide.with(mContext)
.load(mImageIds.get(position))
.apply(RequestOptions.fitCenterTransform())
.into(holder.imageView);
}
As you are calling a static reference your ImageAssets constructor is never getting called.. so get the correct list of images try this in your fragment :-
private void init() {
ImageAssets imageAssets = new ImageAssets(); // add this line to get the images
recyclerView.setAdapter(new MasterListAdapter(getContext(),ImageAssets.getImages()));
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
}
Make constructor in ImageAssets class and initialize images there !!
Found your problem. You forgot to bind your view inside Holder class.
public class MasterListAdapter extends RecyclerView.Adapter<MasterListAdapter.Holder> {
private Context mContext;
private List<Integer> mImageIds;
private LayoutInflater inflater;
public MasterListAdapter(Context context, List<Integer> imageIds) {
mContext = context;
mImageIds = imageIds;
inflater = LayoutInflater.from(context);
}
#Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
return new Holder(inflater.inflate(R.layout.list_view, parent, false));
}
#Override
public void onBindViewHolder(Holder holder, int position) {
Glide.with(mContext).load(mImageIds.get(position)).apply(RequestOptions.fitCenterTransform()).into(holder.imageView);
}
#Override
public int getItemCount() {
return mImageIds.size();
}
public class Holder extends ViewHolder {
#BindView(R.id.images_list_view) ImageView imageView;
public Holder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
}
Fragment
public class ListFragment extends BaseFragment {
#BindView(R.id.recyclerView)
RecyclerView recyclerView;
#Override
int getLayoutId() {
return R.layout.fragment_list;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
ButterKnife.bind(this, view);
init();
}
private void init() {
recyclerView.setAdapter(new MasterListAdapter(getContext(),ImageAssets.getImages()));
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
}
}
Related
I,ve tried everything and nothing is working... Any solution please. if i use listview it works perfectly but on recycler view i'm getting an error.
the code is below
home fragment
recyclerViewOne = view.findViewById(R.id.recyclerViewOne);
collectionReference = firebaseFirestore.collection("Trending songs");
recyclerViewOne.setHasFixedSize(true);
recyclerViewOne.setLayoutManager(new
LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL,false));
mUpload = new ArrayList<>();
gridViewHolder = new GridViewHolder(getActivity(), mUpload, new
GridViewHolder.ItemClickListeners() {
#Override
public void onClicke(GridModel model, int post) {
((DashboardActivity) getActivity()).method();
((DashboardActivity) getActivity()).playO(post);
}
});
recyclerViewOne.setAdapter(gridViewHolder);
i need some help please
According to the naming, you are trying the set a ViewHolder as an adapter to the recycler. If that is the case, then you should implement it the correct way: a custom Adapter that extends RecyclerView.Adapter and that add that adapter to the recycler view
this is the GridViewHolder
public class GridViewHolder extends RecyclerView.Adapter<GridViewHolder.SongsAdapterViewHolder> {
Context context;
List<GridModel> gridModels;
private ItemClickListeners listeners;
private int selected;
#NonNull
#Override
public SongsAdapterViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.grid_item, parent,false);
return new SongsAdapterViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull SongsAdapterViewHolder holder, int position) {
GridModel gridModel = gridModels.get(position);
holder.mDesc.setText(gridModel.getmDesc());
holder.mTitle.setText(gridModel.getmTitle());
String duration = Utility.convertion(Long.parseLong(gridModel.getDuration()));
Glide.with(context).load(gridModel.imageurl).into(holder.imageurl);
holder.bind(gridModel, listeners);
}
public GridViewHolder(Context context, List<GridModel> gridModels, ItemClickListeners listeners) {
this.context = context;
this.gridModels = gridModels;
this.listeners = listeners;
}
#Override
public int getItemCount() {
return gridModels.size();
}
public class SongsAdapterViewHolder extends RecyclerView.ViewHolder{
TextView mTitle, mDesc;
ImageView imageurl;
public SongsAdapterViewHolder(#NonNull View itemView) {
super(itemView);
mTitle = itemView.findViewById(R.id.gridTitle);
mDesc= itemView.findViewById(R.id.gridMusician);
imageurl = itemView.findViewById(R.id.gridImageView);
}
public void bind(final GridModel gridModel, final ItemClickListeners listeners) {
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listeners.onClicke(gridModel, getAdapterPosition());
}
});
}
}
public interface ItemClickListeners {
void onClicke(GridModel model, int post);
}
public int getSelected() {
return selected;
}
public void setSelected(int selected) {
this.selected = selected;
}
}
I have an abstract base fragment class that extends from fragment. Monitor list fragment extend from this base fragment. I need to set individual adapters in each fragment because the data in the fragments are different. Problem is, the monitor list fragment only returns 1 fragment with no data?
In BaseFragment
public abstract class BaseFragment extends Fragment {
private ArrayList<CholesterolMonitor> objectList;
private int mColumnCount = 1;
public void setObjectList(String listKey){
if (getArguments()!=null){
this.objectList= getArguments().getParcelableArrayList(listKey);
}
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup parent, Bundle savedInstanseState)
{
View view = provideYourFragmentView(inflater,parent,savedInstanseState);
// Set to recycler view
if (view instanceof RecyclerView) {
Context context = view.getContext();
RecyclerView recyclerView = (RecyclerView) view;
if (mColumnCount <= 1) {
recyclerView.setLayoutManager(new LinearLayoutManager(context));
} else {
recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
}
}
return view;
}
}
In monitor list fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = super.onCreateView(inflater, container,
savedInstanceState);
//Set individual adapter
monitor_list = getObjectList();
if (v instanceof RecyclerView) {
RecyclerView recyclerView = (RecyclerView) v;
adapter = new MonitorListRecyclerAdapter(monitor_list, this.getContext());
recyclerView.setAdapter(adapter);
}
return v;
}
I found out that the layout I inflated in XML should be a RecyclerView type.
Maybe you can use a abstract method to create Adapter
This may not resolve your problem, but you can do your adapter logic in your sub fragment class, this way will be more clearly
public abstract class BaseFragment extends Fragment {
private ArrayList<CholesterolMonitor> objectList;
private int mColumnCount = 1;
public void setObjectList(String listKey){
if (getArguments()!=null){
this.objectList= getArguments().getParcelableArrayList(listKey);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanseState)
{
View view = provideYourFragmentView(inflater, parent, savedInstanseState);
// Set to recycler view
if (view instanceof RecyclerView) {
Context context = view.getContext();
RecyclerView recyclerView = (RecyclerView) view;
if (mColumnCount <= 1) {
recyclerView.setLayoutManager(new LinearLayoutManager(context));
} else {
recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
}
recyclerView.setAdapter(this.createAdapter());
}
return view;
}
abstract <ViewHolderT extends RecyclerView.ViewHolder, AdapterT extends RecyclerView.Adapter<ViewHolderT>> AdapterT createAdapter();
}
SubFragmentA
public class SubFragmentA extends BaseFragment {
#Override
SubAdapterA createAdapter() {
return new SubAdapterA();
}
public static class SubAdapterA extends RecyclerView.Adapter<SubViewHolderA> {
#NonNull
#Override
public SubViewHolderA onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
return null;
}
#Override
public void onBindViewHolder(#NonNull SubViewHolderA subViewHolder, int i) {
}
#Override
public int getItemCount() {
return 0;
}
}
public static class SubViewHolderA extends RecyclerView.ViewHolder {
public SubViewHolderA(#NonNull View itemView) {
super(itemView);
}
}
}
SubFragmentB
public class SubFragmentB extends BaseFragment {
#Override
SubAdapterB createAdapter() {
return new SubAdapterB();
}
public static class SubAdapterB extends RecyclerView.Adapter<SubViewHolderB> {
#NonNull
#Override
public SubViewHolderB onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
return null;
}
#Override
public void onBindViewHolder(#NonNull SubViewHolderB subViewHolder, int i) {
}
#Override
public int getItemCount() {
return 0;
}
}
public static class SubViewHolderB extends RecyclerView.ViewHolder {
public SubViewHolderB(#NonNull View itemView) {
super(itemView);
}
}
}
If you got multiple ViewHolder Type you can do like this
public static class BaseViewHolder extends RecyclerView.ViewHolder {
protected TextView commonTextView;
public BaseViewHolder(#NonNull View itemView) {
super(itemView);
//init commonTextView;
}
}
public static class SubViewHolderB extends BaseViewHolder {
private TextView textViewB;
public SubViewHolderB(#NonNull View itemView) {
super(itemView);
//init textViewB
}
public void bindData(DataModelB data) {
commonTextView.setText("Common text set Integer data :" + data.intField);
textViewB.setText("This is Integer :" + data.intField);
}
}
public static class SubViewHolderA extends BaseViewHolder {
private TextView textViewA;
public SubViewHolderA(#NonNull View itemView) {
super(itemView);
//init textViewB
}
public void bindData(DataModelA data) {
commonTextView.setText("Common text set String data :" + data.strField);
textViewA.setText("This is String :" + data.strField);
}
}
public static class DataModelB {
int intField;
}
public static class DataModelA {
String strField;
}
I have a fragment that is suppose to display the results of my alert dialog in my RecyclerView. Every time I click the "ADD" in my dialog, it adds duplicated results to my RecyclerView. I've searched and searched but cannot seem to find what I am doing wrong. I've tried adding .clear(); but if I add that, nothing shows up in my RecyclerView at all. I've added in my adapter getItemId and getItemViewType to return position; but the items still get duplicated. I've added adapter.setData(model) followed by adapter.notifyDataSetChange(); and my RecyclerView still shows duplicated items. The app runs so I have no logcat to post. Thank you.
Model
public class SubjectsModel
{
//private long id;
private String mTitle;
private String mTeacher;
public String getmTitle()
{
return mTitle;
}
public void setmTitle(String title)
{
this.mTitle = title;
}
public String getmTeacher()
{
return mTeacher;
}
public void setmTeacher(String teacher)
{
this.mTeacher = teacher;
}
}
Fragment
public class SubjectsFrag extends DialogFragment implements
SubjectsEditor.OnAddSubjectListener
{
private static final String TAG = SubjectsFrag.class.getSimpleName();
#NonNull
Context context;
private EditText titleView, teacherView;
private String sTitle, sTeacher;
public EmptyRecyclerView recyclerView;
public RecyclerView.LayoutManager layoutManager;
public RecyclerSubAdapter recyclerSubAdapter;
public ArrayList<SubjectsModel> subMod = new ArrayList<>();
DbHelper helper;
#BindView(R.id.main_root)
ViewGroup root;
public SubjectsFrag() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_subjects, container, false);
FloatingActionButton fab = view.findViewById(R.id.fab_sub);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showDialog();
}
});
helper = new DbHelper(getActivity());
helper.getSubject();
titleView = view.findViewById(R.id.edit_subject);
teacherView = view.findViewById(R.id.edit_subject_teacher);
View emptyView = view.findViewById(R.id.empty_subject_view);
recyclerView = view.findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerSubAdapter = new RecyclerSubAdapter(getContext(), subMod);
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(recyclerSubAdapter);
return view;
}
#Override
public void OnAddSubjectSubmit(String title, String teacher)
{
SubjectsModel model = new SubjectsModel();
model.setmTitle(title);
model.setmTeacher(teacher);
//subMod.clear();
subMod.add(model);
recyclerSubAdapter.setData(subMod);
recyclerSubAdapter.notifyDataSetChanged();
}
private void showDialog()
{
SubjectsEditor addSubjectDialog = new SubjectsEditor();
addSubjectDialog.setTargetFragment(this, 0);
addSubjectDialog.show(getFragmentManager(), null);
}
}
Adapter
public class RecyclerSubAdapter extends RecyclerView.Adapter<RecyclerSubAdapter.ViewHolder>
{
private static final String TAG = RecyclerSubAdapter.class.getSimpleName();
public List<SubjectsModel> subMod = new ArrayList<>();
private OnItemClicked onClick;
static ClickListener clickListener;
Context context;
DbHelper helper;
public RecyclerSubAdapter(Context context, ArrayList<SubjectsModel> subMod)
{
this.context = context;
this.subMod = subMod;
this.helper = new DbHelper(context);
}
#NonNull
#Override
public RecyclerSubAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType)
{
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.subjects_item_list, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(final RecyclerSubAdapter.ViewHolder holder, final int position)
{
SubjectsModel currentSubject = subMod.get(position);
holder.titleView.setText(currentSubject.getmTitle());
holder.teacher.setText(currentSubject.getmTeacher());
//helper.addClass(subMod.get(position));
}
public class ViewHolder extends RecyclerView.ViewHolder implements
View.OnClickListener
{
TextView titleView;
TextView teacher;
CardView cardView;
public ViewHolder(View itemView)
{
super(itemView);
titleView = itemView.findViewById(R.id.subject_subject);
teacher = itemView.findViewById(R.id.subject_teacher_text);
cardView = itemView.findViewById(R.id.card_view);
}
#Override
public void onClick(View view)
{
if (clickListener != null)
{
}
}
}
#Override
public int getItemCount()
{
if (subMod == null)
{
Log.d(TAG, "sub is null");
}
return subMod.size();
}
#Override
public long getItemId(int position)
{
return position;
}
#Override
public int getItemViewType(int position)
{
return position;
}
public interface OnItemClicked
{
void onItemClick(int position);
}
public void setOnClick(OnItemClicked onClick)
{
this.onClick = onClick;
}
public void setClickListener(ClickListener clicked)
{
RecyclerSubAdapter.clickListener = clicked;
}
public interface ClickListener
{
void itemClicked(SubjectsModel model, int position);
}
public void setData(ArrayList<SubjectsModel> data)
{
this.subMod = data;
//this.subMod.clear();
this.subMod.addAll(data);
notifyDataSetChanged();
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_root">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.example.ashleighwilson.schoolscheduler.adapter.EmptyRecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="60dp"/>
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<TextView
android:id="#+id/empty_subject_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:visibility="gone"
android:text="#string/no_subjects"/>
</RelativeLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab_sub"
style="#style/FAB" />
</RelativeLayout>
In OnAddSubjectSubmit() you call subMod.add(model);
and then you call recyclerSubAdapter.setData(subMod); which in turn calls this.subMod.addAll(data);.
Check it yourself. I believe it's there where you add the new item twice.
Suggestion: comment out recyclerSubAdapter.setData(subMod); from OnAddSubjectSubmit().
this.subMod = data;
this.subMod.addAll(data);
You initilize subMod by assigning data to it, and later you add data again:
this.subMod.addAll(data);
I am working on recyclerview and cardview to load some images using Picasso Library.
I am pasting my code here.
This is my DivineDestinationDistricWiseActivity.
public class DivineDestinationsDistrictWise extends Fragment {
RecyclerView all_divine_dest_places_list;
List<Places> placesList;
GridLayoutManager glm;
public DivineDestinationsDistrictWise() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_divine_destinations_district_wise, container, false);
all_divine_dest_places_list = (RecyclerView) rootView.findViewById(R.id.all_divine_places_rv_id);
glm = new GridLayoutManager(getActivity() , 2);
all_divine_dest_places_list.setLayoutManager(glm);
all_divine_dest_places_list.setHasFixedSize(true);
intializeData();
initializeAdapter();
return rootView;
}
private void intializeData() {
placesList = new ArrayList<>();
placesList.add(new Places("Divine Destinations", R.drawable.hyd_balakampeta_ellamma));
placesList.add(new Places("Adventure Journeys", R.drawable.hyd_birla_mandir));
placesList.add(new Places("boatings", R.drawable.hyd_jagannatha_temple));
placesList.add(new Places("Heritage Spots", R.drawable.hyd_mecca_masjid));
placesList.add(new Places("Shopping", R.drawable.hyd_peddamma_temple));
placesList.add(new Places("Nature Discovery", R.drawable.hyd_peddamma_temple));
placesList.add(new Places("Wild Life", R.drawable.wild_life_main));
}
private void initializeAdapter()
{
DDDWAdapter adapter =new DDDWAdapter(placesList);
// places_type_list_recyclerView.addItemDecoration(new SimpleDividerItemDecoration(this));
all_divine_dest_places_list.setAdapter(adapter);
}
}
This is my adapter class.
public class DDDWAdapter extends RecyclerView.Adapter<DDDWAdapter.DivineDestViewHolder> {
CardView cv;
Context context;
public class DivineDestViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView personName;
ImageView photo_city;
public DivineDestViewHolder(View itemView) {
super(itemView);
cv=(CardView)itemView.findViewById(R.id.cardview_places_id);
personName=(TextView)itemView.findViewById(R.id.places_list_id_tv);
photo_city=(ImageView)itemView.findViewById(R.id.image_places_list_id);
context = itemView.getContext();
photo_city.setOnClickListener(this);
}
#Override
public void onClick(View v) {
}
}
List<Places> place;
DDDWAdapter(List<Places> place)
{
this.place=place;
}
#Override
public DivineDestViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.places_type_list, parent, false);
DivineDestViewHolder pvh = new DivineDestViewHolder(v);
return pvh;
}
#Override
public void onBindViewHolder(DivineDestViewHolder holder, int position) {
holder.personName.setText(place.get(position).city_name);
holder.photo_city.setImageResource(place.get(position).photo);
//here i am trying to load images with Picasso Library but not getting Images.
Picasso.with(context).load(R.id.image_places_list_id).into(holder.photo_city);
}
#Override
public int getItemCount() {
return place.size();
}
}
This is my Places class.
public class Places {
String city_name;
int photo;
Places(String city_name, int photo)
{
this.city_name = city_name;
this.photo=photo;
}
}
I have tried by inserting like below if I use like this I am getting images. But When I scroll the screen Images are scrolling with little jerky behaviour.
Picasso.with(context).load(place.get(position).photo).into(holder.photo_city);
Please help me out from this... Thanks in advance.....
first generate the setter getter of the Places Class
public class Places {
String city_name;
int photo;
public SampleClass(String city_name, int photo) {
this.city_name = city_name;
this.photo = photo;
}
public String getCity_name() {
return city_name;
}
public void setCity_name(String city_name) {
this.city_name = city_name;
}
public int getPhoto() {
return photo;
}
public void setPhoto(int photo) {
this.photo = photo;
}
}
and then initialize your adapter and send also a context to it like
DDDWAdapter adapter =new DDDWAdapter(getActivity,placesList);
and then your adapter look like this
public class DDDWAdapter extends RecyclerView.Adapter<DDDWAdapter.DivineDestViewHolder> {
CardView cv;
Context context;
List<Places> place;
public class DivineDestViewHolder extends RecyclerView.ViewHolder implements
View.OnClickListener{
TextView personName;
ImageView photo_city;
public DivineDestViewHolder(View itemView) {
super(itemView);
cv=(CardView)itemView.findViewById(R.id.cardview_places_id);
personName=(TextView)itemView.findViewById(R.id.places_list_id_tv);
photo_city=(ImageView)itemView.findViewById(R.id.image_places_list_id);
context = itemView.getContext();
photo_city.setOnClickListener(this);
}
#Override
public void onClick(View v) {
}
}
DDDWAdapter(Context context,List<Places> place)
{
this.context=context;
this.place=place;
}
#Override
public DivineDestViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.places_type_list, parent, false);
DivineDestViewHolder pvh = new DivineDestViewHolder(v);
return pvh;
}
#Override
public void onBindViewHolder(DivineDestViewHolder holder, int position) {
holder.personName.setText(place.get(position).city_name());
//here i am trying to load images with Picasso Library but not getting Images.
Picasso.with(context).load(place.get(position).getPhoto()).into(holder.photo_city);
}
#Override
public int getItemCount() {
return place.size();
}
You are setting image resource directly to your imageView and then latter you are assigning R.id instead of R.drawble to load via Picasso
#Override
public void onBindViewHolder(DivineDestViewHolder holder, int position) {
holder.personName.setText(place.get(position).city_name);
holder.photo_city.setImageResource(place.get(position).photo);
//here i am trying to load images with Picasso Library but not getting Images.
Picasso.with(context).load(R.id.image_places_list_id).into(holder.photo_city);
}
You need to load images like this
#Override
public void onBindViewHolder(DivineDestViewHolder holder, int position) {
holder.personName.setText(place.get(position).city_name);
//here is how you should be loading image
Picasso.with(context).load(place.get(position).photo).into(holder.photo_city);
}
I'm not quite sure if this is possible, but I'm trying to change the content of a custom DialogFragment (read: content as in 1 imageview, and 3 textviews) inside of a Recyclerview adapter.
My current custom DialogFragment:
public class ServiceDialogFragment extends DialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.service_card, container, false);
}
}
And the layout:
As you see in the image above: I have a list behind the where the DialogFragment is displayed and whenever I press one of those "buttons" that DialogFragment appears. But my question is: how do I change the content of the custom DialogFragment inside of my Recyclerview Adapter?
My adapter:
public class ServiceAdapter extends RecyclerView.Adapter<ServiceAdapter.ViewHolder>{
private final String TAG = "******* Service Adapter";
private ArrayList<ServiceObject> mDataset;
private Context mContext;
private Resources mResources;
private Context location;
public ServiceAdapter(ArrayList<ServiceObject> mDataset, Context mContext, Resources mResources) {
this.mDataset = mDataset;
this.mContext = mContext;
this.mResources = mResources;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
location = parent.getContext();
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.service_box, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.title.setText(mDataset.get(position).getTitle());
holder.icon.setImageResource(mResources.getIdentifier(Integer.toString(mDataset.get(position).getIcon()), "drawable", "net.example.adapter"));
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
#Override
public int getItemCount() {
return mDataset == null ? 0 : mDataset.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView title, desc, price;
ImageView icon;
public ViewHolder(View v) {
super(v);
title = (TextView) v.findViewById(R.id.service_box_title);
price = (TextView) v.findViewById(R.id.service_card_price);
icon = (ImageView) v.findViewById(R.id.service_box_icon);
v.setOnClickListener(this);
}
#Override
public void onClick(View v) {
FragmentActivity activity = (FragmentActivity)(mContext);
FragmentManager fm = activity.getSupportFragmentManager();
ServiceDialogFragment alertDialog = new ServiceDialogFragment();
alertDialog.show(fm, "display_service");
}
}
}
Thanks to #raghunandan, I figured it out. Resource: http://developer.android.com/reference/android/app/DialogFragment.html