How to create a ListView with multiple views - java

my problem is - how to create custom list view with not just repeating one custom view, but like in Instagram or other app where list include other views in it, which it looks like scroll view with list view android other views in it, but Roman Guy says "List View in a Scroll view is a very poor way", and I'm agreed with it, don't believe which is Google use this way...
What are the best way to achieve this thing with ListView or Recycler View

To achieve that UI, you must define multiple view types for your Listview or Recyclerview; a very similar question has been answered here.
In your example, you will have two view types:
<Horizontal Scroll> which is an embedded horizontal Recyclerview/Listview.
<View> which is a view type that you have defined.
There are many tutorials on this concept. I would recommend you to use Recyclerview in your implementation due to its advantages over Listview.

Hey this is how your main fragment will look like :
package com.leoneo.stackoverflow;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.soulpatch.stackoverflow.R;
import java.util.ArrayList;
public class RecyclerViewExample extends Fragment {
private ArrayList<Object> mValues = new ArrayList<>();
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
final RecyclerView recyclerView = inflater.inflate(R.layout.recycler_view_example, container, false);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
final MultpleItemAdapter adapter = new MultpleItemAdapter(mValues);
recyclerView.setAdapter(adapter);
return recyclerView;
}
}
And here's your adapter code.
package com.leoneo.stackoverflow;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.soulpatch.stackoverflow.R;
import java.util.ArrayList;
public class MultpleItemAdapter extends RecyclerView.Adapter<MultpleItemAdapter.ViewHolder> {
private ArrayList<Object> mValues = new ArrayList<>();
public MultpleItemAdapter(ArrayList<Object> values) {
mValues = values;
}
enum ItemType {
TYPE_A,
TYPE_B;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case 1:
//Inflate Type A layout
final LinearLayout linearLayoutA = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_a, parent, false);
//And pass the view to the ViewHolder
return new ViewHolder(linearLayoutA);
break;
case 2:
//Inflate Type B layout
final LinearLayout linearLayoutB = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_b, parent, false)
//And pass the view to the ViewHolder
return new ViewHolder(linearLayoutB);
break;
}
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
switch (getItemViewType(position)) {
case 1:
final TypeA typeA = (TypeA) mValues.get(position);
//Deal with the views that you defined for LinearLayout A
break;
case 2:
final TypeB typeB = (TypeB) mValues.get(position);
//Deal with the views that you defined for LinearLayout B
break;
}
}
#Override
public int getItemCount() {
return mValues.size();
}
#Override
public int getItemViewType(int position) {
final Object obj = mValues.get(position);
if (obj instanceof TypeA) {
return ItemType.TYPE_A.ordinal();
} else if (obj instanceof TypeB) {
return ItemType.TYPE_B.ordinal();
}
return super.getItemViewType(position);
}
static class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View itemView) {
super(itemView);
}
}
//Class that I want to be displayed in a CardView
private class TypeA {
}
//Class that I want to be displayed in a a LinearLayout
private class TypeB {
}
}
You can have as many classes as you want like TypeA and TypeB and add types to ItemType class as well accordingly.
Rest should be pretty self explanatory assuming you've worked with RecyclerViews before.

You are looking for different View types. It's possible with using these
GetViewTypeCount() this is an overridable method which returns how many view type you have in your listview-recycleview.
getItemViewType(int pos) get which item view type should return at the given position
For example if you want to add a different view in every 10 item, and there is only 2 type of views, you should use a code like this:
#Override
public int getItemViewType(int position){
if (position % 10 == 0)
return SECOND_ITEM;
return FIRST_ITEM;
}
#Override
public int getViewTypeCount() {
return 2;
}
And at the getView() function you can handle it with a switch-case or if-else structure like this:
switch (getItemViewType(cursor.getPosition())) {
case SECOND_ITEM:
//something to do
break;
default:
//something to do
}
You might want to use 2 different layout file to inflate in the switch-case statement above. However, if the layouts of both items are not different that much, I recommend to create just 1 layout, and make views inside it visible and gone according to item you want to get.
Oh and in case you don't know where to use them, you use them at your adapter class. The functions may vary as which kind of adapter you use however, they all work with the same logic.
And finally, I recommend you to use recyclerview. It is just a bit harder to implement than listview but it is a great substitution of listview which is more powerful and flexible. You can do a research for it.
Here is how you can achieve this like instagram, facebook etc. : You inflate a scrollable view at the given positions.
I hope the answer helps.
As always,
Have a nice day.

Related

How can I use photos from my files in a ViewPager2?

