program crashed. and I get this message:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.mob3000.group11.Cars.getImgurl()' on a null object reference
at com.mob3000.group11.Reservation.onCreate(Reservation.java:57)
I want to pass info to another activity
#Reservation class
public class Reservation extends AppCompatActivity {
ImageView imgMakeRes;
TextView pricMakeRes;
TextView brandMakeRes;
TextView girMakeRes;
TextView seatMakeRes;
public static final String BOOKING_INFORMATION= "booking_information";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reservation);
imgMakeRes = (ImageView) findViewById(R.id.imgMakeReservation);
pricMakeRes = (TextView) findViewById(R.id.pricMakereservation);
brandMakeRes = (TextView) findViewById(R.id.brandMakeReservation);
girMakeRes = (TextView) findViewById(R.id.girMakeReservation);
seatMakeRes = (TextView) findViewById(R.id.seatMakeReservation);
Cars cars = getIntent().getParcelableExtra(BOOKING_INFORMATION);
Glide.with(this).load(cars.getImgurl()).into(imgMakeRes);
pricMakeRes.setText((int) cars.getPrice());
brandMakeRes.setText(cars.getBrand());
girMakeRes.setText(cars.getGir());
seatMakeRes.setText(cars.getSeats());
Adapter class
public class FirestoreAdapter extends FirestoreRecyclerAdapter<Cars,FirestoreAdapter.myViewHolder> {
private OnListItemClick onListItemClick;
private Context context;
private ArrayList<Cars> listCars;
public FirestoreAdapter(#NonNull FirestoreRecyclerOptions<Cars> options,OnListItemClick onListItemClick) {
super(options);
this.onListItemClick=onListItemClick;
}
#Override
protected void onBindViewHolder(#NonNull myViewHolder holder, int position, #NonNull Cars cars) {
holder.textpri_ce.setText(cars.getPriceWithLabel());
holder.textbra_nd.setText(cars.getBrand());
holder.textg_ir.setText(cars.getGir());
holder.textsea_ts.setText(cars.getSeatsWithLabel());
Glide.with(holder.img.getContext()).load(cars.getImgurl()).into(holder.img);
}
#NonNull
#Override
public myViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.car_items,
parent, false);
return new myViewHolder(view);
}
public class myViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
CircleImageView img;
TextView textpri_ce,textbra_nd,textg_ir,textsea_ts;
public myViewHolder( View itemView) {
super(itemView);
img = (CircleImageView) itemView.findViewById(R.id.img1);
textpri_ce= (TextView) itemView.findViewById(R.id.textprice);
textbra_nd= (TextView)itemView.findViewById(R.id.textbrand);
textg_ir= (TextView)itemView.findViewById(R.id.textgir);
textsea_ts= (TextView)itemView.findViewById(R.id.textseats);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
onListItemClick.onItemClick(getItem(getAdapterPosition()),getAdapterPosition());
}
}
public interface OnListItemClick{
void onItemClick(Cars snapshot, int position);
}
}
Booking class
public class booking_activity extends AppCompatActivity implements FirestoreAdapter.OnListItemClick{
FirebaseFirestore firebaseFirestore;
RecyclerView mFirestoreList;
private ArrayList<Cars> listCars;
Context context;
FirestoreAdapter adapter;
private static final String TAG = "booking_activity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_booking);
// calling function adapetWithRecycle() which contains adapter and recycleview----
adapetWithRecycle();
//--------------------------------------------------------------------------
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.toolbar_title);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// here we call functions for all the spinners to Find Id and set adapters.
priceSpinner();
brandSpinner();
girSpinner();
seatsSpinner();
}
// onStop and onStart for recycle view
#Override public void onStart() {
super.onStart();
adapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
protected void adapetWithRecycle() {
firebaseFirestore = FirebaseFirestore.getInstance();
mFirestoreList = findViewById(R.id.recview);
Query query = firebaseFirestore.collection("cars");
FirestoreRecyclerOptions<Cars> options = new FirestoreRecyclerOptions.Builder<Cars>()
.setQuery(query, Cars.class)
.build();
adapter = new FirestoreAdapter(options,this);
mFirestoreList.setHasFixedSize(true);
mFirestoreList.setLayoutManager(new LinearLayoutManager(this));
mFirestoreList.setAdapter(adapter);
}
#Override
public void onItemClick(Cars snapshot, int position) {
Intent intent = new Intent(booking_activity.this, Reservation.class);
// intent.putExtra(Reservation.BOOKING_INFORMATION, listCars.get(position));
//intent.putExtra(Reservation.BOOKING_INFORMATION, listCars);
startActivity(intent);
Log.d("ITEM_CLICK","clicked the item: " + position+ " Gear is : " + snapshot.getGir()+
" Price is: "+ snapshot.getPrice()+" , Brand is "+ snapshot.getBrand()+" ,Seats: "+
snapshot.getSeats());
inside your onBindViewHolder() in setText() add listcars.get(position).getPriceWithLable() apply it to the rest of your TextViews
ex. holder.textpri_ce.setText(listcars.get(position).getPriceWithLable());
Related
I want to make a interface which user can add its data to activity and after its must be show on in recyclerview, I used manual data adding in code it's working but I want to user must be able to store its data on application and can see & modified.
I used put_extra function but is not working.`
[code for put_put Extra]
Here is adapter class:
public class Handler_Adapter extends RecyclerView.Adapter<Handler_Adapter.MyViewHolders> {
ArrayList<MyMode> myModel;
View ReceivedImages;
Context context;
public Handler_Adapter (Context context, ArrayList<MyMode> list, View myImagesF){
this.context=context;
this.myModel=list;
ReceivedImages=myImagesF;
}
#Override
public MyViewHolders onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater= LayoutInflater.from(context);
View view = inflater.inflate(R.layout.row,parent,false);
return null;
}
#Override
public void onBindViewHolder(#NonNull MyViewHolders holder, int position) {
holder.EName.setText(myModel.get(position).getLangName());
holder.E_Des.setText(myModel.get(position).getLangName());
holder.Price.setText(myModel.get(position).getLangName());
holder.itemsSize.setText(myModel.get(position).getLangName());
holder.myImages.setImageResource(myModel.get(position).getLangLogo());
holder.rows.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
#Override
public int getItemCount() { return myModel.size();
}
public class MyViewHolders extends RecyclerView.ViewHolder {
TextView EName,E_Des,Price,itemsSize;
ImageView myImages;
RelativeLayout rows;
public MyViewHolders(#NonNull View itemView) {
super(itemView);
EName=itemView.findViewById(R.id.EName);
E_Des=itemView.findViewById(R.id.E_Des);
ReceivedImages=itemView.findViewById(R.id.my_Imaage);
Price=itemView.findViewById(R.id.E_Price);
itemsSize=itemView.findViewById(R.id.itemsSize);
rows=itemView.findViewById(R.id.rows);
}
}
}
Here is UserActivity
public class AddMenu extends AppCompatActivity {
public static final String TEXT_TO_SEND="com.example.EMenu.TEXT_TO_SEND";
private ImageView mImageView;
private EditText EName,E_Des,E_Price,E_size;
private Button Import;
private String TextToSend;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_menu);
mImageView=findViewById(R.id.imageFilterView);
mImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ImagePicker.Companion.with(AddMenu.this)
.crop()
// .cropOval()
.compress(1024)
.maxResultSize(1080,1080)
.start();
}
});
Import=findViewById(R.id.Import);
EName=findViewById(R.id.EName);
E_Des=findViewById(R.id.E_Des);
E_Price=findViewById(R.id.E_Price);
E_size=findViewById(R.id.E_size);
Import.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextToSend=EName.toString().toUpperCase().trim();
TextToSend=E_Des.toString().toLowerCase().trim();
TextToSend=E_Price.toString().toUpperCase().trim();
TextToSend=E_size.toString().toUpperCase().trim();
gotoActivity();
}
});
}
private void gotoActivity() {
Intent on=new Intent(AddMenu.this,Mainpage.class);
on.putExtra("Selected_Image",R.id.imageFilterView);
on.putExtra(TEXT_TO_SEND, String.valueOf(EName).toUpperCase());
on.putExtra(TEXT_TO_SEND, String.valueOf(E_Des));
on.putExtra(TEXT_TO_SEND, String.valueOf (E_Price).toUpperCase());
on.putExtra(TEXT_TO_SEND, String.valueOf(E_size).toUpperCase());
startActivity(on);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Uri uri=data.getData();
mImageView.setImageURI(uri);
}
**Main page code_ at there is
used adding images manually
but I want to its added it by user activity**
public class Mainpage extends AppCompatActivity {
RecyclerView Recycler;
ArrayList<MainModels> mainModel;
MainAdapter mainAdapter;
Handler_Adapter mHandler_adapter;
ArrayList<MyMode> myModel;
SearchView SearchView;
ImageView mImageView,myImagesF;
////////////////////////////////////////////////////////: Images resource file..
String s1[],s2[],s3[],s4[];
/////////////////////////////////////////////////////.......... continue..............
RecyclerView recyclerView;
NavigationView click;
private FirebaseAuth fAuth;
private String currentUserId;
private DatabaseReference databaseReference;
Button Navigation;
//Menu Account,Settings,Restaurant,Menu;
private DrawerLayout mDrawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainpage);
myModel = new ArrayList<>();
int images[]={R.drawable.delfina_cocciardi_ndpbk_d_03m_unsplash,R.drawable.ic_launcher_foreground,
R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground,R.drawable.ic_launcher_foreground };
/////////////////////////////////////.......Goat it data from input data fragment
mImageView=findViewById(R.id.my_Imaage);
Bundle bundle=getIntent().getExtras();
if (bundle != null){
int res_image=bundle.getInt("Selected_Image");
// mImageView.setImageResource(res_image);
}
//////////////////////////////////////////////////................!!!
recyclerView=findViewById(R.id.recyclerView);
Intent intent=getIntent();
s1=getResources().getStringArray(R.array.Menu_items);
s2=getResources().getStringArray(R.array.Description);
s3=getResources().getStringArray(R.array.Prices);
s4=getResources().getStringArray(R.array.Prices);
Adapter mAdapter=new Adapter(this,s1,s2,s3,s4,images);
recyclerView.setAdapter(mAdapter);
recyclerView.setLayoutManager(new GridLayoutManager(this,2,RecyclerView.VERTICAL,false));
/*
recyclerView=findViewById(R.id.recyclerView);
Integer[] langLogos={R.id.imageFilterView};
Intent intent=getIntent();
String langNames=intent.getStringExtra(AddMenu.TEXT_TO_SEND);
if (langNames!=null){
TextView textView=findViewById(R.id.Menu_Name);
textView.setText(langNames);
}
String langPrice=intent.getStringExtra(AddMenu.TEXT_TO_SEND);
if (langPrice!=null){
TextView textView2=findViewById(R.id.Description_name);
textView2.setText(langPrice);
}
String langDisc=intent.getStringExtra(AddMenu.TEXT_TO_SEND);
if (langDisc!=null){
TextView textView3=findViewById(R.id.Price);
textView3.setText(langDisc);
}
String langSize=intent.getStringExtra(AddMenu.TEXT_TO_SEND);
if (langSize!=null){
TextView textView4=findViewById(R.id.itemsSize);
textView4.setText(langSize);
}
myModel=new ArrayList<>();
MyMode so=new MyMode(langLogos,langNames,langDisc,langPrice,langSize);
myModel.add(so);
GridLayoutManager gridLayoutManager= new GridLayoutManager(Mainpage.this,2,GridLayoutManager.VERTICAL,false);
recyclerView.setLayoutManager(gridLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
mHandler_adapter=new Handler_Adapter(Mainpage.this,myModel,myImagesF);
recyclerView.setAdapter(mHandler_adapter);
*/
// for (int j=0;j<langLogos.length;j++){
// }
Here is what you can do.
Create a private list in your activity.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myModel = new ArrayList<>();
}
Now change the constructor of your adapter.
public Handler_Adapter(Context context, ArrayList<MyModels> list, ...)
{
...
this.myModelList = list;
...
}
Now when you modify "myModel" than this is visible to your adapter.
All you have to do is to call the following once you modify it.
mHandlerAdapter.notifyDataSetChanged();
I already know how a recycler viewer works and a good understanding of interface listeners but I'm wondering why is it that we need to pass a listener to my recycler adapter called mAdapter = new GreenAdapter. In my MainActivity.java I have something like this
public class MainActivity extends AppCompatActivity implements GreenAdapter.ListItemClickListener {
private static final int NUM_LIST_ITEMS = 100;
private GreenAdapter mAdapter;
private RecyclerView mNumbersList;
private Toast mToast;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new GreenAdapter(NUM_LIST_ITEMS, this);
mNumbersList.setAdapter(mAdapter);
}
#Override
public void onListItemClick(int clickedItemIndex) {
String toastMessage = "Item #" + clickedItemIndex + "clicked.";
mToast = Toast.makeText(this, toastMessage, Toast.LENGTH_LONG);
mToast.show();
}
In my GreenAdapter.java I understand that my Viewholder constructor will invoke my custom interface called ListItemClickedListener passing in the position of the clicked view and from there I can implement the code for what will happen when the user clicks it through my onListItemClick abstract method
public class GreenAdapter extends RecyclerView.Adapter<GreenAdapter.NumberViewHolder> {
private static final String TAG = GreenAdapter.class.getSimpleName();
private int mNumberItems;
final private ListItemClickListener mOnClickListener;
public interface ListItemClickListener{
void onListItemClick(int clickedItemIndex);
}
public GreenAdapter(int numberOfItems, ListItemClickListener listener) {
mNumberItems = numberOfItems;
mOnClickListener = listener;
}
#Override
public NumberViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
Context context = viewGroup.getContext();
int layoutIdForListItem = R.layout.number_list_item;
LayoutInflater inflater = LayoutInflater.from(context);
boolean shouldAttachToParentImmediately = false;
View view = inflater.inflate(layoutIdForListItem, viewGroup, shouldAttachToParentImmediately);
NumberViewHolder viewHolder = new NumberViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(NumberViewHolder holder, int position) {
Log.d(TAG, "#" + position);
holder.bind(position);
}
#Override
public int getItemCount() {
return mNumberItems;
}
class NumberViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView listItemNumberView;
public NumberViewHolder(View itemView) {
super(itemView);
listItemNumberView = (TextView) itemView.findViewById(R.id.tv_item_number);
itemView.setOnClickListener(this);
}
void bind(int listIndex) {
listItemNumberView.setText(String.valueOf(listIndex));
}
#Override
public void onClick(View view) {
int clickedPosition = getAdapterPosition();
mOnClickListener.onListItemClick(clickedPosition);
}
}
I'm just confuse why we need to pass this in my recycler adapter
By passing this you are actually passing the object of MainActivity.java which is implementing GreenAdapter.ListItemClickListener interface. Now when your user clicks on item you can let your activity know what item in the list was clicked by the user the rest of Business logic will then be written in the onListItemClick() method of MainActivity.
The Error
'FirebaseRecyclerAdapter(com.firebase.ui.database.FirebaseRecyclerOptions<com.munopa.revise.Files>)'
in 'com.firebase.ui.database.FirebaseRecyclerAdapter' cannot be
applied to '(java.lang.Class<com.munopa.revise.Files>, int,
java.lang.Class<com.munopa.revise.MainActivity.FilesViewHolder>,
com.google.firebase.database.DatabaseReference)'
My Main Activity
public class MainActivity extends AppCompatActivity {
private EditText mSearchField;
private RecyclerView mFileList;
private FloatingActionButton mSearchButton;
private DatabaseReference mFilesDatabase;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFilesDatabase = FirebaseDatabase.getInstance().getReference().child("Files");
mSearchField = findViewById(R.id.search_bar_input);
mFileList = findViewById(R.id.file_list);
mSearchButton = findViewById(R.id.btn_search);
mSearchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
firebaseFileSearch();
}
});
}
private void firebaseFileSearch() {
FirebaseRecyclerAdapter<Files, FilesViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Files, FilesViewHolder>(
Files.class,
R.layout.paper_card,
FilesViewHolder.class,
mFilesDatabase
) {
#Override
protected void onBindViewHolder(#NonNull FilesViewHolder holder, int position, #NonNull Files model) {
}
#NonNull
#Override
public FilesViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return null;
}
};
}
public class FilesViewHolder extends RecyclerView.ViewHolder{
View mView;
public FilesViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
}
}
This is your problem (and where you are getting the error):
FirebaseRecyclerAdapter<Files, FilesViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<Files, FilesViewHolder>(
Files.class,
R.layout.paper_card,
FilesViewHolder.class,
mFilesDatabase
)
You are calling the constructor of FirebaseRecyclerAdapter with 4 parameters, but it only expects 1 parameter of type FirebaseRecyclerOptions<Files>.
Refer to https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md for more details about how to set this up.
I'm currently trying to implement a Firestore Recycler Adapter into my Android Studio Project but when I when I try to enter the activity with the recyclerView the app crashes. When I use the debugger it crashes when it reaches the .build() method of FireStoreRecyclerOptions. Is it something I'm doing, and if not, is there a workaround?
Here is the code for for the implementation of the RecyclerView:
public class SecurityCurrentRequests extends AppCompatActivity {
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference requestRef = db.collection("Request");
private Adapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_security_current_requests);
setUpRecycler();
}
public void setUpRecycler()
{
Query query = requestRef.orderBy("priority", Query.Direction.ASCENDING);
FirestoreRecyclerOptions<Request> options = new FirestoreRecyclerOptions.Builder<Request>()
.setQuery(query, Request.class)
.build();
adapter = new Adapter(options);
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
#Override
protected void onStart(){
super.onStart();
adapter.startListening();
}
#Override
protected void onStop(){
super.onStop();
adapter.stopListening();
}
}
and this is the Adapter Class:
public class Adapter extends FirestoreRecyclerAdapter<Request, Adapter.RequestViewHolder> {
public Adapter(#NonNull FirestoreRecyclerOptions<Request> options){
super(options);
}
#NonNull
#Override
public RequestViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
//create new instance of the ViewHolder
// using request_cardview custom layout
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.request_cardview, parent, false);
return new RequestViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull RequestViewHolder holder, int position, #NonNull Request requestObject) {
String firstText = requestObject.getName();
String secondText = requestObject.getMessage();
int thirdText = requestObject.getPriority();
String fourthText = requestObject.getCurrentDate();
holder.text1.setText(firstText);
holder.text2.setText(secondText);
holder.text3.setText(thirdText);
holder.text4.setText(fourthText);
}
public class RequestViewHolder extends RecyclerView.ViewHolder{
private TextView text1, text2, text3, text4;
RequestViewHolder(View itemView){
super(itemView);
text1 = itemView.findViewById(R.id.name);
text2 = itemView.findViewById(R.id.description);
text3 = itemView.findViewById(R.id.text_view_priority);
text4 = itemView.findViewById(R.id.date);
}
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
I'm creating an app that display videos in a recycler view , I already can storage the videos in Database Firestore , but I'm getting this error when I run my app:
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at com.example.carlos.trendy.FeedRecyclerAdapter.getItemCount(FeedRecyclerAdapter.java:35)
My code:
public class FeedRecyclerAdapter extends RecyclerView.Adapter<FeedRecyclerAdapter.ViewHolder> {
public List<Feed>feedList;
public FeedRecyclerAdapter(List<Feed> feedList){
this.feedList = feedList;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.feed_row, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
String desc_data = feedList.get(position).getDesc();
holder.setDesc(desc_data);
}
#Override
public int getItemCount() {
return feedList.size();///ERROR
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView desc;
private View mView;
public ViewHolder(View itemView) {
super(itemView);
mView=itemView;
}
public void setDesc(String descText){
desc = mView.findViewById(R.id.post_desc);
desc.setText(descText);
}
}
}
FeedActivity:
public class FeedActivity extends AppCompatActivity {
private Context mContext = FeedActivity.this;
private static final int ACTIVITY_NUM = 0;
private FirebaseFirestore firebaseFirestore;
private RecyclerView mFeedList;
private List<Feed> feedList;
private FeedRecyclerAdapter feedRecyclerAdapter;
public void videoPicker(View view){
Intent intent = new Intent(FeedActivity.this, postActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feed);
mFeedList = this.findViewById(R.id.feed_list);
feedRecyclerAdapter = new FeedRecyclerAdapter(feedList);
mFeedList.setLayoutManager(new LinearLayoutManager(this));
mFeedList.setAdapter(feedRecyclerAdapter);
feedList = new ArrayList<>();
firebaseFirestore = FirebaseFirestore.getInstance();
firebaseFirestore.collection("Posts").addSnapshotListener(new EventListener<QuerySnapshot>() {
#Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
for (DocumentChange documentChange: documentSnapshots.getDocumentChanges()){
if (documentChange.getType() == DocumentChange.Type.ADDED){
Feed feed = documentChange.getDocument().toObject(Feed.class);
feedList.add(feed);
feedRecyclerAdapter.notifyDataSetChanged();
}
}
}
});
setupBottomNavigationView();
}
private void setupBottomNavigationView() {
BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavViewBar);
BottomNavigationViewHelper.enableNavigation(mContext, bottomNavigationView);
Menu menu = bottomNavigationView.getMenu();
MenuItem menuItem = menu.getItem(ACTIVITY_NUM);
menuItem.setChecked(true);
}
#Override
protected void onStart() {
super.onStart();
}
public static class FeedViewHolder extends RecyclerView.ViewHolder {
View mView;
public FeedViewHolder(View itemView) {
super(itemView);
itemView = mView;
}
public void setDesc(String desc){
TextView post_desc = mView.findViewById(R.id.post_desc);
post_desc.setText(desc);
}
}
}
Can anyone help me?
You initialize the adapter here:
feedRecyclerAdapter = new FeedRecyclerAdapter(feedList);
but feedList is null.
You must initialize it
Move all these lines:
feedRecyclerAdapter = new FeedRecyclerAdapter(feedList);
mFeedList.setLayoutManager(new LinearLayoutManager(this));
mFeedList.setAdapter(feedRecyclerAdapter);
after this line:
feedList = new ArrayList<>();