2017-09-06 86 views
-1

我需要发送一个值,当我点击一个在recyclerview中的行到另一个片段时(我在适配器中的O​​nBind上瘦了)。然后,我需要关闭包含recyclerview的片段并打开接收器片段。我读了一些我们可以使用bundle的地方,但是我对android编程非常陌生,所以我很困惑。如何从Recyclerview适配器将值从一个片段发送到另一个片段。然后关闭第一个片段并打开新的片段

这是片段包含RecyclerView

public class PasienFragment extends Fragment { 
    // TODO: Rename parameter arguments, choose names that match 
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER 
    private static final String ARG_PARAM1 = "param1"; 
    private static final String ARG_PARAM2 = "param2"; 

    // TODO: Rename and change types of parameters 
    private String mParam1; 
    private String mParam2; 
    private OnPasienFragmentInteractionListener mPasienListener; 

    public PasienFragment() { 
     // Required empty public constructor 
    } 
    int countICU=0; 
    int countINAP=0; 
    int countUMUM=0; 
    int countBPJS=0; 

    RequestQueue queue; 
    String jenisAsuransi,jenisKamar; 
    String id="C0E0607E"; 
    ImageView ivTotal,ivIcu,ivInap,ivBpjs,ivUmum; 
    TextView tvTOTAL,tvICU,tvINAP,tvBPJS,tvUMUM; 

    public static final String URL_JUMLAH = "http:"; 

    public static PasienFragment newInstance(String param1, String param2) { 
     PasienFragment fragment = new PasienFragment(); 
     Bundle args = new Bundle(); 
     args.putString(ARG_PARAM1, param1); 
     args.putString(ARG_PARAM2, param2); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     if (getArguments() != null) { 
      mParam1 = getArguments().getString(ARG_PARAM1); 
      mParam2 = getArguments().getString(ARG_PARAM2); 
     } 
     queue = Volley.newRequestQueue(getActivity()); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     final View view = inflater.inflate(R.layout.fragment_pasien, container, false); 
     ivTotal = (ImageView) view.findViewById(R.id.ivTotal); 
     ivIcu = (ImageView) view.findViewById(R.id.ivICU); 
     ivBpjs = (ImageView) view.findViewById(R.id.ivBPJS); 
     ivInap = (ImageView) view.findViewById(R.id.ivINAP); 
     ivUmum = (ImageView) view.findViewById(R.id.ivUMUM); 

     tvTOTAL = (TextView) view.findViewById(R.id.tvTotal); 
     tvICU = (TextView) view.findViewById(R.id.tvICU); 
     tvBPJS = (TextView) view.findViewById(R.id.tvBPJS); 
     tvINAP = (TextView) view.findViewById(R.id.tvINAP); 
     tvUMUM = (TextView) view.findViewById(R.id.tvUMUM); 

     ivTotal.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       mPasienListener.onPasienFragmentInteraction("Daftar Pasien"); 
      } 
     }); 

     ivIcu.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       mPasienListener.onPasienFragmentInteraction("Daftar Pasien ICU"); 
      } 
     }); 

     ivBpjs.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       mPasienListener.onPasienFragmentInteraction("Daftar Pasien BPJS"); 
      } 
     }); 

     ivInap.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       mPasienListener.onPasienFragmentInteraction("Daftar Pasien INAP"); 
      } 
     }); 

     ivUmum.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       mPasienListener.onPasienFragmentInteraction("Daftar Pasien UMUM"); 
      } 
     }); 

     String url1 = URL_JUMLAH+id; 
     JsonObjectRequest req = new JsonObjectRequest(url1, null, 
       new Response.Listener<JSONObject>() { 
        @Override 
        public void onResponse(JSONObject response) { 
         JSONArray users = null; 
         try { 
          users = response.getJSONArray("result1"); 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 
         for (int i = 0; i < users.length(); i++) { 
          try { 
           JSONObject obj = users.getJSONObject(i); 
           jenisAsuransi = (String) obj.get("Jenis_Asuransi"); 
           jenisKamar = (String) obj.get("Jenis_Kamar"); 
           if(jenisAsuransi.equals("BPJS")){ 
            countBPJS++; 
           } 
           if(jenisAsuransi.equals("UMUM")){ 
            countUMUM++; 
           } 
           if(jenisKamar.equals("INAP")){ 
            countINAP++; 
           } 
           if(jenisKamar.equals("ICU")){ 
            countICU++; 
           } 
          } catch (JSONException e) { 
           e.printStackTrace(); 
          } 
         } 
         tvTOTAL.setText(String.valueOf(users.length())); 
         tvBPJS.setText(String.valueOf(countBPJS)); 
         tvUMUM.setText(String.valueOf(countUMUM)); 
         tvINAP.setText(String.valueOf(countINAP)); 
         tvICU.setText(String.valueOf(countICU)); 
        } 
       }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       //Toast.makeText(Home.this,"Terjadi Kendala Koneksi",Toast.LENGTH_LONG).show(); 
      } 
     }); 
     queue.add(req); 

     return view; 
    } 

    // TODO: Rename method, update argument and hook method into UI event 
    public void onButtonPressed(String sentence) { 
     if (mPasienListener != null) { 
      mPasienListener.onPasienFragmentInteraction(sentence); 
     } 
    } 

    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
     if (context instanceof OnPasienFragmentInteractionListener) { 
      mPasienListener = (OnPasienFragmentInteractionListener) context; 
     } else { 
      throw new RuntimeException(context.toString() 
        + " must implement OnFragmentInteractionListener"); 
     } 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
     mPasienListener = null; 
    } 

    public interface OnPasienFragmentInteractionListener { 
     // TODO: Update argument type and name 
     void onPasienFragmentInteraction(String sentence); 
    } 
} 

