简体   繁体   中英

Passing value of rating bar to next activity/intent

Problem: I cannot pass the rating value/data from the rating bar to another intent. It only shows me this instead of the value/data from the rating bar.

Rating: MainActivity@79b604

I think the error is that it did not properly get the value from the rating bar or it did not pass the value correctly. Here is my code:

MainActivity.java

public class MainActivity extends AppCompatActivity {
    String rating_float = "0.0";
    private TextView txtRatingValue;

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

    RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);

    ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
            rating_float = String.valueOf(rating);
        }
    });
}

public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Context context = getApplicationContext();
    CharSequence text = "";

    Intent intent_rating = new Intent(this, SecondActivity.class);
    intent_rating.putExtra("rating_float", toString());
    startActivity(intent_rating);

    return super.onOptionsItemSelected(item);
}}

SecondActivity.java

public class SecondActivity extends AppCompatActivity {
private TextView Msg;
String PassedValue = null;

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

    PassedValue = getIntent().getStringExtra("rating_float");
    Msg = (TextView) findViewById(R.id.Msg);
    Msg.setText("Dimension: 479\nFormat: JPEG\nSize: 360x360\nRating: " + PassedValue);
}

Questions:

  1. Am I doing the right way on getting the value for the rating bar?
  2. How to pass the value/data from the rating bar into the next activity/intent and how to get it from the SecondActivity.java?

That is because you pass the result of MainActivity.toString() to your second activity

Instead of

intent_rating.putExtra("rating_float", toString());

you should presumably write

intent_rating.putExtra("rating_float", this.rating_float);

You are receiving it well but not sending what you have to.

Just use this:

intent_rating.putExtra("rating_float", rating_float);

First you need to save your value into the putExtra of the intent you use to go to the another activity

intent_rating.putExtra("rating_float", rating_float);

to get the value in the SecondActivity you just need to do this wherever you need the value.

 Intent iin= getIntent();
        Bundle b = iin.getExtras();

        if(b!=null)
        {

            String rating_value =(String) b.get("rating_float");
}

Simple and Clear Solution

//1stActivity

    ok.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            float rating = rate.getRating();

            Intent intent = new Intent(ContactActivity.this,RatingIntentSOF.class);

            Bundle bundle = new Bundle();

            bundle.putFloat("totalRating",rating);

            intent.putExtras(bundle);

            startActivity(intent);

//2nd Activity, get the data of 1st Activity.

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

    TextView textView = (TextView) findViewById(R.id.text);

    Intent intent = getIntent();

    Bundle bundle = intent.getExtras();

    float totalRating = bundle.getFloat("totalRating");

    textView.setText(String.valueOf(totalRating));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM