简体   繁体   English

将评分栏的值传递给下一个活动/意图

[英]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 评分: 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 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 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? 如何将值/数据从评分栏中传递到下一个活动/意图中,以及如何从SecondActivity.java中获取它?

That is because you pass the result of MainActivity.toString() to your second activity 那是因为您将MainActivity.toString()的结果传递给第二个活动

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 首先,您需要将值保存到用于其他活动的意图的putExtra

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. 要在SecondActivity中获取值,只需在需要该值的地方执行此操作。

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

        if(b!=null)
        {

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

Simple and Clear Solution 简单明了的解决方案

//1stActivity // 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));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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