2017-09-06 2 views
-1

Ich muss einen Wert senden, wenn ich auf eine Zeile in recyclerview auf ein anderes Fragment klicke (Ich verdünne OnBind im Adapter). Dann muss ich das Fragment, das die Recycleransicht enthält, schließen und das Empfängerfragment öffnen. Ich habe irgendwo gelesen, dass wir Bundle verwenden können, aber ich bin ziemlich neu in der Android-Programmierung, also bin ich verwirrt.So senden Sie einen Wert von einem Fragment an ein anderes Fragment von Recyclerview Adapter. Dann schließe das erste Fragment und öffne das neue Fragment.

Dies ist das Fragment, das 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); 
    } 
} 

Dies ist der Adapter 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); 
     } 
    } 
} 

Dies ist der Empfänger Fragment

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

Dies ist die Aktivität, die das 2-Fragment

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

[Kommunikation mit anderen Fragmenten] (https://developer.android.com/training/basics/fragments/communicating.html) – matoni

Antwort

0

Sie enthalten Datenbündel-Fragment unter Verwendung senden. EG. `Bündel Daten = neues Bündel(); data.putString ("Schlüssel", "Wert");

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

Dann laden Fragment.

Um das aktuelle Fragment zu löschen/entfernen, führen Sie den folgenden Code aus: getActivity(). OnBackPressed();

Verwandte Themen