问题:我无法将评级值/数据从评级栏传递给其他意图。它只显示了这个而不是评级栏中的值/数据。
评级: MainActivity @ 79b604
我认为错误是它没有从评级栏中正确获取值,或者它没有正确传递值。这是我的代码:
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);
}
问题:
答案 0 :(得分:2)
这是因为您将MainActivity.toString()的结果传递给第二个活动
而不是
intent_rating.putExtra("rating_float", toString());
你可能应该写
intent_rating.putExtra("rating_float", this.rating_float);
答案 1 :(得分:0)
你收到的很好但没有发送你所拥有的东西。
请使用:
intent_rating.putExtra("rating_float", rating_float);
答案 2 :(得分:0)
首先,您需要将值保存到用于转到另一个活动的意图的putExtra
intent_rating.putExtra("rating_float", rating_float);
要获取SecondActivity中的值,只需要在需要值的地方执行此操作。
Intent iin= getIntent();
Bundle b = iin.getExtras();
if(b!=null)
{
String rating_value =(String) b.get("rating_float");
}
答案 3 :(得分:0)
简单明了的解决方案
// 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);
//第二个活动,获取第一个活动的数据。
@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));
}