0

I have an App which downloads data from Firebase and displays it in a RecyclerView and this works fine. What I want to do is show or hide an input element in the XML when certain conditions apply. The condition of 'yes' or 'no' is downloaded from Firebase.

It sort of works but only hides the TextView in the first item of the RecyclerView listing. How do I get it to apply to all the listed items? I will add the code and a screenshot.

As you can see, the 'quantity' input field is hidden only on the first item display

Code:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menus);
    setUpRecyclerView();
    }

    private void setUpRecyclerView() {
    // get menu type from MenuSelectListActivity
    selectedMenu = getIntent().getStringExtra("myMenuSelected");

    //get Firestore db and use selected menu for listing
    FirebaseFirestore db = FirebaseFirestore.getInstance();
    CollectionReference notebookRef = db.collection(selectedMenu);

    FirestoreRecyclerOptions<NoteAdapter> options = new    FirestoreRecyclerOptions.Builder<NoteAdapter>()
            .setQuery(query, NoteAdapter.class)
            .build();

    adapter = new Note(options);

    final RecyclerView recyclerView = findViewById(R.id.recycler_view);

    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setAdapter(adapter);

    DocumentReference docRef = db.collection(“delivery    status”).document(“****************”);
    docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
            if (task.isSuccessful()) {
                DocumentSnapshot document = task.getResult();

                if (document.exists()) {
                    String myDeliveryStatus = document.getString("deliverystatus");

                  // if delivery status yes then allow the order to be made
                    if (myDeliveryStatus.equals("yes")) {

                        // show quantity input TextView
                            TextView text_quantity = (TextView) findViewById(R.id.text_view_quantity);

                            //Toggle
                            if (text_quantity.getVisibility() == View.INVISIBLE)
                                text_quantity.setVisibility(View.VISIBLE);
                            else
                                text_quantity.setVisibility(View.VISIBLE);

                        adapter.setOnItemClickListener(new Note.OnItemClickListener() {
                            @Override
                            public void onItemClick(DocumentSnapshot documentSnapshot, int position) {
                                String myTitle = ((TextView) recyclerView.findViewHolderForAdapterPosition(position).itemView.findViewById(R.id.text_view_title)).getText().toString();
                                String myPrice = ((TextView) recyclerView.findViewHolderForAdapterPosition(position).itemView.findViewById(R.id.text_view_price)).getText().toString();
                                String myNumberOrdered = ((TextView) recyclerView.findViewHolderForAdapterPosition(position).itemView.findViewById(R.id.text_view_quantity)).getText().toString();

                       ***** do various calculations on the data downloaded from Firebase. Not relevant to this question so not included

                    }
             //   if no do nothing
                   else if (myDeliveryStatus.equals("no")) {
                        TextView text_quantity = (TextView) findViewById(R.id.text_view_quantity);

                        //Toggle to hide TextView
                        if (text_quantity.getVisibility() == View.VISIBLE)
                            text_quantity.setVisibility(View.INVISIBLE);
                        else
                            text_quantity.setVisibility(View.INVISIBLE);
                   }
                } else {
                    //  Log.d(TAG, "No such document");
                }
            } else {
                // Log.d(TAG, "get failed with ", task.getException());
            }
        }

    });
}

Code for Adapter:

 public class NoteAdapter {
private String title;
private String description;
private double price;

private int priority;

private int quantity;

private String status;

public NoteAdapter() {
    //empty constructor needed

}

public NoteAdapter(String title, String description, double price, int priority, int quantity) {

    this.title = title;
    this.description = description;
    this.price = price;

    this.priority = priority;
    this.quantity = quantity;
    this.status = status;
}

public String getTitle() {
    return title;
}

public String getDescription() {
    return description;
}


public double getPrice() {
    return price;
}

public int getPriority() {
    return priority;
}

public int getQuantity() {
    return quantity;
}

public String getStatus() {
    return status;
}

}

PhilipS
  • 379
  • 5
  • 15
  • you can add this check in the adapter. – Tanveer Munir Feb 11 '19 at 14:04
  • Thank you, although I am not sure what the advantage would be of doing so. How would this solve my problem? For the moment I would like to keep it organised 'as is' – PhilipS Feb 11 '19 at 15:50
  • adapter is responsible for to set the view for each item so you can add a view or change the view according to your requirements. – Tanveer Munir Feb 12 '19 at 06:21
  • add the code of adapter also @PhilipS – Tanveer Munir Feb 12 '19 at 06:25
  • Tanveer, I added the Adapter code as requested. I really do not know how / where to add the necessary code in the adapter. Any help much appreciated. – PhilipS Feb 12 '19 at 09:10
  • have you using some kind of Firebase adapter like type?? or using the normal type of recyclerview adapter listing??? – Tanveer Munir Feb 12 '19 at 09:51
  • Have you any idea regarding recyclerview before?? Because I am using some kind of recyclerview Adapter without Firebase if you're using firebase type adapter I don't about that. – Tanveer Munir Feb 12 '19 at 09:54
  • how to Initialize and set the Adapter for here the [Link](https://stackoverflow.com/questions/49277797/how-to-display-data-from-firestore-in-a-recyclerview-with-android) for your understanding please Initialize like this and update your code in question – Tanveer Munir Feb 12 '19 at 10:01
  • you're Initialize the adapter In wrong way. – Tanveer Munir Feb 12 '19 at 10:07

1 Answers1

0

Firstly you're doing some kind of mistake in Initializing the Recyclerview and Adapter. Which you are using as the adapter that's a just model or class. Please have to look at this answer and change your code structure according to this Then You should do this step in BindViewHolder

  adapter = new FirestoreRecyclerAdapter<NoteAdapter, ProductViewHolder>(options) {
        @Override
        protected void onBindViewHolder(@NonNull holder productViewHolder, int position, @NonNull NoteAdapter productModel) {
            //here you can check the Yes or No like this
            if (poductModel.getStatus.equalsIgnoreCase("no")){
               if (text_quantity.getVisibility() == View.VISIBLE)
                            text_quantity.setVisibility(View.INVISIBLE);
                        else
                            text_quantity.setVisibility(View.INVISIBLE);
               }
        }

        @NonNull
        @Override
        public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_product, parent, false);
            return new ProductViewHolder(view);
        }
    };
Tanveer Munir
  • 1,956
  • 1
  • 12
  • 27