How to update edittext whenever a quantity is added in android studio - java

I am working on android studio. I am using fragments to make the app workflow. The app is basically for order taking. One customer can have more than one order. So for it, I have used RecyclerView. Below is my GUI
What do I want to do?
I want to update my Total Qty and Order Value in such a way that whenever the user enters the quantity both edittexts should be updated. For example, in the above image, I searched for one product, its rate is 287.30 and when I entered the quantity 5 then Total Qty: 5 and Order Value: 287.30. After that, I select another product and enter its quantity 8 then both edit text should be updated like Total Qty: 13 and Order Value: 509.15, and when I remove any product then it should subtract the total quantity and order value.
The Total Qty: and Order Value: is in my main fragment. Below is my code
Fragment Layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/products"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:focusable="true" />
</LinearLayout>
<LinearLayout
android:id="#+id/product_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10sp"
android:orientation="horizontal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tv_product">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total Qty:" />
<EditText
android:id="#+id/total_qty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center_horizontal"
android:hint=""
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Order Value :" />
<EditText
android:id="#+id/order_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center_horizontal"
android:hint=""
android:inputType="none" />
</LinearLayout>
</LinearLayout>
Fragment Code
// created getter setters
public EditText totalQty,totalOrder;
public EditText getTotalQty(){
return totalQty;
}
public void setTotalQty(EditText et){
this.totalQty = et;
}
public EditText getTotalOrder(){return totalOrder;}
public void setTotalOrder(EditText ett){this.totalOrder=ett;}
addProduct.setOnClickListener(v -> {
mProducts.add(new ProductDetail());
mProductsAdapter.setProducts(mProducts);
productsRecyclerView.smoothScrollToPosition(mProducts.size() - 1);
});
Product Layout
<AutoCompleteTextView
android:id="#+id/tv_product"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10sp"
android:gravity="start"
android:hint="Enter Product"
android:inputType="text"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="#+id/product_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10sp"
android:orientation="horizontal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tv_product">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:orientation="vertical">
<EditText
android:id="#+id/prod_qty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="left"
android:hint="Enter Quantity"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:orientation="vertical">
<EditText
android:id="#+id/prod_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="left"
android:hint="Prod Price"
android:inputType="numberDecimal" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:orientation="vertical">
<EditText
android:id="#+id/prod_specs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="left"
android:hint="Prod Specs"
android:inputType="none" />
</LinearLayout>
</LinearLayout>
<Button
android:id="#+id/btn_rmv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
android:text="Remove Product"
android:textColor="#color/white"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/product_infos" />
Product Adapter
View productView = inflater.inflate(R.layout.product_layout, parent, false);
ProductViewHolder viewHolder = new ProductViewHolder(productView);
viewHolder.tvProduct.setAdapter(prodAdapter);
viewHolder.tvProduct.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
#Override
public void afterTextChanged(Editable s) {
mProductManager = new ProductManager(context);
int position = viewHolder.getAdapterPosition();
if (position != -1) {
ProductDetail productDetail = mProductManager.getProductByPrdName(s.toString());
mProducts.get(position).setProductNameFull(s.toString());
viewHolder.price.setText(productDetail.getPrice());
viewHolder.spec.setText(productDetail.getProductSpec());
viewHolder.pgrp.setText(productDetail.getProductGroup());
viewHolder.ccode.setText(productDetail.getCompanyCode());
viewHolder.cname.setText(productDetail.getCompanyName());
viewHolder.pcode.setText(productDetail.getProductCode());
}
}
});
viewHolder.quantity.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
int position = viewHolder.getAdapterPosition();
if (position != -1) {
Integer val = null;
try {
val = Integer.valueOf(s.toString());
} catch (Exception e) {
}
mProducts.get(position).setQuantity(val);
}
}
});
viewHolder.removeBtn.setOnClickListener(v -> {
int position = viewHolder.getAdapterPosition();
if (position != -1) {
mProducts.remove(position);
notifyItemRemoved(position);
}
});
return viewHolder;
Update 1
Below is my Fragment Class
public class SurveyFormFragment extends Fragment {
.
.
.
.
private final LiveData<Integer> mQuantity = new MutableLiveData<>(0);
private final LiveData<Integer> mOrderValue = new MutableLiveData<>(0);
private ViewModel mViewModel;
private FragmentBinding mBinding;
#RequiresApi(api = Build.VERSION_CODES.M)
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d("myfragment", "onCreateView");
getActivity().setTitle(getString(R.string.title_new_survey));
mContext = getActivity();
if (view == null) {
view = inflater.inflate(R.layout.new_survey_form_layout, container, false);
mLinearLayout = view.findViewById(R.id.ll_prod);
this.initElements(view);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
this.initListeners(getActivity());
}
Bundle arguments = getArguments();
if (arguments != null && arguments.containsKey("booking_id")) {
isNewSurvey = false;
initSurveyData(arguments.getString("booking_id"));
this.updatingSurveyId = arguments.getString("booking_id");
}
}
Log.d("package", getActivity().getPackageName());
Log.d(TAG, Common.getCarrierName(getActivity()));
return view;
}
#Override
public void onStart() {
super.onStart();
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d("myfragment", "onActivityCreated");
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
Log.d("myfragment", "onCreate");
}
#Override
public void onDestroy() {
super.onDestroy();
Log.d("myfragment", "onDestroy");
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
//save now
savedInstanceState.putString("faisal_qayyum", consumerNameEditText.getText().toString());
super.onSaveInstanceState(savedInstanceState);
Log.d("myfragment", "onSaveInstanceState saving " + consumerNameEditText.getText().toString());
}
#Override
public void onViewStateRestored(Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
//restore now
Log.d("myfragment", "onViewStateRestored");
}
.
.
.
.
}
Error
How can I add and then update the values?
Any help would be highly appreciated.

