我为android中的微调器做了一个简单的演示,其中2个活动在第一个活动中有一个简单的微调器,第二个是textView.So当微调器中的任何项目被选中时它将显示在第二个Activity上,我尝试了以下代码,但它不起作用:
Activity1.java
package com.example.sppiner;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
public class MainActivity extends Activity {
Spinner sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp=(Spinner)findViewById(R.id.spinner1);
String[] s={"ahmedabad","baroda","chitor","daman","england","france","georgia"};
sp.setAdapter(new ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item,s));
sp.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String n = (String)arg0.getItemAtPosition(arg2);
Intent i =new Intent(MainActivity.this,Act2.class);
Bundle b =new Bundle();
i.putExtra("nat", n);
startActivity(i);
}
});
}
}
act2.java
package com.example.sppiner;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;
public class Act2 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_act2);
TextView tv =(TextView)findViewById(R.id.tv1);
Bundle b = getIntent().getExtras();
Intent i =getIntent();
String nat = b.getString("nat");
tv.setText(nat);
}
}
请帮助我......提前致谢
答案 0 :(得分:3)
尝试将setOnItemSelectedListener
用于spinner
cat.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
String n = (String)arg0.getItemAtPosition(arg2);
Intent i =new Intent(MainActivity.this,Act2.class);
Bundle b =new Bundle();
i.putExtra("nat", n);
startActivity(i);
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
和第二项活动
String city =b.getString("nat");
使用此行而不是
String city =b.getString("city");
答案 1 :(得分:1)
在您的Ist活动中,请执行以下操作:
sp.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String n = (String)arg0.getItemAtPosition(arg2);
Intent i =new Intent(MainActivity.this,Act2.class);
i.putExtra("nat", n);
startActivity(i);
}
});
并且,在您的第二项活动中,请执行以下操作:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_act2);
TextView tv =(TextView)findViewById(R.id.tv1);
String nat = getIntent().getStringExtra("nat");
tv.setText(nat);
}
答案 2 :(得分:0)
你可以尝试一下。
您需要在之前key
中按给定activity
获取值。即nat
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity2);
tv=(TextView)findViewById(R.id.textView1);
Intent i =getIntent();
Bundle b =i.getExtras();
String city =b.getStringExtras("nat");
tv.setText(city);
}