[英]How to send data from main activity to fragment
public class MyActivity extends Activity implements ButtonFragement.OnFragmentInteractionListener, TextFragment.OnFragmentInteractionListener,Communicator {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
ButtonFragement btnfrg=new ButtonFragement();
TextFragment txtfrg= new TextFragment();
FragmentManager fm=getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
ft.add(R.id.my_activity,btnfrg,"Fragment");
ft.add(R.id.my_activity,txtfrg,"Second Fragment");
ft.commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onFragmentInteraction(Uri uri) {
}
@Override
public void respond(String data) {
FragmentManager fm=getFragmentManager();
TextFragment f1= (TextFragment) fm.findFragmentById(R.id.textfrg);
f1.changeText(data);
}
}
这是我的main_Activity代码,在这里我试图通过片段发送数据,但是它给我f1.changeText(data)错误。我项目的基本结构是在主Activity上创建了两个片段。 一个带有按钮,另一个带有文本。 我想显示使用通信器界面在第二个片段上单击按钮的次数。 这里在“数据”计数器中显示了单击了多少次按钮,但是我无法在第二个片段上传输它。
该程序的完整代码-
public interface Communicator {
public void respond(String data);
}
在TextFragment
类中,我添加了此方法----
public void changeText(String data)
{
txt.setText(data);
}
在ButtonFragment
类中,我添加并修改了以下方法
public class ButtonFragement extends Fragment implements View.OnClickListener{
int counter=0;
private OnFragmentInteractionListener mListener;
Button btn;
Communicator comm;
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
comm= (Communicator) getActivity();
btn= (Button) getActivity().findViewById(R.id.button);
btn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
counter++;
// comm.respond("The button was clicked "+counter+" times");
comm.respond("hi");
}
在这里,我只是添加了我在程序中添加的内容。 我的程序在...崩溃MainActiviy f1.changeText(data);
但是为什么我没有得到它?有人可以帮我解决这个错误吗?
Bundle bundle = new Bundle();
bundle.putString("key", value);
// set Fragmentclass Arguments
YourFragment ff= new YourFragment ();
ff.setArguments(bundle);
transaction.add(R.id.my_activity, ff);
使用捆绑
来自活动:
Bundle bundle = new Bundle();
bundle.putString("message", "Hello!");
FragmentClass fragInfo = new FragmentClass();
fragInfo.setArguments(bundle);
transaction.replace(R.id.fragment_single, fragInfo);
transaction.commit();
分段:
读取片段中的值
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
String myValue = this.getArguments().getString("message");
...
...
}
您没有ID为R.id.textfrg
片段。 您出于某种原因要添加两个ID为R.id.my_activity
片段。 一个带有标签"Fragment"
,另一个带有标签"Second Fragment"
。 因此,您遇到了错误。 你的想法是严厉的。 这可能会有所帮助
TextFragment f1= (TextFragment) fm.findFragmentByTag("Second Fragment");
这样做:
在活动方面。
Fragment fragment = new GridTemplate(); Bundle bundle = new Bundle(); bundle.putInt("index",your value); fragment.setArguments(bundle); FragmentManager fragmentManager = getFragmentManager(); fragmentManager.beginTransaction() .replace(R.id.frame_container, fragment).commit();
在这里R.id.frame_container是一个框架布局或相对布局,您必须在其中添加片段。
在片段一侧。
int index = getArguments().getInt("index");
希望这能解决您的问题。 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.