0

I'm beginner for developing. I'm using Android studio, I've created App that retrieve data from Firebase Realtime database. I know how to add operation for the click counter but it is not save EX:"when i click on the button the TextView change to ++1, but if i closed the application and reopen it again it's show me 0 as I didn't any thing before, or even if I moved to other activity". what I want to do is "count the click ++1 and store in firebase at the same time its update for all users who are using the application ((Retrieve data)). So: my question is how to make the Click counter Store and Retrieve in a TextView?. Here's the Image of what i want: https://vigor-ads.com/wp-content/uploads/2018/11/TEST.png

And RealTimeDataBase: https://vigor-ads.com/wp-content/uploads/2018/11/FireBase-RealTime.png

PharPolice.java

public class PharPolice extends Fragment {

private int ClicksCounter = 0;
private TextView counter;
DatabaseReference clicksref;


ListView listView;
FirebaseDatabase database;
DatabaseReference myRef;
ArrayList<String> list;
PolicePharmacies policePharmacies ;
FirebaseListAdapter adapter;

public PharPolice() {
    // Required empty public constructor
}


@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    return inflater.inflate(R.layout.fragment_phar_police, container, false);
}

//---------------------F I R E B A S E------------------------

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    Query query = FirebaseDatabase.getInstance().getReference("PharmaciesDB").child("PolicePharmacies");
    FirebaseListOptions<PolicePharmacies> options = new FirebaseListOptions.Builder<PolicePharmacies>()
            .setLayout(R.layout.pharinfo)
            .setQuery(query, PolicePharmacies.class)
            .build();

    clicksref = FirebaseDatabase.getInstance().getReference("PharmaciesDB").child("PolicePharmacies");
    policePharmacies = new PolicePharmacies();
    listView = view.findViewById(R.id.pharpoliceListView);
    database = FirebaseDatabase.getInstance();
    myRef = database.getReference("PolicePharmacies");
    list = new ArrayList<>();

    adapter = new FirebaseListAdapter(options) {
        @Override
        protected void populateView(View v, final Object model, int position) {
            CardView cv = v.findViewById(R.id.pharcard);
            counter = v.findViewById(R.id.SACounter);
            TextView Name = v.findViewById(R.id.SAName);
            TextView Address = v.findViewById(R.id.SAAddress);

            Name.setText(((PolicePharmacies) model).getName());
            Address.setText(((PolicePharmacies) model).getAddress());
            counter.setText(((PolicePharmacies) model).getClickCounter());

            cv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {


                    Visits();

                    Intent intent = new Intent(getActivity(), PharPoliceScreen.class);
                    intent.putExtra("Name", ((PolicePharmacies) model).getName());
                    intent.putExtra("Address", ((PolicePharmacies) model).getAddress());
                    intent.putExtra("Phone1", ((PolicePharmacies)model).getPhone1());
                    intent.putExtra("Phone2", ((PolicePharmacies)model).getPhone2());
                    intent.putExtra("Phone3", ((PolicePharmacies)model).getPhone3());
                    intent.putExtra("Offer", ((PolicePharmacies)model).getOffer());
                    intent.putExtra("Facebook", ((PolicePharmacies)model).getFacebook());



                    startActivity(intent);
                }
            });
        }

    };
    listView.setAdapter(adapter);


}

@Override
public void onStart() {
    super.onStart();
    adapter.startListening();

}

@Override
public void onStop() {
    super.onStop();
    adapter.stopListening();
}

private void Visits ()
{
    ClicksCounter ++;
    counter.setText(Integer.toString(ClicksCounter));

    String clickCounter = counter.getText().toString().trim();
    PolicePharmacies saveData = new PolicePharmacies(clickCounter);
    clicksref.push().setValue(saveData);
}

//---------------------F I R E B A S E------------------------

PolicePharmacies.class

public class PolicePharmacies {
private String Name;
private String Address;
private String Phone1;
private String Phone2;
private String Phone3;
private String Offer;
private String Facebook;
private String ClickCounter;

public PolicePharmacies() {

}



public PolicePharmacies(String name, String address, String phone1, String phone2, String phone3, String offer, String facebook) {
    Name = name;
    Address = address;
    Phone1 = phone1;
    Phone2 = phone2;
    Phone3 = phone3;
    Offer = offer;
    Facebook = facebook;

}

public PolicePharmacies(String clickCounter) {
    ClickCounter = clickCounter;
}


public String getName() {
    return Name;
}

public void setName(String name) {
    Name = name;
}

public String getAddress() {
    return Address;
}

public void setAddress(String address) {
    Address = address;
}

public String getPhone1() {
    return Phone1;
}

public void setPhone1(String phone1) {
    Phone1 = phone1;
}

public String getPhone2() {
    return Phone2;
}

public void setPhone2(String phone2) {
    Phone2 = phone2;
}

public String getPhone3() {
    return Phone3;
}

public void setPhone3(String phone3) {
    Phone3 = phone3;
}

public String getOffer() {
    return Offer;
}

public void setOffer(String offer) {
    Offer = offer;
}

public String getFacebook() {
    return Facebook;
}

public void setFacebook(String facebook) {
    Facebook = facebook;
}

public String getClickCounter() {
    return ClickCounter;
}

public void setClickCounter(String clickCounter) {
    ClickCounter = clickCounter;
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

0

The problem is not in the code. The problem is in the logic. Try to imagine that there are two separate parts in this process. First step increase the number Second step show the result. So first step you can easy implement after reading this article about WRITING into firebase https://firebase.google.com/docs/database/admin/save-data Now about second step how to show the result. First what you have to understand that TextView is not saving any data it just showing it. So to show the data you suppose to read it from firebase which you saved it on first step. The same article also showed how to read data. So when it is saved and when you received it ONLY after that you could show it in TextView. Hope it helps

Bo Z
  • 2,359
  • 1
  • 13
  • 31
  • actually I've tried a lot of codes by my self, so in code now when i click on any of those CardView, it's create by it self a new Child with "ClickCounter" and inside this Child create other one by clickCounter, and it can show in the TextView. as u see in my code that i can read it but the only one i'm confusing that when it count the clicks it should go to the right place. EX: "if i click on the first thing on the CardView it will save on parent named by "ClickCounter" and then the TextView will view the number of clicks. – Mohamed Soliman Apr 23 '19 at 20:57
  • @MohamedSoliman you have several mistakes. First you instead of trying to push saveData you need to push just value. How to get value? Need one more method which will read value from firebase for example it is 3 after click you make 3+1 and push to the firebase result of 4. Got it? – Bo Z Apr 24 '19 at 12:56
  • yes you right, actually i was following tutorial for retrieving data and android studio telling me now its unsafe and unchecked.. so i'll try to re project with a professional code ((onDatachange)) after that i'll try to search more for this operation if counting. Over all really thank you for your support :) – Mohamed Soliman Apr 24 '19 at 18:28
  • @MohamedSoliman on data change is the part where you are setting the value to the textView after you made +1 thats the last part. But in onClick listener you need method which read value from firebase --> make it as int --> calculate +1 --> convert to string --> update / push the value back to firebase. Does that clear enough? – Bo Z Apr 24 '19 at 19:29