Android:如何将Spinner的值设置为Firebase实时数据库中的键?

huangapple 未分类评论42阅读模式
英文:

Android: How do I set spinner value as key in Firebase Realtime Database?

问题

Sure, here's the translation of your provided content:

我正在制作一个用于餐厅员工的反馈应用程序,经理可以从下拉列表中选择员工的姓名,并从评分条中为其评分(1-5分)。

我的问题是,如何使 key 成为员工姓名,而 value 成为来自Firebase实时数据库中评分条的评分。

类似于这样的结构 -

"-MD0C0rC95pBItYO73Qo" : {
     "hygiene" : 5,
     "name" : "Arpit",
     "Max" : 5,
     "Alex" : 4,
     "totalRatings" : 5
}

这里的 Max 和 Alex 是通过下拉列表选择的员工姓名。

这是我的 MainActivity -

// ...(MainActivity的代码,包括布局设置和事件处理等)

这是我的 UserHelper 类 -

// ...(UserHelper类的代码,包括属性和方法)

我只想了解将下拉列表的值分配给Firebase实时数据库键的逻辑。提前谢谢您。

Note: I've provided the translation of the non-code parts as you requested. If you need any specific explanation or further assistance regarding the logic, feel free to ask.

英文:

I am making a feedback app for restaurant employees where a manager can select the name of the employee from the spinner and give him a rating from a rating bar (1-5).

My question is how do I make the key the employee name and the value the rating from the rating bar in Firebase Realtime Database.

Something Like this -

 "-MD0C0rC95pBItYO73Qo" : {
      "hygiene" : 5,
      "name" : "Arpit",
      "Max" : 5,
      "Alex" : 4,
      "totalRatings" : 5
    }

Here Max & Alex are the employee names that are selected through a spinner list.

Here is my MainActivity -

public class MainActivity extends AppCompatActivity {
    EditText name;
    Spinner staff1Selection;
    Spinner staff2Selection;
    RatingBar hygieneRating;
    RatingBar staff1Rating;
    RatingBar staff2Rating;
    RatingBar totalRating;
    Button submit;
    FirebaseDatabase rootNode;
    DatabaseReference reference;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        name = findViewById(R.id.editTextTextPersonName2);
        staff1Selection = findViewById(R.id.spinner2);
        staff2Selection = findViewById(R.id.spinner3);
        rootNode = FirebaseDatabase.getInstance();
        reference = rootNode.getReference().child("Users");

        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.planets_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        staff1Selection.setAdapter(adapter);
        staff1Selection.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        staff2Selection.setAdapter(adapter);
        staff2Selection.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        hygieneRating = findViewById(R.id.ratingBar);
        totalRating = findViewById(R.id.ratingBar2);
        staff1Rating = findViewById(R.id.ratingBar5);
        staff2Rating = findViewById(R.id.ratingBar3);
        submit = findViewById(R.id.button);

        class OnClickListener implements View.OnClickListener {

            @Override
            public void onClick(View view) {
                if (name.length() == 0) {
                    name.setError("Please enter your full name.");
                }
                else {
                    name.setError(null);
                    final String staff1Name = staff1Selection.getSelectedItem().toString();
                    final String staff2Name = staff2Selection.getSelectedItem().toString();
                    int hygiene = (int) hygieneRating.getRating();
                    int staff1Score = (int) staff1Rating.getRating();
                    int staff2Score = (int) staff2Rating.getRating();
                    int overall = (int) totalRating.getRating();
                    String customer = name.getText().toString();
                    UserHelper helper = new UserHelper(customer, hygiene ,overall, staff1Score, staff2Score);
                    reference.push().setValue(helper);
                    Toast.makeText(MainActivity.this, "Thanks for your feedback!", Toast.LENGTH_SHORT).show();
                }
            }
        }
        submit.setOnClickListener(new OnClickListener());
    }
}

Here is my UserHelper class -

public class UserHelper {
    String name;
    String staff1;
    String staff2;
    String staff1Rating;
    int hygiene;
    int totalRatings;
    String staff2Rating;

    public UserHelper() {

    }

    public UserHelper(String name, int hygiene, int totalRatings, Spinner staff1Rating, Spinner staff2Rating) {
        this.name = name;
        this.staff1 = staff1;
        this.staff2 = staff2;
        this.hygiene = hygiene;
        this.totalRatings = totalRatings;
        this.staff1Rating = staff1Rating.getSelectedItem().toString();
        this.staff2Rating = staff2Rating.getSelectedItem().toString();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getStaff1Rating() {
        return staff1Rating;
    }

    public void setStaff1Rating(String staff1Rating) {
        this.staff1Rating = staff1Rating;
    }

    public int getHygiene() {
        return hygiene;
    }

    public void setHygiene(int hygiene) {
        this.hygiene = hygiene;
    }

    public int getTotalRatings() {
        return totalRatings;
    }

    public void setTotalRatings(int totalRatings) {
        this.totalRatings = totalRatings;
    }

    public String getStaff2Rating() {
        return staff2Rating;
    }

    public void setStaff2Rating(String staff2Rating) {
        this.staff2Rating = staff2Rating;
    }
}

I would just like to know the logic behind assigning spinner values to the Firebase realtime database keys. Thanks in advance.

huangapple
  • 本文由 发表于 2020年7月24日 22:45:17
  • 转载请务必保留本文链接:https://java.coder-hub.com/63075968.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定