这是RecyclerView适配器

public class ListPasienAdapter extends RecyclerView.Adapter<ListPasienAdapter.PasienViewHolder> { 

    private List<Pasien> pasienList; 
    public int count = 0; 
    public ListPasienAdapter(List<Pasien> pasienList) { 
     this.pasienList = pasienList; 
    } 

    @Override 
    public void onBindViewHolder(final PasienViewHolder pasienViewHolder, int i) { 

     final Pasien pi = pasienList.get(i); 
     pasienViewHolder.tvTanggal.setText(pi.Tgl_Masuk); 
     pasienViewHolder.tvNama.setText(pi.Nama_Pasien + "/" + pi.Gender); 
     pasienViewHolder.tvKamar.setText(pi.No_Kamar); 

     if (pi.Perawatan.toString().equals("Merah")) { 
      pasienViewHolder.ivRambu.setImageResource(R.drawable.merah); 
     } else if (pi.Perawatan.toString().equals("Kuning")) { 
      pasienViewHolder.ivRambu.setImageResource(R.drawable.kuning); 
     } else if (pi.Perawatan.toString().equals("Hijau")) { 
      pasienViewHolder.ivRambu.setImageResource(R.drawable.hijau); 
     } 

     pasienViewHolder.ivRambu.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
      } 
     }); 

    } 

    @Override 
    public ListPasienAdapter.PasienViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { 
     View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_view_pasien, viewGroup, false); 
     return new ListPasienAdapter.PasienViewHolder(itemView); 
    } 


    @Override 
    public int getItemCount() { 
     return pasienList.size(); 
    } 

    public static class PasienViewHolder extends RecyclerView.ViewHolder { 
     protected TextView tvTanggal; 
     protected TextView tvNama; 
     protected TextView tvKamar; 
     protected TextView tvGender; 
     protected ImageView ivRambu; 
     protected ImageView ivDown; 
     protected ImageView btnIngat; 
     protected ImageView btnTerkirim; 
     protected TextView tvPeringatan; 
     protected LinearLayout btnDetail; 

     public PasienViewHolder(View v) { 
      super(v); 
      tvTanggal = (TextView) v.findViewById(R.id.tvTanggal); 
      tvNama = (TextView) v.findViewById(R.id.tvNama); 
      tvKamar = (TextView) v.findViewById(R.id.tvKamar); 
      ivRambu = (ImageView) v.findViewById(R.id.ivRambu); 
      ivDown = (ImageView) v.findViewById(R.id.ivDown); 
      //btnIngat = (ImageView) v.findViewById(R.id.btnIngat); 
      //btnTerkirim = (ImageView) v.findViewById(R.id.btnTerkirim); 
      btnDetail = (LinearLayout) v.findViewById(R.id.btnDetail); 
      //tvPeringatan = (TextView) v.findViewById(R.id.tvPeringatan); 
     } 
    } 
} 

这是接收器片段

public class ListAktivitas extends Fragment { 
    private static final String ARG_PARAM1 = "param1"; 
    private static final String ARG_PARAM2 = "param2"; 

    // TODO: Rename and change types of parameters 
    private String mParam1; 
    private String mParam2; 

    private OnFragmentInteractionListener mListener; 

    public ListAktivitas() { 
     // Required empty publtic constructor 
    } 

