您好,我如何将两个参数从片段传递给活动?我试着这样做,但我做不到。 我只在TextView editRef中得到了ref,但TextView et_login是空的
public class ResultFragmentC extends Fragment implements ListView.OnItemClickListener {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fragment_result, container, false);
et_login = (TextView) rootview.findViewById(R.id.et_login);
Intent intent = getActivity().getIntent();
String log = intent.getStringExtra("login");
et_login.setText(log);
......
}
......
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getActivity().getApplicationContext(), ViewResult.class);
HashMap<String, String> map = (HashMap) parent.getItemAtPosition(position);
String tid = map.get("ref");
intent.putExtra("ref", tid);
String tid2 = map.get("log");
intent.putExtra("log", tid2);
startActivity(intent);
}
ViewResult.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_result);
Intent intent = getIntent();
layout=(LinearLayout) findViewById(R.id.layout);
String ref2 = intent.getStringExtra("ref");
et_login = (TextView) findViewById(R.id.et_login);
String log = intent.getStringExtra("log");
et_login.setText(log);
editRef = (TextView) findViewById(R.id.editRef);
editRef.setText(ref2);
}
答案 0 :(得分:0)
仔细检查您是否正在使用相同的密钥进行意图。
Intent intent = getActivity().getIntent();
String log = intent.getStringExtra("login");
与
不一样Intent intent = getIntent();
String log = intent.getStringExtra("log");
ViewResult.java
中显示的内容应该与onItemClick
假设map.get()
没有回复null
的内容有关。
应该提到的是,使用Activity意图并不是如何为Fragments提供参数。请参阅Best practice for instantiating a new Android Fragment。