如何从此活动传递额外数据?
public class DetailActivity extends Activity {
@Override
public void onBackPressed() {
}
@SuppressLint("NextAPI")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
Button btnForum = (Button) findViewById(R.id.btnForum);
Button btnKuis = (Button) findViewById(R.id.btnKuis);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
showInfo();
final Button btnLogout = (Button) findViewById(R.id.btnBack);
btnLogout.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent newActivity = new Intent(DetailActivity.this,MainActivity.class);
startActivity(newActivity);
}
});
btnForum.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent forum = new Intent(DetailActivity.this, Forum.class); ;
startActivity(forum);
}
});
btnKuis.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent kuis = new Intent(DetailActivity.this, Kuisioner.class); ;
startActivity(kuis);
}
});
}
public void showInfo(){
final TextView tNrp = (TextView)findViewById(R.id.txtNrp);
final TextView tName = (TextView)findViewById(R.id.txtName);
final TextView tSmt = (TextView)findViewById(R.id.txtSmt);
final TextView tStatus = (TextView)findViewById(R.id.txtStatus);
String url = "http://xxx/blabla/getID.php";
Intent intent = getIntent();
String MhsID = intent.getStringExtra("idm");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("sMhsID", MhsID));
String resultServer = getHttpPost(url,params);
String strMemberID = "";
String strNrp = "";
String strPassword = "";
String strName = "";
String strSmt = "";
String strStatus = "";
JSONObject c;
try {
c = new JSONObject(resultServer);
strNrp = c.getString("idm");
strName = c.getString("nama-mahasiswa");
strSmt = c.getString("semester");
strStatus = c.getString("status-mahasiswa");
if(!strMemberID.equals(""))
{
tNrp.setText(strNrp);
tName.setText(strName);
tSmt.setText(strSmt);
tStatus.setText(strStatus);
}
else
{
tNrp.setText("-");
tName.setText("-");
tSmt.setText("-");
tStatus.setText("-");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getHttpPost(String url,List<NameValuePair> params) {
StringBuilder str = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = client.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) { // Status OK
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
str.append(line);
}
} else {
Log.e("Log", "Failed to download result..");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str.toString();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
此 DetailActivity 已经有来自上一个活动的额外数据,但我需要传递额外的数据&#34; idm&#34;到 Kuisioner 活动。任何人都可以教我如何做到这一点?请这对我来说非常重要:(
答案 0 :(得分:0)
答案 1 :(得分:0)
将String MhsID
声明为类级成员字段,如
public class DetailActivity extends Activity {
private String MhsID = null;
现在在showInfo中获取Intent数据,如
public void showInfo(){
....
Intent intent = getIntent();
MhsID = intent.getStringExtra("idm");
....
并使用MhsID
通过Kuisioner
将putExtra()
的值传递给Intent
活动,就像您之前的活动中所做的那样
btnKuis.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent kuis = new Intent(DetailActivity.this, Kuisioner.class);
kuis.putExtra("idm",MhsID);
startActivity(kuis);
}
});
并在String idm
活动中获得与此活动相同的Kuisioner
。
答案 2 :(得分:0)
所有解释都是HERE。
只需添加以下代码:
intent.putExtra(name, value);
因此,开始新的 Kuisioner 活动应如下所示:
String value = "Example value"
Intent kuis = new Intent(DetailActivity.this, Kuisioner.class);
kuis.putExtra("idm", value);
startActivity(kuis);
答案 3 :(得分:-1)
如果您正在使用太多变量,并希望在活动之间传递数据,我建议您将Activity-variables static ,并使用类名,不再维护变量。
注意:我假设你正在使用Fragments,你可能会再次回到活动n。