如何将片段中的两个参数传递给活动
您好,我如何将片段中的两个参数传递给活动?我试图做到这一点,但我不能。 我只是裁判在TextView的editRef但是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);
}
仔细检查你使用的是相同的密钥的意图。
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。
我有一个登录表单。所以我想在所有片段和活动中显示登录。在第一个片段中,我有一个使用登录的列表视图,但是当我在列表视图中单击时,登录未显示在活动中 – clara
呃,就像我说的那样,'ViewResult.java'应该工作并显示数据,除非您最终放置用intent.putExtra(“log”,tid2);' –
'''fragment_result.xml'和'view_result.xml'实际上是一样的东西?他们都有'@ + id/et_login'? –
这应该工作,你确定日志不是空的,或当你把它放在空? – cyroxis
检查我的这个答案 http://stackoverflow.com/a/36467198/3073945 –
@ cricket_007它是两个。对不起 – clara