Java 通过包传递数据
我想通过以下方式发送数据:Java 通过包传递数据,java,android,bundle,Java,Android,Bundle,我想通过以下方式发送数据: public static final String Itemmm = "tryIT"; String item3 = (String)arrayAdapter.getItem(position).toString(); ClassicsA class = new Classics(); Bundle bundle2 = new Bundle(); bundle2.putString(ClassicsB.Itemmm, item3); class.setArgume
public static final String Itemmm = "tryIT";
String item3 = (String)arrayAdapter.getItem(position).toString();
ClassicsA class = new Classics();
Bundle bundle2 = new Bundle();
bundle2.putString(ClassicsB.Itemmm, item3);
class.setArguments(bundle2);
要接收数据,我需要执行以下操作:
Bundle bundle2 = this.getArguments();
if(bundle2!=null){
String i = bundle2.getString(ClassicsB.Itemmm, "");
testButton.setText(i);
}
然而,数据从未被发送,当我检查testButton的值时,它是空的。你能看出我做错了什么吗?当我在括号中添加item3时,它只将item3作为字符串
顺便说一句,这是两个片段之间的通信。要发送数据,请使用以下代码行:
Bundle bundle = new Bundle();
bundle.putString("KEY", "VALUE");
// set Fragmentclass Arguments
Fragmentclass fragobj = new Fragmentclass();
fragobj.setArguments(bundle);
为了接收数据,请使用以下命令:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext = getArguments().getString("KEY");
return inflater.inflate(R.layout.fragment, container, false);
}
希望这有帮助