I'm trying to use a viewpager2 to show pictures taken from my app. Right now, I'm just using photos currently on my device to test, although the tutorial I followed (https://www.geeksforgeeks.org/viewpager2-in-android-with-example/) showed me how to make a viewpager2 with premade resource files. I'm trying to use an image bitmap instead, although whenever I run the code, the images come out blank. Is it an error with my files or is there a problem in my code? Here is my code:
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
class ViewPager2Adapter extends RecyclerView.Adapter<ViewPager2Adapter.ViewHolder> {
// Array of images
// Adding images from drawable folder
Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/DCIM/yes/yes_20220809_095311.jpg");
Bitmap bitmap2 = BitmapFactory.decodeFile("/sdcard/DCIM/yes/yes_20220809_095311___WITHTEXT.jpg");
private Bitmap[] images = {bitmap, bitmap2};
private Context ctx;
// Constructor of our ViewPager2Adapter class
ViewPager2Adapter(Context ctx) {
this.ctx = ctx;
}
// This method returns our layout
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(ctx).inflate(R.layout.images_holder, parent, false);
return new ViewHolder(view);
}
// This method binds the screen with the view
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
// This will set the images in imageview
holder.images.setImageBitmap(images[position]);
}
// This Method returns the size of the Array
#Override
public int getItemCount() {
return images.length;
}
// The ViewHolder class holds the view
public static class ViewHolder extends RecyclerView.ViewHolder {
ImageView images;
public ViewHolder(#NonNull View itemView) {
super(itemView);
images = itemView.findViewById(R.id.images);
}
}
}
PS: I am currently using hard coded files for testing, this will be changed later. The problem might be that the file path is wrong, but that is why I am asking this question.

Converting RecyclerView to ViewPager / PagerAdapter

Background - New to Android, but pretty nifty with moving layouts around, understanding Java, Kotlin and XML. However this task seems to be way above my head.
Problem - I'm looking to convert the following Java file (RecyclerView) into a Kotlin file (ViewPager) - since I already have a ViewPager hooked-up with the same scrolling behaviour as desired. I get the impression it's a 10min job for a seasoned developer. If that's the case I wonder if I could call upon some assistance from the community? At least to work out where to start. I can't seem to find a guide on how to convert a RecyclerView into a PagerAdapter or ViewPager.
Essentially the existing ViewPager I'm using has static data (5 items) and this one could have tens of items, so needs to be dynamic with a separate datasource (items of CardItem).
RecyclerViewAdapter - Current (Java)
package com.APPNAME.fragments.cards;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import com.APPNAME.R;
import com.APPNAME.databinding.FragmentCardsRecentlyViewedBinding;
import com.APPNAME.model.cardItem.CardItem;
public class RecentlyViewedAdapter extends RecyclerView.Adapter<RecentlyViewedAdapter.RecentlyViewedViewHolder> {
public OnCardClicked listener;
private ArrayList<CardItem> items = new ArrayList<>();
public void addItems(ArrayList<CardItem> list) {
items = list;
notifyDataSetChanged();
}
#Override
public RecentlyViewedViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
FragmentCardsRecentlyViewedBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.fragment_cards_recently_viewed, parent, false);
return new RecentlyViewedViewHolder(binding);
}
#Override
public void onBindViewHolder(RecentlyViewedViewHolder holder, int position) {
holder.binding.setViewModel(new RecentlyViewedViewModel(items.get(position)));
}
#Override
public int getItemCount() {
return items.size();
}
interface OnCardClicked {
void onCardClicked(View view, CardItem cardItem);
}
class RecentlyViewedViewHolder extends RecyclerView.ViewHolder {
FragmentCardsRecentlyViewedBinding binding;
public RecentlyViewedViewHolder(FragmentCardsRecentlyViewedBinding itemView) {
super(itemView.getRoot());
binding = itemView;
binding.cardView.setOnClickListener(v -> {
if (listener != null) {
listener.onCardClicked(v, items.get(getAdapterPosition()));
}
});
}
}
}
ViewPagerAdapter - Future (Kotlin)
package com.APPNAME.fragments.cards
import android.support.annotation.DrawableRes
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import java.util.*
import kotlinx.android.synthetic.main.fragment_cards_recently_viewed.view.*
import com.APPNAME.R
import com.APPNAME.activities.BaseActivity
import com.APPNAME.activities.cards.NewCardActivity
import com.APPNAME.model.cardItem.CardItem
import com.APPNAME.views.wrapContentViewPager.ObjectAtPositionPagerAdapter
class RecentlyViewedItemAdapter constructor(private val activity: BaseActivity) : ObjectAtPositionPagerAdapter() {
private var items = ArrayList<CardItem>()
override fun instantiateItemObject(container: ViewGroup, position: Int) : Any {
return getImageView(container, R.drawable.placeholder_card_image) { NewCardActivity.start(activity, it) }
}
private fun getImageView(container: ViewGroup, #DrawableRes imageResourceId: Int, onClick: (imageResourceId: Int) -> Unit = {}): View {
val layoutInflater = LayoutInflater.from(container.context)
val layout = layoutInflater.inflate(R.layout.fragment_cards_recently_viewed, container, false)
val image = layout.recentlyViewedImage
image.setImageResource(imageResourceId)
image.setOnClickListener { onClick(imageResourceId) }
container.addView(layout)
return layout
}
override fun isViewFromObject(view: View, anObject: Any) = (view == anObject)
override fun getCount() = 5 //Placeholder
override fun destroyItemObject(container: ViewGroup, position: Int, view: Any) {
container.removeView(view as View)
}
}
If you have relatively large number of items and need views to be recycled then the RecyclerView is the right option for you. It will be definitely easier than creating a custom view pager adapter with a fixed item count and a view holder pattern.
The default/generic PageAdapter is not recycling anything - it's added to make sure you have all your views initiated and ready to swipe through. However, you can use https://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html that will destroy/recreate the fragments when no longer used or reused.
Job done. I actually surprised myself by being able to modify the recycler view to mimmick the PageView's behaviour precisely (including padding, snap scrolling etc). Thanks to the simple subclass SnapHelper. Saved me a tonne of work, so no need to refactor the codebase.
SnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(recyclerView);
PagerSnapHelper solved my problem.
binding.recyclerViewHomeAnnouncement.apply {
layoutManager = LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL, false)
binding.recyclerViewHomeAnnouncement.layoutManager = layoutManager
setHasFixedSize(true)
itemAnimator = DefaultItemAnimator()
adapter = homeAnnouncementAdapter
}
val snapHelper: SnapHelper = PagerSnapHelper()
snapHelper.attachToRecyclerView(binding.recyclerViewHomeAnnouncement)

