2012-10-08 24 views
0

我想从一个页面中的值传递到另一个网页我的代码是:我试图将值从一个页面传递到另一个页面,但它不起作用?

public class main extends Activity implements OnClickListener { 

private static final String TAG = "AskMeShowMe2"; 
private String choice = null; 
private String fname = null; 
private Menu myMenu = null; 
final static int START =0; 



@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Log.i(TAG,"Main - onCreate() ..."); 
    setContentView(R.layout.activity_main); 

    View promptButton = findViewById(R.id.submit_button); 
    promptButton.setOnClickListener((OnClickListener) this); 
} 

public void onClick(View v) { 
    RadioGroup radioGroup = (RadioGroup) findViewById(R.id.greetingsRadioGroup); 
    int checkedRadioButton = radioGroup.getCheckedRadioButtonId(); 

    switch (checkedRadioButton) { 
    case R.id.mrButton : 
     choice = "Mr."; 
     break; 
    case R.id.mrsButton : 
     choice = "Mrs."; 
     break; 
    case R.id.msButton: 
     choice = "Ms."; 
     break; 
    case R.id.drButton: 
     choice = "Dr."; 
     break; 
    } 
    switch(v.getId()){ 
    case R.id.submit_button: 
     Intent j = new Intent(this, Info.class); 
     j.putExtra("choice", choice); 
     startActivity(j); 
     fname=String.valueOf(R.id.firstname); 
     Intent t = new Intent(this, Info.class); 
     j.putExtra("fname", fname); 
     startActivity(t); 

     break; 
    } 
} 

和它的其他Info.Class:

public class Info extends Activity { 
private static final String TAG = "Info"; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.info); 

    Intent myIntent = getIntent(); 
    Bundle b = myIntent.getExtras(); 
    String choice = b.getString("choice"); 
    String fname = b.getString("fname"); 

    Log.i(TAG,"selection: " + choice); 
    TextView textView = (TextView) findViewById(R.id.showmeText); 
    textView.append(": " + choice); 
    TextView textView1 = (TextView) findViewById(R.id.fname); 
    textView1.append(": " + fname); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.my_menu, menu); 
    return true; 
} 

}

当我运行这个程序,然后我的Info.class页面不显示当我点击提交按钮,你能帮我解决它吗?

回答

1

改变开关块以下几点:

switch(v.getId()){ 
    case R.id.submit_button: 
     Intent j = new Intent(this, Info.class); 
     j.putExtra("choice", choice); 
     fname=String.valueOf(R.id.firstname); 
     j.putExtra("fname", fname); 
     startActivity(j); 

     break; 
    } 

当你开始活动信息两次,把群众演员只有一个意向,目前正在起步较早。

1

使用

Intent j = new Intent(this, Info.class); 
j.putExtra("choice", choice); 
fname=String.valueOf(R.id.firstname); 
j.putExtra("fname", fname); 
startActivity(j); 

,而不是

Intent j = new Intent(this, Info.class); 
    j.putExtra("choice", choice); 
    startActivity(j); 
    fname=String.valueOf(R.id.firstname); 
    Intent t = new Intent(this, Info.class); 
    j.putExtra("fname", fname); 
    startActivity(t); 

监守目前你第一次为intent jintent t秒时间两次调用startActivity因此将其删除一个,把两个选择,FNAME单意图和只调用一次startActivity。

+0

感谢它的工作 – user1050667

+0

@ user1050667:最受欢迎的朋友!!! –

+0

以上ans正在运行! –

相关问题