    public static final String URL_LIST_Aktivitas = "http"; 
    public static final String URL_Pasien = "http://"; 
    String id="3"; 
    RequestQueue queue; 
    List<Aktivitas> resultAktivitas = new ArrayList<>(); 
    List<Pasien> resultPasien = new ArrayList<>(); 
    Aktivitas a = new Aktivitas(); 
    ListAktivitasAdapter laa; 

    RecyclerView cardListAktivitas; 

    Integer Urut; 
    String Nama_Prosedur; 
    String Id_Prosedur; 
    String Id_Aktivitas; 
    String Status; 
    String Nama_Pasien, No_Kamar, Gender; 
    Context context; 

    TextView tvNamaPasien,tvKamar; 


    public static ListAktivitas newInstance(String param1, String param2) { 
     ListAktivitas fragment = new ListAktivitas(); 
     Bundle args = new Bundle(); 
     args.putString(ARG_PARAM1, param1); 
     args.putString(ARG_PARAM2, param2); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     if (getArguments() != null) { 
      mParam1 = getArguments().getString(ARG_PARAM1); 
      mParam2 = getArguments().getString(ARG_PARAM2); 
     } 
     queue = Volley.newRequestQueue(getActivity()); 
     context = getActivity(); 
     } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View view = inflater.inflate(R.layout.fragment_list_aktivitas, container, false); 

     cardListAktivitas = (RecyclerView) view.findViewById(R.id.cardListAktivitas); 
     cardListAktivitas.setHasFixedSize(true); 
     LinearLayoutManager llm = new LinearLayoutManager(getActivity()); 
     llm.setOrientation(LinearLayoutManager.VERTICAL); 
     cardListAktivitas.setLayoutManager(llm); 

     tvNamaPasien = (TextView) view.findViewById(R.id.tvNamaPasien); 
     tvKamar = (TextView) view.findViewById(R.id.tvKamar); 

     String url2 = URL_Pasien+id; 
     JsonObjectRequest req2 = new JsonObjectRequest(url2, null, 
       new Response.Listener<JSONObject>() { 
        @Override 
        public void onResponse(JSONObject response) { 
         JSONArray users = null; 
         try { 
          users = response.getJSONArray("result1"); 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 
          try { 
           JSONObject obj = users.getJSONObject(0); 
           Nama_Pasien = (String) obj.get("Nama_Pasien"); 
           No_Kamar = (String) obj.get("No_Kamar"); 
           if(obj.get("Gender").toString().equals("Laki-laki")){ 
            Gender = "L"; 
           }else if (obj.get("Gender").toString().equals("Perempuan")){ 
            Gender = "P"; 
           } 
           resultPasien.add(new Pasien(Nama_Pasien, No_Kamar,Gender)); 

          } catch (JSONException e) { 
           e.printStackTrace(); 
          } 
         tvNamaPasien.setText(Nama_Pasien+"/"+Gender); 
         tvKamar.setText("kamar:"+No_Kamar); 
        } 
       }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       //Toast.makeText(Home.this,"Terjadi Kendala Koneksi",Toast.LENGTH_LONG).show(); 
      } 
     }); 
     queue.add(req2); 

     String url1 = URL_LIST_Aktivitas+id; 
     Toast.makeText(getActivity(), url1, Toast.LENGTH_SHORT).show(); 
     JsonObjectRequest req = new JsonObjectRequest(url1, null, 
       new Response.Listener<JSONObject>() { 
        @Override 
        public void onResponse(JSONObject response) { 
         JSONArray users = null; 
         try { 
          users = response.getJSONArray("result1"); 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 
         Toast.makeText(getActivity(), "yes", Toast.LENGTH_SHORT).show(); 
         Toast.makeText(getActivity(), String.valueOf(users.length()), Toast.LENGTH_SHORT).show(); 
         for (int i = 0; i < users.length(); i++) { 
          try { 
           JSONObject obj = users.getJSONObject(i); 
           Urut = (Integer) obj.get("Urut"); 
           Nama_Prosedur= (String) obj.get("Nama_Prosedur"); 
           Id_Prosedur = (String) obj.get("Id_Prosedur"); 
           Id_Aktivitas = String.valueOf(obj.getInt(Id_Aktivitas)); 
           Status = (String) obj.get("Status"); 
          } catch (JSONException e) { 
           e.printStackTrace(); 
          } 
          Toast.makeText(context,Nama_Prosedur, Toast.LENGTH_SHORT).show(); 
          resultAktivitas.add(new Aktivitas(Urut, Nama_Prosedur, Id_Prosedur, Id_Aktivitas, Status)); 
          laa = new ListAktivitasAdapter(resultAktivitas); 
          cardListAktivitas.setAdapter(laa); 
         } 

        } 
       }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       Toast.makeText(getActivity(),"Terjadi Kendala Koneksi",Toast.LENGTH_LONG).show(); 
      } 
     }); 
     queue.add(req); 

     return view; 
    } 

    // TODO: Rename method, update argument and hook method into UI event 
    public void onButtonPressed(Uri uri) { 
     if (mListener != null) { 
      mListener.onFragmentInteraction(uri); 
     } 
    } 

    /* 
    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
     if (context instanceof OnFragmentInteractionListener) { 
      mListener = (OnFragmentInteractionListener) context; 
     } else { 
      throw new RuntimeException(context.toString() 
        + " must implement OnFragmentInteractionListener"); 
     } 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
     mListener = null; 
    } 

*/ 
    public interface OnFragmentInteractionListener { 
     // TODO: Update argument type and name 
     void onFragmentInteraction(Uri uri); 
    } 
} 

