Making a database-voting app

↘锁芯ラ 提交于 2019-12-13 17:40:46

问题


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

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.

Here is the layout

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

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)?

EDIT:

Ok now I have the code for it:

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("+");
        }
    });
}

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.


回答1:


Here is the code.

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

Thanks to Jokel from Androidpit.de

Here is the link:

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



来源:https://stackoverflow.com/questions/50555452/making-a-database-voting-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!