我有一个包含2个片段的活动。既然已经实现了将数据从一个Fragment发送到我的Activity的接口,我想在Textview中显示我的Data(字符串)。但我怎么能这样做? 我有方法:
@Override
public void sendText(String dataInput) {
}
但我怎样才能访问我的字符串?
MainActivity:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, AddDataFragment.OnFragmentInteractionListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.scanbutton);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Hier kann man später QR-Codes scannen", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Fragment fragment;
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
fragment = new ListViewFragment();
ft.replace(R.id.container, fragment);
ft.addToBackStack(null);
ft.commit();
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
Fragment fragment;
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
fragment = new ListViewFragment();
int id = item.getItemId();
if (id == R.id.nav_listview) {
fragment= new ListViewFragment();
} else if (id == R.id.nav_add_data) {
fragment= new AddDataFragment();
} else if (id == R.id.nav_settings) {
} else if (id == R.id.nav_legal_information) {
fragment = new LegalInformationFragment();
}
ft.replace(R.id.container, fragment);
ft.addToBackStack(null);
ft.commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void sendText(String dataInput) {
return;
}
}
AddDataFragment:
public class AddDataFragment extends Fragment {
Button buttonadd;
Button buttondelete;
private EditText inputProduct;
private EditText inputLabel;
private EditText inputSerial;
private EditText inputMac;
private EditText inputDaaId;
private EditText inputBill;
private TextView listElement;
private OnFragmentInteractionListener mListener;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_add_data, container, false);
inputProduct = (EditText) view.findViewById(R.id.editText_product);
inputLabel = (EditText) view.findViewById(R.id.editText_label);
inputSerial = (EditText) view.findViewById(R.id.editText_serial);
inputMac = (EditText) view.findViewById(R.id.editText_mac);
inputDaaId = (EditText) view.findViewById(R.id.editText_daa_id);
inputBill = (EditText) view.findViewById(R.id.editText_bill);
buttonadd = (Button) view.findViewById(R.id.button_add_addData);
buttondelete = (Button) view.findViewById(R.id.button_delete_addData);
final String Bsp = "BEISPIELEINTRAG 1234214124";
buttonadd.setEnabled(true);
buttondelete.setEnabled(true);
buttonadd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String dataInput = inputProduct.getText().toString() + inputLabel.getText().toString() +
inputSerial.getText().toString() + inputMac.getText().toString() +
inputDaaId.getText().toString() + inputBill.getText().toString();
mListener.sendText(dataInput);
}
});
return view;
}
public interface OnFragmentInteractionListener {
void sendText(String dataInput);
}
}
我只想将AddDataFragment中的数据发送到ListViewFragment。有人说这将是通过接口进行通信的最佳方式......
编辑:我是否正确,我只能在我的MainACtivity中的sendText方法中使用AddFragments数据?问题是如何显示此字符串。抱歉初学者问题......
答案 0 :(得分:0)
android中的接口用于来自“孩子”的回调。到一个父母'。例如,您的活动会创建一个片段,并告诉他他是实现OnFragmentInteractionListener
的委托。然后,片段可以通过调用mListener.sendText(dataInput)
来触发活动中的代码。优异。
但是,因为您想将数据发送到另一个片段(而不是父活动),所以这不是一个好方法。
Activity
/ \
Fragment A Fragment B
从Activity到片段A和后面很容易进行通信。同样对于B,但是直接从片段A到B进行通信并不容易。至少没有接口。
最好暂时将dataInput
存储在sharedpreferences(或其他地方)中,以便将它们存储在AddDataFragment中,并在ListViewFragment中轻松检索它们。