这是包含2片段

public class PasienActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener,PasienFragment.OnPasienFragmentInteractionListener { 


    PasienFragment pasienFragment; 
    ListPasienFragment listPasienFragment; 
    ListAktivitas listAktivitasFragment; 

    RequestQueue queue; 

    String Nama_Dokter,Nama_RS; 

    public static final String URL_LIST = "http:"; 
    String id="vvvv"; 

    View navHeaderView; 
    TextView tvNavDokter, tvNavRS; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_pasien); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     queue = Volley.newRequestQueue(this); 

     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); 


     listPasienFragment = new ListPasienFragment(); 
     listPasienFragment.setArguments(getIntent().getExtras()); 
     getSupportFragmentManager().beginTransaction().add(R.id.listPasienContainer, listPasienFragment).commit(); 


     pasienFragment = new PasienFragment(); 
     pasienFragment.setArguments(getIntent().getExtras()); 
     getSupportFragmentManager().beginTransaction().add(R.id.pasienFragmentContainer,pasienFragment).commit(); 


     listAktivitasFragment = new ListAktivitas(); 
     listAktivitasFragment.setArguments(getIntent().getExtras()); 
     getSupportFragmentManager().beginTransaction().add(R.id.listAktivitasContainer, listAktivitasFragment).commit(); 


     View navHeaderView = navigationView.getHeaderView(0); 
     tvNavDokter = (TextView) navHeaderView.findViewById(R.id.tvNavDokter); 
     tvNavRS = (TextView) navHeaderView.findViewById(R.id.tvNavRS); 

     String url1 = URL_LIST+id; 
     JsonObjectRequest req = new JsonObjectRequest(url1, null, 
       new Response.Listener<JSONObject>() { 
        @Override 
        public void onResponse(JSONObject response) { 
         JSONArray users = null; 
         try { 
          users = response.getJSONArray("result1"); 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 
          try { 
           JSONObject obj = users.getJSONObject(0); 
           Nama_Dokter = (String) obj.get("Nama_Dokter"); 
           Nama_RS = (String) obj.get("Rumah_Sakit"); 

           tvNavDokter.setText(Nama_Dokter); 
           tvNavRS.setText(Nama_RS); 
          } catch (JSONException e) { 
           e.printStackTrace(); 
          } 
        } 
       }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       //Toast.makeText(Home.this,"Terjadi Kendala Koneksi",Toast.LENGTH_LONG).show(); 
      } 
     }); 
     queue.add(req); 
    } 

    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.pasien, 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(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 
     /* 
     if (id == R.id.nav_camera) { 
      // Handle the camera action 
     } else if (id == R.id.nav_gallery) { 

     } else if (id == R.id.nav_slideshow) { 

     } else if (id == R.id.nav_manage) { 

     } else if (id == R.id.nav_share) { 

     } else if (id == R.id.nav_send) { 

     } 
     */ 


     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 

    @Override 
    public void onPasienFragmentInteraction(String sentence) { 
     if(listPasienFragment!=null) 
      listPasienFragment.updateSentence(sentence); 
    } 

} 
+0

[与其它片段通讯](https://developer.android.com/training/basics/fragments/communicating.html) – matoni

回答

0

可以使用包将数据发送到片段的活性。 EG。 Bundle data = new Bundle(); data.putString(“key”,“value”);

NewFragment newFragment =新NewFragment(); newFragment.setArguments(eventDetailsData);`

然后加载片段。

杀/删除当前片段执行以下码 getActivity()onBackPressed();

相关问题