I will disappoint you by not sharing code. I will share you a simple graphic of what you need to do.
[Fragment]: displays your list of orders. When an event is triggered (user tapped something or changed something), forwards this event to a ViewModel. Also Observes for the ViewModel data state.
[ViewModel]: exposes a series of functions to listen for user events (e.g. the user tapped this order, or the user changed the quantity of this order to NN, etc.). Also offers a LiveData (or Flow) with the State and Data your Fragment needs. Maybe even talks to the repository where this data originates from (where you save your Orders in your persistent or backend).
Interactions:
When the UI modified something, you tell the viewModel: "This data changed, the order X, now has a quantity of Y".
The ViewModel will process this, update the State of the screen, and send the new data via your Livedata/Flow that the Fragment observes.
When the Fragment receives this new data, it will update the UI accordingly, as that's its most important job.
This whole thing has been greatly documented in the official Android recommended architecture guides. I suggest you start from there instead of trying to fight the framework and making your life more complicated.

implement interface between adapter and fragment like this (it is just sodu to tell how you have to implement things also you have to check it out how to implement interfaces
===============================================================
public frgment extends Fragment implements GetValueInterface{
in onCreate function{
int totalquantity;
call this after adapter initialization
adapter.registergetValueInterface(this)
}
#Override Void setQuantity(Int _gotquantity){
_gotquantity+=_gotquantity;
totalquantityedittext.setText(_gotquantity,TextView.BufferType.EDITABLE)
}
}
and in adapter class
Adapter class
{
public GetValueInterface instance;
function registergetValueInterface(GetValueInterface obj){
instance=obj;
}
//and in quantity edittext text watcher
call instance.setQuantity(quanity)
}
like this you can minus the quantity as well on removal of item

Actually it's releated with Interactions like LiveData.
But rather than write a looot of code, simply I'm passing runnable to childs for call it when somethink changed :)
For example:
class Root{
...
public void addItem(){
ItemController cont = new ItemController(() -> updateSums());
...
}
public void updateSums(){
//TODO
}
}
class ItemController{
Runnable updater;
public ItemController(Runnable updater){
this.updater = updater;
}
public void onSomethingChanged(){
updater.run();
}
}

You should work with LiveData and DataBinding
In order to do so you activate it in your build.gradle file:
android {
...
buildFeatures {
dataBinding true
}
}
Then add the following code.
Add a ViewModel class:
public class ViewModel extends AndroidViewModel
{
private final MediatorLiveData<Integer> mTotalQuantity = new MediatorLiveData<>();
private final MediatorLiveData<Integer> mTotalOrder = new MediatorLiveData<>();
private final List<LiveData<Integer>> mOrders = new ArrayList<>();
private final List<LiveData<Integer>> mQuantities = new ArrayList<>();
public ViewModel(#NonNull Application application)
{
super(application);
mTotalOrder.setValue(0);
mTotalQuantity.setValue(0);
}
public LiveData<Integer> getTotal()
{
return mTotalOrder;
}
public LiveData<Integer> getQuantity()
{
return mTotalQuantity;
}
public void attachQuantity(LiveData<Integer> quantity)
{
mQuantities.add(quantity);
mTotalQuantity.addSource(quantity, (q) -> calculateQuantities());
}
public void detachQuantity(LiveData<Integer> quantity)
{
mQuantities.remove(quantity);
mTotalQuantity.removeSource(quantity);
calculateQuantities();
}
private void calculateQuantities()
{
int total = 0;
for (LiveData<Integer> quantity : mQuantities)
{
Integer value = quantity.getValue();
if (value != null)
{
total += value;
}
}
mTotalQuantity.setValue(total);
}
public void attachTotal(LiveData<Integer> total)
{
mOrders.add(total);
mTotalOrder.addSource(total, (q) -> calculateOrders());
}
public void detachTotal(LiveData<Integer> total)
{
mOrders.remove(total);
mTotalOrder.removeSource(total);
calculateOrders();
}
private void calculateOrders()
{
int total = 0;
for (LiveData<Integer> order : mOrders)
{
Integer value = order.getValue();
if (value != null)
{
total += value;
}
}
mTotalOrder.setValue(total);
}
}
In your fragment class add the following:
private LiveData<Integer> mQuantity = new MutableLiveData<>(0);
private LiveData<Integer> mOrderValue = new MutableLiveData<>(0);
private ViewModel mViewModel;
private FragmentBinding mBinding;
#Override
public void onCreate(#Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mViewModel = new ViewModelProvider(requireActivity()).get(ViewModel.class);
mViewModel.attachQuantity(mQuantity);
mViewModel.attachTotal(mOrderValue);
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState)
{
mBinding = DataBindingUtil.setContentView(this, R.layout.fragment);
mBinding.quantity = mQuantity;
mBinding.order_value = mOrderValue;
}
#Override
public void onDestroy()
{
super.onDestroy();
mViewModel.detachQuantity(mQuantity);
mViewModel.detachTotal(mOrderValue);
}
Add this to your product layout:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="quantity"
type="android.arch.lifecycle.MutableLiveData<Integer>" />
<variable
name="order_value"
type="android.arch.lifecycle.MutableLiveData<Integer>" />
</data>
...
<EditText
android:id="#+id/prod_qty"
// This does the magic
android:text="#{quantity}" />
...
<EditText
android:id="#+id/prod_price"
// And this of course...
android:text="#{order_value}" />
...
</layout>
And this to your fragment layout:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewmodel"
type="yourpackage.ViewModel" />
</data>
...
<EditText
android:id="#+id/total_qty"
// This does the magic
android:text="#{viewmodel.quantity}" />
...
<EditText
android:id="#+id/order_value"
// And this of course..
android:text="#{viewmodel.total}" />
...
</layout>
Note that in both classes you need to get the viewmodel from the activity, not from the fragment itself, so you share the same viewmodel for both classes:
ViewModelProvider(requireActivity()).get(ViewModel.class);
If you only get values of LiveData objects from within the UI Thread (which is the case in my code, as onChange of LiveData and binding updates are always called from the UI thread), you do not need to synchronize the lists in the ViewModel. Otherwise you need to make the whole story thread safe.

It is easy to understand that you want to take action when changing the value in a text field. So the first thing is to add a TextChangeListener or TextWatcher. Then, you have to work on the received text value to update the rest of the dependencies: Your product list or whatever it is.
Just a few basic things you need to take care of:
If you are working with fragment communication, propagate your changes by reloading in onResume of the fragment or activity. Or you can use EventBus or any other suitable mechanism.
Be sure to notify the adapter once you have changed the underlying data. I see a call to "notify**" after your code mProducts.get(position).setQuantity(val); is missing in your Product adapter. When modifying the underlying data, it is necessary to notify the adapter using "notify**" calls to update the recycler view.
The MutableLiveData error you posted above can be cleared with MutableLiveData declaration such as private final MutableLiveData mutableData = new MutableLiveData(0) in your case. You can have a function to just return LiveData as LiveData getData() { return mutableData; }

Related

Android RecyclerView item click not working - MVVM

The RecyclerView is showing all data, but the item click is not working. Here I am attaching what I have done so far. For better understanding I am removing all the unnecessary code.
This is my recyclerview item xml.
<data>
<variable
name="model"
type="com.xyz.abc.pojo.EmployeeListWithDesignationSetGet" />
<variable
name="viewModel"
type="com.xyz.abc.viewmodels.EmpListWithDesigViewModel" />
</data>
<LinearLayout
android:id="#+id/ll_details"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:clickable="true"
android:focusable="true"
android:onClick="#{() -> viewModel.itemClick(model)}">
<TextView
android:id="#+id/tv_show_details"
android:layout_width="70dp"
android:layout_height="30dp"
android:text="Show"
android:textColor="#FFFFFF" />
</LinearLayout>
The ViewModel class where I have written the click method.
public class EmpListWithDesigViewModel extends ViewModel {
private MutableLiveData<List<EmployeeListWithDesignationSetGet>> mutableLiveData;
private EmpListWithDesigClickListener listener;
private EmpListWithDesigRepository empListWithDesigRepository;
public void setListener(EmpListWithDesigClickListener listener) {
this.listener = listener;
}
public void init() {
if (mutableLiveData != null) {
return;
}
empListWithDesigRepository = EmpListWithDesigRepository.getInstance();
mutableLiveData = empListWithDesigRepository.getEmpList();
}
public MutableLiveData<List<EmployeeListWithDesignationSetGet>> getEmpList() {
return mutableLiveData;
}
public void itemClick(EmployeeListWithDesignationSetGet employeeListWithDesignationSetGet) {
listener.onItemClick(employeeListWithDesignationSetGet);
}
}
Now in activity I am implementing the click interface.
public class EmployeeDesignationActivity extends AppCompatActivity implements EmpListWithDesigClickListener {
private RecyclerView mRv_recyclerView;
private List<EmployeeListWithDesignationSetGet> arrayList;
private EmployeeListWithDesigAdapter employeeListWithDesigAdapter;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_employee_designation);
setViewReferences();
arrayList = new ArrayList<>();
employeeListWithDesigAdapter = new EmployeeListWithDesigAdapter(this,arrayList);
mRv_recyclerView.setAdapter(employeeListWithDesigAdapter);
EmpListWithDesigViewModel empListWithDesigViewModel = new ViewModelProvider(this,new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(EmpListWithDesigViewModel.class);
empListWithDesigViewModel.setListener(this);
empListWithDesigViewModel.init();
empListWithDesigViewModel.getEmpList().observe(this, new Observer<List<EmployeeListWithDesignationSetGet>>() {
#Override
public void onChanged(List<EmployeeListWithDesignationSetGet> employeeListWithDesignationSetGets) {
arrayList.addAll(employeeListWithDesignationSetGets);
employeeListWithDesigAdapter.notifyDataSetChanged();
}
});
}
private void setViewReferences(){
mRv_recyclerView = findViewById(R.id.rv_activity_employee_designation);
}
#Override
public void onItemClick(EmployeeListWithDesignationSetGet employeeListWithDesignationSetGet) {
String phone = employeeListWithDesignationSetGet.getEmpPhone();
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
startActivity(intent);
}
}
Pardon me if I have not provided enough info, this is my first SO post. Thanks
You should remove the android:onClick="#{() -viewModel.itemClick(model)}" from Linearlayout. Also add the below properties.
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
Then your item layout will be as below:
<LinearLayout
android:id="#+id/ll_details"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
>
<TextView
android:id="#+id/tv_show_details"
android:layout_width="70dp"
android:layout_height="30dp"
android:text="Show"
android:textColor="#FFFFFF" />
</LinearLayout>
Problem fixed. I forgot to bind the viewmodel in recyclerview adapter.

