[英]getting data from one activity to another
i have two activities that is friends and details, the friends activity is a list view with the list data populated from a database i already created, when a list item is clicked, the details activity should be launched and the list item data carried to the details activity and put into the edit text box in the details class 我有两个活动,即朋友和详细信息,朋友活动是一个列表视图,其中列表数据是从我已经创建的数据库中填充的,单击列表项时,应启动details活动并将列表项数据携带到详细信息活动,并放入详细信息类的“编辑”文本框中
package com.rich.myfinal;
public class FriendsActivity extends Activity {
private CustomCursorAdapter customAdapter;
private PersonDataHelper databaseHelper;
private ListView listView;
private static final String TAG = FriendsActivity.class.getSimpleName();
/**
* Called when the activity is first created.
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_friends);
databaseHelper = new PersonDataHelper(this);
listView = (ListView) findViewById(R.id.list_data);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d(TAG, "clicked on item: " + position);
Intent i = new Intent(getApplicationContext(), DetailsActivity.class);
startActivity(i);
}
});
// Database query can be a time consuming task ..
// so its safe to call database query in another thread
// Handler, will handle this stuff for you <img src="http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif?m=1129645325g" alt=":)" class="wp-smiley">
new Handler().post(new Runnable() {
@Override
public void run() {
customAdapter = new CustomCursorAdapter(FriendsActivity.this, databaseHelper.getAllData());
listView.setAdapter(customAdapter);
}
});
}
}
The details activity is below 详细活动如下
package com.rich.myfinal;
public class DetailsActivity extends Activity {
EditText details;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
details = (EditText) findViewById (R.id.details);
}
}
You can do this in two ways, one is using Bundle and the another one throught Intent. 您可以通过两种方式执行此操作,一种是使用Bundle,另一种是通过Intent。
Using Bundle, 使用捆绑包,
To Send: 发送:
Intent passIntent = new Intent(CurrentActivity.this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putString("Key", "Value");
passIntent.putExtras(bundle);
startActivity(passIntent);
To Receive: 接受:
Bundle bundle = getIntent().getExtras();
String recText = bundle.getString("Key");
Using Through Intent: 使用通过意图:
To Send: 发送:
Intent passIntent = new Intent(CurrentActivity.this, SecondActivity.class);
passIntent.putExtras("Key", "Value");
startActivity(passIntent);
To Receive: 接受:
String recText = getIntent().getExtras().getString("Key");
For your Code, in your FirstActivity 对于您的代码,在您的FirstActivity中
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d(TAG, "clicked on item: " + position);
Intent passIntent = new Intent(CurrentActivity.this, SecondActivity.class);
passIntent.putExtras("Key", position);
startActivity(passIntent);
}
});
In SecondActivity,
details.setText(getIntent().getExtras().getString("Key"));
Pass values you want from FriendsActivity
to DetailsActivity
activity with Intent
like below. 使用如下所示的
Intent
将DetailsActivity
值从FriendsActivity
到DetailsActivity
活动。 Pass values from FriendsActivity when you start another activity by calling startActivity()
当您通过调用
startActivity()
启动另一个活动时,从FriendsActivity传递值
intent.putExtra("pos", v.getId()) ;
intent.putExtra("TranObject",(Serializable) data.get(v.getId()).getTranse()) ;
And then receive values in DetailsActivity
as below in onCreate()
然后在
DetailsActivity
接收值,如下所示: onCreate()
try {
pos = getIntent().getExtras().getInt("pos");
blog =(clsTranscation) getIntent().getExtras().getSerializable("TranObject");
} catch (Exception e) {
e.printStackTrace();
}
use like that 这样使用
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d(TAG, "clicked on item: " + position);
String value = listView .getItemAtPosition(position).toString();
Intent i = new Intent(getApplicationContext(), DetailsActivity.class);
i.putExtra("data", value);
startActivity(i);
}
});
use in detail Activity 详细使用活动
Bundle bundle = getIntent().getExtras();
String value= bundle.getString("data");
details = (EditText) findViewById (R.id.details);
details.setText(value)
just call String value=YourList.get(position);
只需调用
String value=YourList.get(position);
And pass this in Intent using intent.putExtra("Key","Value");
并使用
intent.putExtra("Key","Value");
将其传递给Intent intent.putExtra("Key","Value");
And get this value using Bundle bundle=getIntent.getExtra();
并使用
Bundle bundle=getIntent.getExtra();
获得此值Bundle bundle=getIntent.getExtra();
, ,
String value=bundle.getExtra("Key");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.