制作数据库投票应用 [英] Making a database-voting app

查看:105
本文介绍了制作数据库投票应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我既是Java编程的业余爱好者,也是Android Studio的业余爱好者,而且我对数据库的了解也不是很好.

I'm a amateur in Java programming and also in Android Studio and my knowledge about databases isn't that good, too.

现在是问题.
我该如何制作一个让用户可以为事件投票的应用程序.他们可以在是"和否"按钮之间进行选择.在两个按钮下方,有两个TextViews,它们显示了用户选择的平均值.

Now the question.
How can I make an app where the users can vote about an event. They can choose between the "yes" and "no" buttons. Below the two buttons there are two TextViews which shows the average of the users choices.

这是布局

<FrameLayout 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"
tools:context=".TabCommunity">

<RelativeLayout
    android:id="@+id/voting"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/question"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="100dp"
        android:gravity="center"
        android:text="Do you think the event is good?"
        android:textSize="20dp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/btnYes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_marginStart="50dp"
        android:background="@color/colorPrimaryDark"
        android:fontFamily="casual"
        android:padding="15dp"
        android:text="YES"
        android:textColor="#fff"
        android:textSize="20dp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/btnNo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:layout_marginEnd="50dp"
        android:background="@color/colorPrimaryDark"
        android:fontFamily="casual"
        android:padding="15dp"
        android:text="NO"
        android:textColor="#fff"
        android:textSize="20dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/com_yes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="170dp"
        android:layout_marginStart="120dp"
        android:text="- %"
        android:textSize="16dp"
        android:textStyle="italic" />

    <TextView
        android:id="@+id/com_no"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignTop="@+id/com_yes"
        android:layout_marginEnd="102dp"
        android:text="- %"
        android:textSize="16dp"
        android:textStyle="italic" />

</RelativeLayout>

</FrameLayout>

如何保护数据库中用户的选择,并实时更新百分比(并将其分配给两个TextView)?

How can I safe the choices of the users in a database, and update the percent in realtime (and give it to the two TextViews)?

好,现在我有了代码:

public class Location extends Activity {

public static String[] bet = new String[] {"Yes", "No"};

private Button btn_yes, btn_no;
private TextView comY, comN;
private View voteView;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_blank);

    btn_yes = (Button) findViewById(R.id.btnYes);

    btn_yes.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            FirebaseDatabase database = FirebaseDatabase.getInstance();
            DatabaseReference myRef = database.getReference("VoteYes");

            myRef.setValue("+");
        }
    });

    btn_no = (Button) findViewById(R.id.btnNo);

    btn_no.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            FirebaseDatabase database = FirebaseDatabase.getInstance();
            DatabaseReference myRef = database.getReference("VoteNo");

            myRef.setValue("+");
        }
    });
}

在Firebase数据库中,代码如下所示:

In the Firebase Database the code looks just like this:

{
 "rules": {
   ".read": true,
   ".write": true,
  }
}

因此,单击按钮可在数据库中创建一个新条目,仅此而已.
我已经在互联网上做了一些研究...但是我太笨了,无法理解它.

So a click on the button make a new entry in the database, but that's it.
I've done a little research in the internet...but I'm too dumb to understand it.

我该如何编辑代码,让每个用户都有一票,并且数据库增加是或否呢?

How can I edit the code, that every user has one vote, and the database increase either the yes or no side?

在此先感谢大家!对不起我的英语不好.

Thanks in advance guys! Sorry for my English.

推荐答案

以下是代码.

public class MainActivity extends AppCompatActivity implements View.OnClickListener { 
public static String[] bet = new String[] {"YES", "NO"};

private Button btn_yes, btn_no;
private TextView comY, comN;
private View voteView;

FirebaseDatabase database;
DatabaseReference myRefYes, myRefNo;
long oldYes, oldNo;
int flag = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btn_yes = (Button) voteView.findViewById(R.id.btnYes);
btn_yes.setOnClickListener(this);
btn_no = (Button) voteView.findViewById(R.id.btnNo);
btn_no.setOnClickListener(this);

myRefYes = database.getReference("ForecastVoteYes");
myRefYes.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
          oldYes = dataSnapshot.getValue(long.class);
    }
    @Override
    public void onCancelled(DatabaseError databaseError) {
    }
});

myRefNo = database.getReference("ForecastVoteNo");
myRefYes.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
         oldYes = dataSnapshot.getValue(long.class);
    }
    @Override
    public void onCancelled(DatabaseError databaseError) {
    }
});

}




public void onClick(View v) {
switch (v.getId()){
    case R.id.btnYes:
        if ( flag == 0){
            oldYes++;
            myRefYes.setValue(oldYes);
            flag = 1;
        }
        break;

    case R.id.btnNo:
        if ( flag == 0){
            oldNo++;
            myRefYes.setValue(oldNo);
            flag = 1;
        }
        break;
}
}
}

感谢Androidpit.de的Jokel

Thanks to Jokel from Androidpit.de

这里是链接:

https://www.androidpit.de/forum/772740/eine-datenbank-voting-app-erstellen#3174286

这篇关于制作数据库投票应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