I'm trying not to overwhelm the user by collecting a lot of data in a single screen so I have used a 'Viewpager' with 3 XML layouts. My button is in the last slide and I want to store all the data I've collected into Firestore. I'm having trouble getting the input from all slides all in one activity. What's the solution to my problem.
Here's my onClickListener code
// Adding property details through onclick listener
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String location = Location.getText().toString().trim();
String surbab = Surbab.getText().toString().trim();
String city = City.getText().toString().trim();
String country = Country.getText().toString().trim();
String Name = name.getText().toString().trim();
Double Valuation = Double.valueOf(valuation.getText().toString().trim());
boolean Property_type = Boolean.parseBoolean(type.getText().toString().trim());
String No_of_units = number_of_units.getText().toString().trim();
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
if (!validateInputs(location, surbab, city, country, Name, String.valueOf(Valuation), No_of_units)) {
CollectionReference dbTenants = db.collection("Users");
Property property = new Property(
location,
surbab,
city,
country,
Name,
Valuation,
Property_type,
No_of_units
);
dbTenants.document(uid).collection("property").add(property)
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
Toast.makeText(PropertyActivity.this, "Tenant Added", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(PropertyActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
});
For the property class, I have this
package com.example.MyLandlordStudio;
public class Property {
private String Locaation;
private String Surbab;
private String City;
private String Country;
private String name;
private Double valuation;
private boolean type;
private String number_of_units;
public Property() {
}
public Property(String locaation, String surbab, String city, String country, String name, Double valuation, boolean type, String number_of_units) {
Locaation = locaation;
Surbab = surbab;
City = city;
Country = country;
this.name = name;
this.valuation = valuation;
this.type = type;
this.number_of_units = number_of_units;
}
public String getLocaation() {
return Locaation;
}
public String getSurbab() {
return Surbab;
}
public String getCity() {
return City;
}
public String getCountry() {
return Country;
}
public String getName() {
return name;
}
public Double getValuation() {
return valuation;
}
public boolean isType() {
return type;
}
public String getNumber_of_units() {
return number_of_units;
}
}
Here's a visual representation of what I mean https://i.stack.imgur.com/8R8Gj.png . When I press the green button I want to add all data from all slides into Firestore. Open the link for the image. Thanks in advance
Here's my adapter
import android.content.Context;
import android.os.Build;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
public class SliderAdapter extends PagerAdapter {
Context context;
LayoutInflater layoutInflater;
public SliderAdapter(Context context) {
this.context = context;
}
@Override
public int getCount() {
return 3;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
LayoutInflater inflater = (LayoutInflater) container.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0:
resId = R.layout.address_slider_layout;
break;
case 1:
resId = R.layout.description_slider_layout;
break;
case 2:
resId = R.layout.confirm_slider_layout;
break;
}
View view = inflater.inflate(resId, null);
((ViewPager) container).addView(view, 0);
return view;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
((ViewPager) container).removeView((View) object);
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == ((View) object);
}
@Nullable
@Override
public Parcelable saveState() {
return null;
}
}
@svi.data
You can add one method in base activity/fragment which hold the object which contains this values.
for example BaseFragment is parent fragment of viewpager
BaseFragment.java
//create object of property
Property property=new Property();
//this method can be accessed in all the viewpager
public Property getPropertyValues(){
return property;
}
ViewPager1.java (first viewpager)
//in onclick button or swipe add this
BaseFragment basefragment=(BaseFragment)ViewPager1.this.getParentFragment();
Property obj=basefragment.getPropertyValues();
obj.setLocation("your value");
...
...
//same thing you have to do in viewpager2
viewpager3.java (last viewpager)
BaseFragment basefragment=(BaseFragment)ViewPager1.this.getParentFragment();
Property obj=basefragment.getPropertyValues();
String location=obj.getLocation();
...
...
...
//here you will get all the values
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.