Screen not reflecting changes to Firebase database in Android

I'm writing a shopping list app that will save data to a Firebase Realtime database and update the screen in real time. As of right now I have it so that when you click the "+ List item" button, it adds a ShoppingListItem to the database with the name "test". It does that correctly, but it isn't updating the screen. I've double checked that the methods are being called, but the screen isn't reflecting the updated data. I have also tried using notifyDataSetChanged() and that hasn't seemed to help either.
This is the code for the ShoppingListFragment:
public class ShoppingListFragment extends Fragment {
private ArrayList<String> listItems = new ArrayList<String>();
private ListView mListView;
private TextView addItemButton;
private LinearLayout ll;
private RecyclerView rv;
private FirebaseRecyclerAdapter<ShoppingListItem, ShoppingListHolder> firebaseRecyclerAdapter;
private DatabaseReference mDatabase;
public ShoppingListFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
mDatabase = FirebaseDatabase.getInstance().getReference();
//get reference to user
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
Query query = mDatabase.child("users").child(uid).child("shopping");
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_shopping_list, container, false);
rv = view.findViewById(R.id.shopping_list);
rv.setLayoutManager(new LinearLayoutManager(getContext()));
// find the + List Item button
final TextView addCheckBox = view.findViewById(R.id.add_check_box);
addCheckBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addNewCheckBox(container);
}
});
FirebaseRecyclerOptions<ShoppingListItem> options = new FirebaseRecyclerOptions.Builder<ShoppingListItem>()
.setQuery(query, ShoppingListItem.class)
.build();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<ShoppingListItem, ShoppingListHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull ShoppingListHolder holder, int position, #NonNull ShoppingListItem item) {
holder.setItem(item);
}
#NonNull
#Override
public ShoppingListHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.shopping_list_item, parent, false);
return new ShoppingListHolder(view);
}
};
return view;
}
#Override
public void onStart() {
super.onStart();
firebaseRecyclerAdapter.startListening();
rv.setAdapter(firebaseRecyclerAdapter);
}
#Override
public void onStop() {
super.onStop();
if (firebaseRecyclerAdapter != null) {
firebaseRecyclerAdapter.stopListening();
}
}
public void addNewCheckBox(ViewGroup container) {
String key = mDatabase.child("users").child(getUid()).child("shopping_list").push().getKey();
ShoppingListItem item = new ShoppingListItem("test");
mDatabase.child("users").child(getUid()).child("shopping_list").child(key).setValue(item);
firebaseRecyclerAdapter.notifyDataSetChanged();
}
public int convertToPx(int input) {
DisplayMetrics metrics = getResources().getDisplayMetrics();
int px = (int) (input * ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT));
return px;
}
private class ShoppingListHolder extends RecyclerView.ViewHolder {
private final EditText itemName;
public ShoppingListHolder(View itemView) {
super(itemView);
itemName = itemView.findViewById(R.id.item_name);
}
public void setItem(ShoppingListItem item) {
itemName.setText(item.getItemName());
}
}
private String getUid() {
return FirebaseAuth.getInstance().getCurrentUser().getUid();
}
}
and the ShoppingListItem:
public class ShoppingListItem {
private String itemName;
private String type;
private String unit;
private int quantity;
private String location;
public ShoppingListItem() {
}
public ShoppingListItem(String name) {
this.itemName = name;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
}
XML for the shopping list view:
<LinearLayout 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"
android:id="#+id/home_fragment_main_layout"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.jggdevelopment.wannacook.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Shopping List"
android:textStyle="bold"
android:textSize="18sp"
android:layout_marginStart="16dp"
android:layout_marginBottom="16dp"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/shopping_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:clipToPadding="false"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/add_check_box"
android:text="+ List item"
android:textSize="16sp"
android:layout_marginStart="40dp"
android:layout_marginTop="16dp"/>
</LinearLayout>
and XML for each line item in the shopping list:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/item_name"
android:layout_weight="1"
android:layout_marginStart="12dp"
android:layout_marginEnd="32dp"
android:inputType="text"
android:background="#android:color/transparent"
/>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:id="#+id/xbutton"
android:layout_marginEnd="12dp"
android:src="#drawable/ic_close_white_24dp"
android:layout_gravity="center_vertical"/>
</LinearLayout>
This is not working because you forgot to set the adapter in the onCreate() method. To solve this, add rv.setAdapter(firebaseRecyclerAdapter);, right before you are returning the view like this:
rv.setAdapter(firebaseRecyclerAdapter);
return view;
And you can remove that line of code from the onStart() method because is not needed there.
If you are interested, I have created a tutorial in which I'm explaining step by step, how to build a Shopping List App using Cloud Firestore and Android.

Adding progressbar at the end of recyclerview

In my app, I send a volley request which fetches list items one-by-one, not all at once. I want to implement a progressbar at the end of the recyclerview when the data is being fetched. The class 'updateAdapter' updates the adapter and I was thinking of making the progress bar visible in the recyclerview scroll listener. But I have no idea how to do this in my code.
updateAdapter.java
//THIS CLASS UPDATES THE RECYCLER VIEW
public class updateAdapter{
private FetchWpApi fetch;
private int totalItemCount;
private int prevLastvisible=0;
private int fpage=1;
private Context context;
private RecyclerView recyclerView;
private ArrayList<sItem> list= new ArrayList<>();
private LinearLayoutManager manager;
private ProgressBar progressBar;
//CONSTRUCTOR FOR updateAdapter
public updateAdapter(RecyclerView recyclerView, final Context context, String url, LinearLayoutManager manager){
this.context=context;
this.recyclerView=recyclerView;
fetch=new FetchWpApi(url,context);
this.manager=manager;
}
public void fetchAndPut()
{
if(recyclerView.getAdapter()==null) {
fetch.fetchApiData(fpage, new FetchWpApi.Callback() {
#Override
public void onSuccess(sItem sitem) {
list.add(sitem);
if (list.size() == 1 || recyclerView.getAdapter() == null) {
ItemAdapter adapter = new ItemAdapter(context, list);
recyclerView.setAdapter(adapter);
} else if (list.size() > 1 && recyclerView.getAdapter() != null) {
recyclerView.getAdapter().notifyDataSetChanged();
recyclerView.getAdapter().notifyItemRangeChanged(0, recyclerView.getAdapter().getItemCount());
}
}
#Override
public void onFail(String msg) {
Toast.makeText(context, "FAILED PRIMARY LOAD", Toast.LENGTH_LONG).show();
}
});
}
recyclerView.addOnItemTouchListener(
//Not important
);
//SCROLL LISTENER ATTACHED TO THE RECYCLER VIEW
//SHOW PROGRESS BAR HERE?
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener()
{
#Override
public void onScrolled(final RecyclerView recyclerView, int dx, int dy)
{
if(dy > 0) //check for scroll down
{
totalItemCount = manager.getItemCount();
int lastVisibleItemPosition = ((LinearLayoutManager) manager).findLastVisibleItemPosition();
if( (lastVisibleItemPosition+1)==totalItemCount && totalItemCount%10==0 && lastVisibleItemPosition>prevLastvisible)
{
//SET VISIBILITY OF THE PROGRESS BAR IN THE LAST CARD ITEM TO VISIBLE ??
fpage++;
//loading = false;
Log.v("...", "Last Item !");
//Do pagination.. i.e. fetch new data
prevLastvisible=lastVisibleItemPosition;
fetch.fetchApiData(fpage,new FetchWpApi.Callback(){
#Override
public void onSuccess(sItem sitem){
list.add(sitem);
recyclerView.getAdapter().notifyDataSetChanged();
recyclerView.getAdapter().notifyItemRangeChanged(0, recyclerView.getAdapter().getItemCount());
}
#Override
public void onFail(String msg){
Toast.makeText(context,"FAILED ONLOAD",Toast.LENGTH_LONG).show();
}
});
}
}
}
});
}
}
The progressbar which I want to display is in this cardview:
finalcard.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/cvItem"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<!--<LinearLayout-->
<!--android:id="#+id/linearlayout"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_margin="10dp"-->
<!-->-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/tvTitle"
android:textColor="#000"
android:text="Sample text Sampletext sample text sample text sample text"
android:layout_alignParentStart="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toStartOf="#+id/ivMainImage"
android:layout_marginTop="15dp"
android:paddingStart="16dp"
android:paddingEnd="2dp"
android:maxLines="4"
android:textSize="20sp"
android:layout_gravity="start"
/>
<TextView
android:id="#+id/dateText"
android:text="DATE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="16dp"
android:paddingEnd="0dp"
android:layout_gravity="start"
android:layout_below="#id/tvTitle"
android:ellipsize="marquee"/>
<ImageView
android:id="#+id/ivMainImage"
android:layout_width="140dp"
android:layout_height="130dp"
android:layout_gravity="end"
android:layout_alignParentEnd="true"
android:layout_margin="15dp"
android:src="#android:drawable/btn_star"
android:scaleType="centerCrop"
/>
<View
android:id="#+id/view"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#0C000000"
android:layout_below="#id/ivMainImage"
/>
<ProgressBar
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar2"
android:layout_below="#id/view"
android:visibility="gone"
/>
</RelativeLayout>
Fix your progress bar below your recycler view instead of card view.
Set visible progress bar when call web service, and set visibility Gone after complete service call
//Progress bar setVisibility - Visible
progressbar.setVisibility(View.VISIBLE);
fetch.fetchApiData(fpage, new FetchWpApi.Callback() {
#Override
public void onSuccess(sItem sitem) {
progressbar.setVisibility(View.GONE);
list.add(sitem);
recyclerView.getAdapter().notifyDataSetChanged();
recyclerView.getAdapter().notifyItemRangeChanged(0, recyclerView.getAdapter().getItemCount());
}
#Override
public void onFail(String msg) {
progressbar.setVisibility(View.GONE);
Toast.makeText(context, "FAILED ONLOAD", Toast.LENGTH_LONG).show();
}
});
I think you can follow this link to add header or footer Recyclerview offically. And you add footer as progressbar to load more data, and remove footer when you finish loading data.
There are 2 ways:
1. Put the progress bar in the bottom of the screen and when you are bottom of the RecyclerView by int last = layoutManager.findLastVisibleItemPosition(); it just display and when you get the response then hide it;
2. In your adapter:
add a dummy row to the last position of your list.
check in adapter when the position is last then show the progress bar:
if(position == list.size - 1) {
// create a layout which only have progress bar
View row = LayoutInflater.from(viewGroup.getContext()).inflate(
R.layout.progressbar, viewGroup, false);
} else {
View row = LayoutInflater.from(viewGroup.getContext()).inflate(
R.layout.layout_list, viewGroup, false);
}