Exchanging icons in listview items

I am trying to change icon image according to the item data . when pressing the icon, it should switch to other icon and change the item data. everything is going right but when i scroll up or down the new icon image change its place to some other items because i am using list view with adapter. how can i keep the new icons in the pressed items without mixing with other items.
package com.example.sairamkrishna.handymade;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class HandAdapter extends ArrayAdapter<HandClass> {
private int myColor;
public HandAdapter(Context context, ArrayList<HandClass> objects, int my_Color) {
super(context, 0, objects);
myColor = my_Color;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View myView = convertView;
final HandClass myData = getItem(position);
if (myView == null) {
myView = LayoutInflater.from(getContext()).inflate(
R.layout.label_item, parent, false);
}
ImageView aImage = (ImageView) myView.findViewById(R.id.itemImage);
aImage.setImageResource(myData.getClsImage());
TextView aName = (TextView) myView.findViewById(R.id.itemName);
aName.setText(myData.getClsName());
FrameLayout aColor = (FrameLayout) myView.findViewById(R.id.itemColor);
aColor.setBackgroundColor(myColor);
final ImageView aAddToBasket = (ImageView) myView.findViewById(R.id.itemAddToBasket);
aAddToBasket.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final Boolean aClsToBasket = (Boolean) myData.getClsToBasket();
if (aClsToBasket) {
aAddToBasket.setImageResource(R.drawable.ic_add_circle);
myData.setClsToBasket(false);
Toast.makeText(getContext(), "Remove from basket"+ position, Toast.LENGTH_SHORT).show();
} else {
// if (!aClsToBasket) {
aAddToBasket.setImageResource(R.drawable.ic_remove_circle);
myData.setClsToBasket(true);
Toast.makeText(getContext(), "Add to basket"+ position, Toast.LENGTH_SHORT).show();
}
}
});
ImageView aAddToFavorite = (ImageView) myView.findViewById(R.id.itemAddToFavorite);
aAddToFavorite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getContext(), "Favorite - List item was clicked at " + position, Toast.LENGTH_SHORT).show();
}
});
return myView;
}
}
A fundamental concept of ListView and RecyclerView is that you need to "update" the value (in this case, the image) for each portion of the row every time getView() or onBindViewHolder() is called.
For example, every time getView() is called, you're always updating the (text) value of TextView aName. That is, there is always a call to aName.setText().
Right now, the only time you call aAddToBasket.setImageResource() is inside an OnClickListener. Of course, it makes sense to do it here, but you must also update the image outside of the listener.
Add this code right after your ImageView aAddToBasket line:
if ((Boolean) myData.getClsToBasket()) {
aAddToBasket.setImageResource(R.drawable.ic_remove_circle);
} else {
aAddToBasket.setImageResource(R.drawable.ic_add_circle);
}
Add this line after icon change.
adapter.notifyDataSetChanged();

