如何添加点击计数器,存储在Firebase中并检索

时间:2019-04-23 19:31:34

标签: android firebase-realtime-database counter

我是开发的初学者。 我正在使用Android Studio,已经创建了从Firebase Realtime数据库检索数据的App。 我知道如何为点击计数器添加操作,但无法保存 例如:“当我单击按钮时,TextView更改为++ 1,但是如果我关闭该应用程序并再次重新打开它,则显示0,因为我之前没做任何事情,或者即使我移至其他活动也是如此”。 我想做的是“对点击++ 1进行计数,并同时将所有使用该应用程序的用户的更新存储在Firebase中((检索数据))。 所以:我的问题是如何使Click计数器在TextView中存储和检索? 这是我想要的图像: https://vigor-ads.com/wp-content/uploads/2018/11/TEST.png

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

PharPolice.java

公共类PharPolice扩展了片段{

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;
}

1 个答案:

答案 0 :(得分:0)

问题不在代码中。问题出在逻辑上。试想一下,在此过程中有两个单独的部分。第一步增加数量第二步显示结果。因此,阅读本文有关写入到Firebase https://firebase.google.com/docs/database/admin/save-data的文章后,您可以轻松实现第一步 现在关于第二步如何显示结果。首先,您必须了解TextView不会保存任何数据,而只是显示数据。因此,为了显示数据,您假设要从Firebase读取数据,并在第一步中将其保存。同一篇文章还显示了如何读取数据。因此,当它被保存并且只有在此之后您才可以在TextView中显示它。 希望对您有帮助