Android: Scrolling from one Fragment to another through a ViewPager

I have implemented a ViewPager which connects multiple Fragments together to give off that 'scrolling view' experience as you might already know. Anyhow I have a button on my first fragment that needs to scroll the user to the second fragment. How do I get this done folks? Here's my code:
ViewPager XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
</LinearLayout>
Fragment 1:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#3498db" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="left"
android:layout_marginLeft="15dp"
android:layout_marginTop="25dp"
android:text="Step 1"
android:textColor="#ffffff"
android:textSize="50dp" />
<Button
android:id="#+id/step1Btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:layout_marginBottom="5sp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:text="Next Step"
android:textColor="#FFFFFF"
android:textSize="30sp" />
</LinearLayout>
</RelativeLayout>
In order to make this post short, let's say the second fragment has the same XML.
So we have TWO fragments. Here's the fragment java class:
public class RegisterPageOne extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.registration_page1, container, false);
Button nextStep = (Button) v.findViewById(R.id.step1Btn);
nextStep.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
//Where I want to call the SECOND FRAG
}
});
return v;
}
public static RegisterPageOne newInstance(String text) {
RegisterPageOne f = new RegisterPageOne();
Bundle b = new Bundle();
b.putString("msg", text);
f.setArguments(b);
return f;
}
}
help me out please :)
You can use the setCurrentItem(int item,boolean smoothScroll) method of your viewPager object to achieve this. Here is the documentation.
Saw your followup question about how to refer to the ViewPager, from your Fragment. It's pretty convoluted, for a relative newbie like me, but, I think I can get all of it... I'll give you what I did in my code. Set up a listener in the ViewPager:
public class PageViewActivity extends FragmentActivity
implements PageDisplayPortFrag.OnLinkExpectedListener {
...
}
This gets referenced in your Fragment:
private OnLinkExpectedListener mListenerLink;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListenerLink = (OnLinkExpectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnLinkExpectedListener");
}
}
#Override
public void onDetach() {
mListenerLink = null;
super.onDetach();
}
// THIS IS THE METHOD CALLED BY THE BUTTON CLICK !!!
public void linkExpected(String ticker) {
if (mListenerLink != null) {
mListenerLink.onLinkActivity(ticker);
}
}
public interface OnLinkExpectedListener {
public void onLinkActivity(String ticker);
}
And, then, a method gets called in the ViewPager:
#Override
public void onLinkActivity(String ticker) {
// receive ticker from port click; user wants to view page in webview, for this security
//Toast.makeText(this, "Ticker item clicked: " + ticker, Toast.LENGTH_SHORT).show();
// NEHARA, YOU MAY OR MAY NOT NEED TO DO STUFF HERE
// THIS IS WHAT I NEEDED TO DO; LEAVING IT FOR THE
// getFragmentTag() EXAMPLE -- WHICH CAME FROM Stack Overflow POSTERS
// get all-important fragment tag
String frag_Tag = getFragmentTag(pager.getId(),1);
// use tag to get the webview fragment
PageXYZWebviewFrag xyzWeb = (PageXYZWebviewFrag)
getSupportFragmentManager().findFragmentByTag(frag_Tag);
// send request to the webview fragment
xyzWeb.loadPageForSelectedSecurity(ticker);
// SET THE DESIRED FRAGMENT, assumes I know the fragment number
// from when I set up the ViewPager
// switch to the webview fragment
pager.setCurrentItem(1);
}
// fragment tag getter -- very important, yet has been elusive
private String getFragmentTag(int viewPagerId, int fragmentPosition) {
return "android:switcher:" + viewPagerId + ":" + fragmentPosition;
}