view.setTag(Object obj) throws nullpointerexception [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
I'm trying to create an app with listview and arrayadapter.
I have a custom class(Word) and a custom adapter(WordAdapter).
By running this code, the app is crashing
this is the error :
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.view.View.setTag(java.lang.Object)' on a null object reference
Code given below :
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class WordAdapter extends ArrayAdapter<Word> {
//datastructure to hold two textviews, so that it can be reused rather than instantiating
static class ViewHolder {
TextView textView1;
TextView textView2;
}
//sets up the arrayadapter
//#param context : specify the context (e.g current activity)
//#param words : list of words to populate the view (contains objects of custom word class)
public WordAdapter(#NonNull Context context, #NonNull ArrayList<Word> words) {
super(context, 0, words);
}
/*
Needs to override when using custom arrayadapater.
Called by the adapter class when populating data from data source into the view
#param position : position of the data in the data source
#param convertView : data from source is set as the data for this view
#param parent : parent view to which convertview gets attached
*/
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
ViewHolder viewHolder;
View list_item_view = convertView;
//checks if any reusable view is available
//list_item_view = null if no reusable view is available
if (list_item_view == null) {
viewHolder = new ViewHolder();
//convert the custom layout file into view object and inflate the parent with the view
//returns the root of the custom layout file (if attachToRoot = false)
//returns parent view to which the layout is to be attached (if attachToRoot = true)
list_item_view = LayoutInflater.from(getContext())
.inflate(R.layout.list_item, parent, false);
viewHolder.textView1 = (TextView) list_item_view.findViewById(R.id.miwok_text_view);
viewHolder.textView2 = (TextView) list_item_view.findViewById(R.id.default_text_view);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
//get the data from the position
Word word = getItem(position);
//set the miwok word
viewHolder.textView1.setText(word.getMiwokWord());
//set the default word
viewHolder.textView2.setText(word.getDefaultWord());
//retrun the view to which data is to be placed
return list_item_view;
}
}
Anyone please help.
you should change the following lines:
list_item_view .setTag(viewHolder);
viewHolder = (ViewHolder) list_item_view .getTag();

Displaying CardViews inside a RecyclerView using FirebaseUI and Firebase database

I am trying to display CardViews inside a RecyclerView, each card will represent a cheese object.
This cheese object has 6 instance variables.
This is my Cheese.java :
public class Cheese {
private String CheeseName;
private String CheeseCountryOfOrigin;
private String CheeseDayMade;
private String CheeseDayExpire;
private String CheeseDescription ;
private String CheesePrice;
public Cheese(){} //Required for firebase
public Cheese(String CheeseName, String CheeseCountryOfOrigin, String CheeseDayMade, String CheeseDayExpire, String CheeseDescription, String CheesePrice) {
this.CheeseName = CheeseName;
this.CheeseCountryOfOrigin = CheeseCountryOfOrigin;
this.CheeseDayMade = CheeseDayMade;
this.CheeseDayExpire = CheeseDayExpire;
this.CheeseDescription = CheeseDescription;
this.CheesePrice = CheesePrice;
}
public String getCheeseName() {
return CheeseName;
}
public String getCheeseCountryOfOrigin() {
return CheeseCountryOfOrigin;
}
public String getCheeseDayMade() {
return CheeseDayMade;
}
public String getCheeseDayExpire() {
return CheeseDayExpire;
}
public String getCheeseDescription() {
return CheeseDescription;
}
public String getCheesePrice() {
return CheesePrice;
}
}
and this is my cheese_card.xml (I hardcoded some android:text for better understanding): cheese_card.xml
my RecyclerView is in a fragment.
This is my fragment:
fragment_cheeses_list.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/cheeses_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
all my cheese items are already in my Firebase Real-Time Database. To make my life simpler I am trying to use FirebaseUI to populate my RecyclerView with data from my Firebase database.
This is my CheesesListFragment.java, which is displayed in my MainActivity:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.CardView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
public class CheeseListFragment extends Fragment {
private static final String TAG = "CheesesListFragment";
private FirebaseDatabase aFirebaseDatabase;
private DatabaseReference aCheesesDatabaseReference;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.e(TAG, "onCreateView Started Successfully");
//Create the recycler view object
RecyclerView cheesesRecycler = (RecyclerView) inflater.inflate(R.layout.fragment_cheeses_list, container, false);
//Add a grid layout manager to the recycler view
GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 1);
cheesesRecycler.setLayoutManager(layoutManager);
cheesesRecycler.setHasFixedSize(true);
aFirebaseDatabase = FirebaseDatabase.getInstance();
aCheesesDatabaseReference = aFirebaseDatabase.getReference().child("cheeses");
//Query the cheeses in firebase db using firebaseUI instead of addChildEventListener
Query query = aCheesesDatabaseReference;
//configuration for the FirebaseRecyclerAdapter
FirebaseRecyclerOptions<Cheese> options =
new FirebaseRecyclerOptions.Builder<Cheese>()
.setQuery(query, Cheese.class)
.build();
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Cheese, CheeseViewHolder>(options) {
#Override
public CheeseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// Create a new instance of the ViewHolder, in this case we are using a custom
// layout called R.layout.cheese_card for each item
CardView cv = (CardView) LayoutInflater.from(parent.getContext())
.inflate(R.layout.cheese_card, parent, false);
return new CheeseViewHolder(cv);
}
#Override
protected void onBindViewHolder(CheeseViewHolder holder, int position, Cheese model) {
CheeseViewHolder myHolder = (CheeseViewHolder)holder;
myHolder.cheeseName.setText(model.getCheeseName());
myHolder.cheeseCountryOfOrigin.setText(model.getCheeseCountryOfOrigin());
myHolder.cheeseDayMade.setText(model.getCheeseDayMade());
myHolder.cheeseDayExpire.setText(model.getCheeseDayExpire());
myHolder.cheeseDescription.setText(model.getCheeseDescription());
myHolder.cheesePrice.setText(model.getCheesePrice());
}
};
//Set the adapter to the recycle View
cheesesRecycler.setAdapter(adapter);
return cheesesRecycler;
}
public static class CheeseViewHolder extends RecyclerView.ViewHolder {
CardView cardView;
TextView CheeseName;
TextView CheeseCountryOfOrigin;
TextView CheeseDayMade;
TextView CheeseDayExpire;
TextView CheeseDescription;
TextView CheesePrice;
public CheeseViewHolder (CardView v){
super(v);
cardView = v;
CheeseName = (TextView)cardView.findViewById(R.id.cheese_name);
CheeseCountryOfOrigin= (TextView)cardView.findViewById(R.id.cheese_origin);
CheeseDayMade= (TextView)cardView.findViewById(R.id.cheese_day_made);
CheeseDayExpire= (TextView)cardView.findViewById(R.id.cheese_day_expire);
CheeseDescription= (TextView)cardView.findViewById(R.id.cheese_description);
CheesePrice= (TextView)cardView.findViewById(R.id.cheese_price);
}
}
}
So my questions are: (answering any of them is welcomed and very helpful)
If i get it right, onCreateViewHolder is supposed to make ViewHolders for my Cheese object using my cheese_card.xml . if so, assuming I delete onBindingViewHolder am I suppose to see lots of view holders that look like my cheese_card.xml?
in onBindingViewHolder in setText : how can I get my TextViews to get a value from my firebase?
I am new to programming and not sure about onCreateViewHolder, onBindingHolder and cheesesViewHolder.I am not sure what every code I writed there means as some of them are copy-pasted.If I got it all wrong, can you please explain how can I reach my desired outcome, and what I did wrong?
Thank you, in advance :)
Modify onBindingViewHolder and cheesesViewHolder. Because in onBindingViewHolder you will bind data with Views not Views with they ids. Bind Views with they ids inside cheesesViewHolder. For example:
CardView cardView;
TextView cheese_name;
TextView cheese_origin;
public CheeseViewHolder(CardView v) {
super(v);
cardView = v;
cheese_name = (TextView) cardView.findViewById(R.id.cheese_name);
cheese_origin = (TextView) cardView.findViewById(R.id.cheese_origin);
// and so on...
}
Then inside onBindingViewHolder you will do something like this:
#Override
protected void onBindViewHolder(CheeseViewHolder holder, int position, Cheese model) {
cheesesViewHolder myHolder = (cheesesViewHolder)holder;
myHolder.cheese_name.setText(model.getCheeseName());
myHolder. cheese_origin.setText(model.getCheeseOrigin());
//and so on...
}
I was able to eventually fix my problem and get onCreateViewHolder and onBindViewHolder to start simply by adding
adapter.startListening();
to my onStart method. like this:
#Override
public void onStart() {
super.onStart();
Log.e(TAG,"onStart Started Successfully");
adapter.startListening();
}
And I edited the code using #Yupi suggestion.

Categories