Android:如何将Spinner的值发送到我的UserHelper对象?

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

Android: How do I send a Spinner value to my UserHelper object?

问题

You can send the text value to the UserHelper object by making it a member variable of your MainActivity class. Here's the modified code to achieve this:

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    // ... (other member variables)

    String comment; // Declare 'comment' as a member variable

    // ... (rest of your code)

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        name = findViewById(R.id.editTextTextPersonName2);
        stylistSelection = findViewById(R.id.spinner);

        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);
        stylistSelection.setAdapter(adapter);
        stylistSelection.setOnItemSelectedListener(this); // Use setOnItemSelectedListener instead of setOnItemClickListener

        // ... (rest of your code)
    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        comment = adapterView.getItemAtPosition(i).toString(); // Set the 'comment' variable when an item is selected in the spinner
    }

    // ... (rest of your code)
}

In this code, I've declared comment as a member variable at the beginning of the MainActivity class. Then, in the onItemSelected method, I assign the selected spinner item to the comment variable. Now, you can use this comment variable when creating the UserHelper object later in your code.

英文:

I have created an app that collects information in the form of rating bar and a spinner. I want to send the ratings and the spinner item selected to my UserHelper object for Firebase realtime database.

Here is the code:

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    EditText name;
    Spinner stylistSelection;
    RatingBar hygieneRating;
    RatingBar serviceRating;
    RatingBar totalRating;
    Button submit;
    FirebaseDatabase rootNode;
    DatabaseReference reference;
    Handler handler = new Handler();
    Intent intent;
    String comment;

    public void displayComment() {
        intent = new Intent(getApplicationContext(), ratings.class);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        name = findViewById(R.id.editTextTextPersonName2);
        stylistSelection = findViewById(R.id.spinner);

        ArrayAdapter&lt;CharSequence&gt; adapter = ArrayAdapter.createFromResource(this, R.array.planets_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        stylistSelection.setAdapter(adapter);
        stylistSelection.setOnItemClickListener(this);

        hygieneRating = findViewById(R.id.ratingBar3);
        totalRating = findViewById(R.id.ratingBar2);
        serviceRating = findViewById(R.id.ratingBar);
        submit = findViewById(R.id.button);
        final String stylistName = stylistSelection.getSelectedItem().toString();
        final String hygiene = String.valueOf(hygieneRating.getRating());
        final String service = String.valueOf(serviceRating.getRating());
        final String overall = String.valueOf(totalRating.getRating());


        class OnClickListener implements View.OnClickListener {

            @Override
            public void onClick(View view) {
                if ((name.length() == 0) &amp;&amp; stylistName.isEmpty())  {
                    name.setError(&quot;Please enter your full name.&quot;);
                    ((TextView)stylistSelection.getSelectedView()).setError(&quot;Please select a stylist.&quot;);
                }
                else {
                    name.setError(null);
                    ((TextView)stylistSelection.getSelectedView()).setError(null);
                    rootNode = FirebaseDatabase.getInstance();
                    reference = rootNode.getReference().child(&quot;Users&quot;);
                    String customer = name.getText().toString();
                    int hygieneScore = Integer.parseInt(hygiene);
                    int serviceScore = Integer.parseInt(service);
                    int totalScore = Integer.parseInt(overall);
                    UserHelper helper = new UserHelper(customer, text, hygieneScore, serviceScore, totalScore, comment); // Need the spinner value here
                    reference.push().setValue(helper);
                    Toast.makeText(MainActivity.this, &quot;Thanks for your feedback!&quot;, Toast.LENGTH_SHORT).show();
                    Runnable runnable = new Runnable() {
                        @Override
                        public void run() {
                            displayComment();
                        }
                    };
                    handler.postDelayed(runnable, 3000);
                }
            }
        }
    }

    @Override
    public void onItemSelected(AdapterView&lt;?&gt; adapterView, View view, int i, long l) {
        String text = adapterView.getItemAtPosition(i).toString();
    }
    
    @Override
    public void onNothingSelected(AdapterView&lt;?&gt; adapterView) {
    }
}

My question how do I send this text value to the UserHelper object above.
Thanks for any help in advance.

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

发表评论

匿名网友

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

确定