FrameLayout onClick event doesn't work

Have this layout in my app
<FrameLayout
android:gravity="center"
android:layout_width="35dp"
android:layout_height="35dp"
android:clickable="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:id="#+id/avatar_layout">
<ImageView
android:id="#+id/image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/knotable_icon"
android:clickable="true"/>
<TextView
android:id="#+id/char_text_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="#string/character"
android:clickable="true"
android:textStyle="bold"
android:textColor="#color/black"
android:textSize="22sp" />
</FrameLayout>
Its actually profile picture of the user.
I need to implement onClick listener for this FrameLayout.
Behind the scene I have AvatarListAdapter.java with
public class AvatarListAdapter extends ArrayAdapter<AvatarUiItem> {
...
private class ViewHolder {
private View.OnClickListener mAvatarClickListener;
}
private View.OnClickListener mAvatarClickListener;
holder.avatarView=(FrameLayout) convertView.findViewById(R.id.avatar_layout); //inflates FrameLayout here
holder.avatarView.setTag(itemInfo);
holder.avatarView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Log.d("tag", "here we are");
if (mAvatarClickListener != null) {
mAvatarClickListener.onClick(view);
}
}
});
then in the main activity :
mAdp = new AvatarListAdapter(this, mAvatarUiItemArray, mApp,
mImageLoader, mDisplayMetrics, mPartCounterNum);
mListView.setAdapter(mAdp);
mAdp.setOnAvatarClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Object object = v.getTag();
if (object instanceof KnoteUiItem) {
KnoteUiItem uiItem = (KnoteUiItem) object;
Log.d("tag","avatar click");
}
}
});
The problem here is that OnClick event NEVER FIRES.
Really would appreciate some help on this.
Use OnItemClickListener
holder.setTag(itemInfo); // Set the tag on the root view rather than the avatarView
.
.
.
.
OnItemClickListener listener = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object object = v.getTag();
if (object instanceof KnoteUiItem) {
KnoteUiItem uiItem = (KnoteUiItem) object;
Log.d("tag","avatar click");
}
}
};
mListView.setOnItemClickListener(listener);

Categories

Resources