Reputation: 101
Hi ive been going through questions and found a solution to pass a string from an activity (not the fragment activity) to a fragment. i want to pass a document ID on click from RestaurantsList.class to Mainsfragment.class, inside my onclick.
The error message i get is the following:
Attempt to invoke virtual method 'android.content.Intent android.app.Activity.getIntent()' on a null object reference
here is my code for the activity:
package com.example.hostapp;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RelativeLayout;
import com.example.hostapp.Adapters.RestaurantAdapter;
import com.example.hostapp.Adapters.categoryCardAdapter;
import com.example.hostapp.MenuFragments.MainsFragment;
import com.example.hostapp.Models.Restaurant;
import com.example.hostapp.Models.categoryCard;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
public class RestaurantList extends AppCompatActivity {
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference restaurantref = db.collection("restaurants");
private RestaurantAdapter adapter;
String categoryid = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_restaurant_list);
setUpRecyclerView();
}
private void setUpRecyclerView() {
//Get intent
if (getIntent() != null)
categoryid = getIntent().getStringExtra("categoryid");
if (!categoryid.isEmpty() && categoryid != null) {
Query query = restaurantref.whereEqualTo("categoryid", categoryid).orderBy("name");
final FirestoreRecyclerOptions<Restaurant> options = new FirestoreRecyclerOptions.Builder<Restaurant>().setQuery(query, Restaurant.class).build();
adapter = new RestaurantAdapter(options);
RecyclerView recyclerView = findViewById(R.id.restaurant_recycler);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
adapter.setOnItemClickListerner(new RestaurantAdapter.OnItemClickListener() {
@Override
public void onItemClick(DocumentSnapshot documentSnapshot, int position) {
Restaurant restaurant = documentSnapshot.toObject(Restaurant.class);
Intent foodlist = new Intent(RestaurantList.this, Foodlist.class);
Bundle bundle = new Bundle();
bundle.putString("restaurantid", documentSnapshot.getId());
MainsFragment m4 = new MainsFragment();
m4.setArguments(bundle);
//foodlist.putExtra("restaurantid", documentSnapshot.getId());
startActivity(foodlist);
}
});
}
}
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
}
here is my code for the fragment:
package com.example.hostapp.MenuFragments;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.hostapp.Adapters.FoodAdapter;
import com.example.hostapp.Models.FoodModel;
import com.example.hostapp.R;
import com.example.hostapp.RestaurantList;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
public class MainsFragment extends Fragment {
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private FoodAdapter adapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_mains, container, false);
String restaurantNumber = getArguments().getString("restaurantid");
Query menuref = db.collectionGroup("Foods").whereEqualTo("menuid", restaurantNumber);
FirestoreRecyclerOptions<FoodModel> options = new FirestoreRecyclerOptions.Builder<FoodModel>().
setQuery(menuref, FoodModel.class)
.build();
adapter = new FoodAdapter(options);
RecyclerView recyclerView = view.findViewById(R.id.recycler_mains);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
recyclerView.setAdapter(adapter);
return view;
}
private void setUpRecyclerView() {
}
@Override
public void onStart() {
super.onStart();
adapter.startListening();
}
@Override
public void onStop() {
super.onStop();
adapter.stopListening();
}
}
Upvotes: 0
Views: 67
Reputation: 535
I had the same experience like this. Here is how I handled it:
1, Pass your data from RestaurantList activity to your foodlist activity via intent.putExtra();
Intent foodlist = new Intent(RestaurantList.this, Foodlist.class);
foodlist.putExtra("restaurantid", documentSnapshot.getId());
startActivity(foodlist);
2, Get the value by getIntent() from Foodlist activity;
String id = getIntent().getStringExtra("restaurantid");
3, Pass your data to the Adapter where you are initializing the adapter. (sth like this :)
YourAdapter adapter = new YourAdapter(getSupportFragmentManager(), tabLayout.getTabCount(),id);
4, Get the data in constractor of adapter (you have to create a global variable and fill it in constructor) and create a bundle in getItem() method and pass the data to it and set arguments to the fragment (like below code)
public class YourAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
String id;
public AdapterPagerShift(FragmentManager fm, int NumOfTabs, String id) {
super(fm);
this.mNumOfTabs = NumOfTabs;
this.id = id;
}
@Override
public Fragment getItem(int position) {
Bundle bundle = new Bundle();
bundle.putString("restaurantid", documentSnapshot.getId());
switch (position) {
case 0:
MainsFragment main = new MainsFragment();
main.setArguments(bundle);
return main;
case 1:
//...
case 2:
//...
default:
return null;
}
}
@Override
public int getCount() {
return mNumOfTabs;
}
}
5, Get the data by using below code in your MainsFragment class :
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String id = getArguments().getString("restaurantid");
}
Hope it helps!
Upvotes: 1