2015-10-21 32 views
0

我得到了一個有2個片段的活動。既然實現了將數據從一個片段發送到我的活動的接口,我想在Textview中顯示我的數據(字符串)。但我怎麼能做到這一點? Ive得到的方法:如何使用接口接收的字符串

@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中使用AddText方法中的AddFragments數據?問題是如何顯示這個字符串。對於初學者問題抱歉...

+0

我不知道你的意思。該字符串是一個傳遞到sendText的參數..你可能會添加更多的代碼,並說哪些代碼屬於活動,哪些代碼片段 –

+0

如果在Activity中創建了Fragment,則不需要接口來訪問它數據。此外,活動必須「實施」接口以傳遞信息。 – luiscosta

回答

0

android中的接口用於從'子'到'父'的回調。例如,您的活動會創建一個片段並告訴他他是實施OnFragmentInteractionListener的代表。然後片段可以通過調用mListener.sendText(dataInput)來觸發活動中的代碼。優秀。

但是,因爲您想將數據發送到另一個片段(而不是父級活動),所以這不是一個好的方法。

很容易從Activity到片段A進行通信並返回。對於B也是如此,但直接從片段A到B進行通信並不容易。至少不用接口。

您可能最好暫時將dataInput存儲在sharedpreferences(或其他地方)中,以便您可以將它們存儲在AddDataFragment中,並輕鬆地將它們檢索到ListViewFragment中。

+0

所以你說我不應該使用接口。相反,我應該保存我的輸入(例如)sharedpreferences? – Rastaman

+1

@Rastaman它取決於你對ListViewFragment中的數據做些什麼,但是如果你只是想在片段變得可見時顯示數據,那麼是 –