我创建了一个基本的反馈app,其中主activity(mainactivity.java
)以imageViews的形式记录客户的姓名和评级(1-5),并将数据发送到FireBase。
我的第二个activity(score.java
)只是汇总来自FireBase中所有客户的评级。
我的问题是应用程序在启动时直接跳转到score.java
,而不收集来自客户的反馈。 附注。 我使用intent
在活动之间跳转。
下面是代码:
主要活动-
package com.example.feedback;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import static android.os.SystemClock.sleep;
public class MainActivity extends AppCompatActivity {
EditText name;
ImageView oneStar, twoStar, threeStar, fourStar, fiveStar;
Intent intent;
FirebaseDatabase rootNode;
DatabaseReference reference;
public void displayScore() {
intent = new Intent(getApplicationContext(), Score.class);
startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); //will hide the title
getSupportActionBar().hide(); // hide the title bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN); //enable full screen
setContentView(R.layout.activity_main);
name = findViewById(R.id.editTextPersonName);
oneStar = findViewById(R.id.imageView1);
twoStar = findViewById(R.id.imageView2);
threeStar = findViewById(R.id.imageView3);
fourStar = findViewById(R.id.imageView4);
fiveStar = findViewById(R.id.imageView5);
oneStar.setTag(1);
twoStar.setTag(2);
threeStar.setTag(3);
fourStar.setTag(4);
fiveStar.setTag(5);
oneStar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (name.length() == 0){
name.setError("Please enter your full name.");
}
else {
name.setError(null);
rootNode = FirebaseDatabase.getInstance();
reference = rootNode.getReference().child("Users");
//Fetch all values
String username = name.getText().toString();
String value = view.getTag().toString();
int rating = Integer.parseInt(value);
UserHelper helper = new UserHelper(username,rating);
reference.child(username).setValue(helper);
Toast.makeText(MainActivity.this, "Feedback submitted successfully!", Toast.LENGTH_SHORT).show();
sleep(200);
displayScore();
}
}
});
twoStar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (name.length() == 0){
name.setError("Please enter your full name.");
}
else {
name.setError(null);
rootNode = FirebaseDatabase.getInstance();
reference = rootNode.getReference().child("Users");
//Fetch all values
String username = name.getText().toString();
String value = view.getTag().toString();
int rating = Integer.parseInt(value);
UserHelper helper = new UserHelper(username,rating);
reference.child(username).setValue(helper);
Toast.makeText(MainActivity.this, "Feedback submitted successfully!", Toast.LENGTH_SHORT).show();
displayScore();
}
}
});
threeStar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (name.length() == 0){
name.setError("Please enter your full name.");
}
else {
name.setError(null);
rootNode = FirebaseDatabase.getInstance();
reference = rootNode.getReference().child("Users");
//Fetch all values
String username = name.getText().toString();
String value = view.getTag().toString();
int rating = Integer.parseInt(value);
UserHelper helper = new UserHelper(username,rating);
reference.child(username).setValue(helper);
Toast.makeText(MainActivity.this, "Feedback submitted successfully!", Toast.LENGTH_SHORT).show();
displayScore();
}
}
});
fourStar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (name.length() == 0){
name.setError("Please enter your full name.");
}
else {
name.setError(null);
rootNode = FirebaseDatabase.getInstance();
reference = rootNode.getReference().child("Users");
//Fetch all values
String username = name.getText().toString();
String value = view.getTag().toString();
int rating = Integer.parseInt(value);
UserHelper helper = new UserHelper(username,rating);
reference.child(username).setValue(helper);
Toast.makeText(MainActivity.this, "Feedback submitted successfully!", Toast.LENGTH_SHORT).show();
displayScore();
}
}
});
fiveStar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (name.length() == 0){
name.setError("Please enter your full name.");
}
else {
name.setError(null);
rootNode = FirebaseDatabase.getInstance();
reference = rootNode.getReference().child("Users");
//Fetch all values
String username = name.getText().toString();
String value = view.getTag().toString();
int rating = Integer.parseInt(value);
UserHelper helper = new UserHelper(username,rating);
reference.child(username).setValue(helper);
Toast.makeText(MainActivity.this, "Feedback submitted successfully!", Toast.LENGTH_SHORT).show();
displayScore();
}
}
});
}
}
记分。java-
package com.example.feedback;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import androidx.appcompat.app.AppCompatActivity;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
public class Score extends AppCompatActivity {
TextView avgScore;
DatabaseReference dbRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); //will hide the title
getSupportActionBar().hide(); // hide the title bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN); //enable full screen
setContentView(R.layout.score);
avgScore = findViewById(R.id.textView2);
dbRef = FirebaseDatabase.getInstance().getReference().child("Users");
scoreRealTime();
}
public void scoreRealTime() {
dbRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
double total = 0;
for (DataSnapshot ds : snapshot.getChildren()){
double values = Double.parseDouble(ds.child("rating").getValue().toString());
total = total + values;
}
double average = (double) total / snapshot.getChildrenCount();
avgScore.setText(String.format("%.2f", average));
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
}
AndroidManifest.xml-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.feedback">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:screenOrientation="landscape">
</activity>
<activity
android:name=".Score"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我假设displayScore()
有一个非常简单的问题,但似乎无法找出它。 提前谢谢你。
你保持得分
activity作为发射者activity。 所以使用mainactivity
作为启动器activity,如下所示
null
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".Score"
android:screenOrientation="landscape">
</activity>
<activity
android:name=".MainActivity"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
还有一个建议是,存在大量代码重复,您可以创建一个函数来上传评级,并在onClickListner
的else块中调用它
你保持了第二个activity作为发射器activity,这就是为什么android忽略了你的主要活动。java
您应该将您的舱单更改为
<?xml version="1.0" encoding="utf-8"?>
null
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".Score"
android:screenOrientation="landscape">
</activity>
<activity
android:name=".MainActivity"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
而且在你的应用程序中还可以有一个改进,你可以添加回收视图而不是5个按钮,这将使你的代码更短。