Here's the case. I am trying to develop an Android app that will display a list of restaurant using CardView. Each time the user click a restaurant, it will display a screen that contains 3 fragments with swipe view - DetailsFragment, MenuFragment and TableBookingFragment.
I am a novice in Android development so I am not sure what should I do.
Initially I thought of starting intent for different restaurant the user clicked. But in that case, I would have to create so many activities for different restaurant. Let say I have 10 restaurant in my list, I would have to build 10 activities that describe different details and menu (but using the same layout).
Is there any better way to display details and menu for different restaurant (without having to create so many activities)?
Do let me know if I did not describe my question clear enough.
Thanks in advance.
Restaurant.java
public class Restaurant extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_restaurant);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager){
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new details(), "Details");
adapter.addFragment(new menu(), "Menu");
adapter.addFragment(new tableBooking(), "Table booking");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter{
private final List<Fragment> fragmentList = new ArrayList<>();
private final List<String> fragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager){
super(manager);
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
public void addFragment (Fragment fragment, String title){
fragmentList.add(fragment);
fragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return fragmentTitleList.get(position);
}
}
}
RestaurantListAdapter.java
public class RestaurantListAdapter extends RecyclerView.Adapter<RestaurantListAdapter.ViewHolder> {
List<RestaurantList_Item> items;
private static ClickListener clickListener;
private Context context;
public RestaurantListAdapter(){
super();
items = new ArrayList<RestaurantList_Item>();
// POSITION 1
RestaurantList_Item restaurant = new RestaurantList_Item();
restaurant.setrImage(R.mipmap.img_anjappar);
restaurant.setrName("Anjappar Indian Chettinad Restaurant");
restaurant.setrCuisine("Indian");
items.add(restaurant);
// POSITION 2
restaurant = new RestaurantList_Item();
restaurant.setrImage(R.mipmap.img_morganfield);
restaurant.setrName("Morganfield");
restaurant.setrCuisine("Western");
items.add(restaurant);
// POSITION 3
restaurant = new RestaurantList_Item();
restaurant.setrImage(R.mipmap.img_nandos);
restaurant.setrName("Nando's");
restaurant.setrCuisine("Western");
items.add(restaurant);
// POSITION 4
restaurant = new RestaurantList_Item();
restaurant.setrImage(R.mipmap.img_shellout);
restaurant.setrName("Shellout");
restaurant.setrCuisine("Seafood");
items.add(restaurant);
// POSITION 5
restaurant = new RestaurantList_Item();
restaurant.setrImage(R.mipmap.img_beyond);
restaurant.setrName("Beyond Veggie");
restaurant.setrCuisine("Vegetarian");
items.add(restaurant);
// POSITION 6
restaurant = new RestaurantList_Item();
restaurant.setrImage(R.mipmap.img_myelephant);
restaurant.setrName("My Elephant");
restaurant.setrCuisine("Thai");
items.add(restaurant);
// POSITION 7
restaurant = new RestaurantList_Item();
restaurant.setrImage(R.mipmap.img_padangkota);
restaurant.setrName("Nasi Kandar Padang Kota");
restaurant.setrCuisine("Mamak");
items.add(restaurant);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.cardview_restaurant, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
RestaurantList_Item restaurant = items.get(position);
viewHolder.resImage.setImageResource(restaurant.getrImage());
viewHolder.restaurantName.setText(restaurant.getrName());
viewHolder.cuisine.setText(restaurant.getrCuisine());
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context context = v.getContext();
// Toast.makeText(context, "Item clicked with position " +position, Toast.LENGTH_LONG).show();
}
});
}
@Override
public int getItemCount() {
return items.size();
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public ImageView resImage;
public TextView restaurantName;
public TextView cuisine;
public ViewHolder(final View itemView) {
super(itemView);
resImage = (ImageView) itemView.findViewById(R.id.resImage);
restaurantName = (TextView) itemView.findViewById(R.id.restaurantName);
cuisine = (TextView) itemView.findViewById(R.id.cuisine);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
clickListener.onItemClick(getAdapterPosition(), v);
}
}
public void setOnItemClickListener (ClickListener clickListener){
RestaurantListAdapter.clickListener = clickListener;
}
public interface ClickListener{
void onItemClick (int position, View v);
}
}
details.java - (First fragment in Restaurant.java)
public class details extends Fragment {
public details() {
// Required empty public constructor
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_details, container, false);
}
}
menu.java (Second fragment)
public class menu extends Fragment {
public menu() {
// Required empty public constructor
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_menu, container, false);
}
}
fragment_details.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.jenna.iwanteat.fragments.details">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/resName"
android:text="Restaurant Name"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
/>
<TextView
android:id="@+id/resAddress"
android:text="Restaurant Address"
android:textSize="12sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="45dp"
android:layout_marginLeft="20dp"
/>
<TextView
android:id="@+id/resPhone"
android:text="Restaurant Phone"
android:textSize="12sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="65dp"
android:layout_marginLeft="20dp"
/>
<TextView
android:id="@+id/resCuisine"
android:text="Types of Cuisine"
android:textSize="12sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="85dp"
android:layout_marginLeft="20dp"
/>
<TextView
android:id="@+id/resPriceRange"
android:text="Price Range"
android:textSize="12sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="105dp"
android:layout_marginLeft="20dp"
android:textStyle="italic"
/>
</FrameLayout>
RestaurantModel.java
public class RestaurantModel {
// variable main photo
private int mainImage;
//variables for details fragment
private String resName, resAddress, resPhone, resCuisine, resPriceRange;
//variables for menu fragment
private int foodPhoto;
private String foodPrice, foodDescription;
//get set methods
public int getMainImage(){
return mainImage;
}
public void setMainImage(int mainImage){
this.mainImage = mainImage;
}
public String getResName(){
return resName;
}
public void setResName(String resName){
this.resName = resName;
}
public String getResAddress(){return resAddress;}
public void setResAddress(String resAddress){this.resAddress = resAddress;}
public String getResPhone(){return resPhone;}
public void setResPhone(String resPhone){this.resPhone = resPhone;}
public String getResCuisine(){return resCuisine;}
public void setResCuisine(String resCuisine){this.resCuisine = resCuisine;}
public String getResPriceRange(){return resPriceRange;}
public void setResPriceRange(String resPriceRange){this.resPriceRange = resPriceRange;}
public int getFoodPhoto(){return foodPhoto;}
public void setFoodPhoto(int foodPhoto){this.foodPhoto = foodPhoto;}
public String getFoodDescription(){return foodDescription;}
public void setFoodDescription(String foodDescription){this.foodDescription = foodDescription;}
public String getFoodPrice(){return foodPrice;}
public void setFoodPrice(String foodPrice){this.foodPrice = foodPrice;}
}
In an Intent you have extras. In the extras you can put ints, doubles, longs, strings, any object that implements Serializable or Parcelable, or lists of any of those things. So you either pass it all the data via extras, or you pass it some way of getting the data (like a filename or url) via the extras.
You need only one Activity for the restaurant (in addition to your Restaurant List Activity). When you open the second Activity on clicking the restaurant from your list you need to pass the corresponding data to identify the restaurant in the second activity. In your second activity display data according to the incoming data.
For sending data to Activity you need to use Intents. Check here http://developer.android.com/training/basics/firstapp/starting-activity.html to understand how it is done.
There you can find these codes which you need to modify for your needs
When starting activity
On second Activity
If you need more help please add some code you are using right now.
you want to show different data with the same layout why you didn't add send just the data for these fragmnet
recieve intent
or put data in bundle and send via fragment if you using cardview in fragment send data
recieve it in fragments