我需要通过get intent从两个活动中获取数据但不起作用。
这个activiy1:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long viewId) {
TextView idTextView = (TextView) view.findViewById(R.id.id);
TextView titleTextView = (TextView) view.findViewById(R.id.title);
TextView descTextView = (TextView) view.findViewById(R.id.desc);
String id = idTextView.getText().toString();
String title = titleTextView.getText().toString();
String desc = descTextView.getText().toString();
Intent modify_intent = new Intent(getApplicationContext(), ShowDat.class);
modify_intent.putExtra("title1", title);
modify_intent.putExtra("desc1", desc);
modify_intent.putExtra("id1", id);
startActivity(modify_intent);
}
});
&#13;
和此活动2:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long viewId) {
TextView idTextView = (TextView) view.findViewById(R.id.id);
TextView titleTextView = (TextView) view.findViewById(R.id.title);
TextView descTextView = (TextView) view.findViewById(R.id.desc);
String id = idTextView.getText().toString();
String title = titleTextView.getText().toString();
String desc = descTextView.getText().toString();
Intent modify_intent = new Intent(getApplicationContext(), ShowDat.class);
modify_intent.putExtra("title2", title);
modify_intent.putExtra("desc2", desc);
modify_intent.putExtra("id2", id);
startActivity(modify_intent);
}
});
&#13;
此活动比接收数据:
TextView idTextView = (TextView) findViewById(R.id.id);
TextView titleTextView = (TextView) findViewById(R.id.title);
TextView descTextView = (TextView) findViewById(R.id.desc);
Intent intent = getIntent();
if(intent != null) {
intent = getIntent();
String id = intent.getStringExtra("id1");
String name = intent.getStringExtra("title1");
String desc = intent.getStringExtra("desc1");
try {
_id = Long.parseLong(id);
} catch (NumberFormatException e) {
_id = 0; // your default value
}
idTextView.setText(id);
titleTextView.setText(name);
descTextView.setText(desc);
}
else {
intent = getIntent();
String id = intent.getStringExtra("id2");
String name = intent.getStringExtra("title2");
String desc = intent.getStringExtra("desc2");
try {
_id = Long.parseLong(id);
} catch (NumberFormatException e) {
_id = 0; // your default value
}
idTextView.setText(id);
titleTextView.setText(name);
descTextView.setText(desc);
}
&#13;
我尝试使用此代码,但是就像上面的代码一样,只是放了额外的接收而其他不能正常工作:
TextView idTextView = (TextView) findViewById(R.id.id);
TextView titleTextView = (TextView) findViewById(R.id.title);
TextView descTextView = (TextView) findViewById(R.id.desc);
Intent intent = getIntent();
if(getIntent() !=null) {
String id = intent.getStringExtra("id1");
String name = intent.getStringExtra("title1");
String desc = intent.getStringExtra("desc1");
try {
_id = Long.parseLong(id);
} catch (NumberFormatException e) {
_id = 0; // your default value
}
idTextView.setText(id);
titleTextView.setText(name);
descTextView.setText(desc);
}
else {
String id = intent.getStringExtra("id2");
String name = intent.getStringExtra("title2");
String desc = intent.getStringExtra("desc2");
try {
_id = Long.parseLong(id);
} catch (NumberFormatException e) {
_id = 0; // your default value
}
idTextView.setText(id);
titleTextView.setText(name);
descTextView.setText(desc);
}
&#13;
请帮忙。
答案 0 :(得分:0)
首先你需要像这样检查
if(getIntent().hasExtra("key_1"))
{
String data = getIntent().getString("key_1");
}
else if(getIntent().hasExtra("key_2"))
{
String msg = getIntent().getString("key_2");
}
......
答案 1 :(得分:0)
你可以用两种方式做到这一点。
1)if(getIntent().hasExtra("your_key")
)和
2)在两个活动中使用相同的密钥来传递数据
喜欢modify_intent.putExtra("same_key", title);