rikisaraan

viewmodel with 1 row

Nov 17th, 2020
944
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.73 KB | None | 0 0
  1. //Viewmodelnya
  2.  
  3. public class ProfileAnggotaViewmodel extends ViewModel {
  4.     private MutableLiveData<DataProfileAnggota> profileAnggota;
  5.  
  6.     public MutableLiveData<DataProfileAnggota> getProfileAnggota(String userId) {
  7.         profileAnggota = new MutableLiveData<>();
  8.         loadProfileAnggota(userId);
  9.         return profileAnggota;
  10.     }
  11.  
  12.     public void loadProfileAnggota(String userId){
  13.         final String urlProfileAnggota = Server.URL + "cekSegment.php?user_id="+userId;
  14.         JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.GET, urlProfileAnggota,
  15.                 null, response -> {
  16.             Log.e("responsexx", response.toString());
  17.             try {
  18.                 DataProfileAnggota dataProfileAnggota = new DataProfileAnggota();
  19.                 dataProfileAnggota.setUserId(response.getString("nrp"));
  20.                 dataProfileAnggota.setUserName(response.getString("nama"));
  21.                 dataProfileAnggota.setGolongan(response.getString("golongan"));
  22.                 dataProfileAnggota.setSisaPlafon(response.getString("sisaplafon"));
  23.  
  24.                 profileAnggota.setValue(dataProfileAnggota);
  25.  
  26.             } catch (JSONException e) {
  27.                 e.printStackTrace();
  28.             }
  29.         }, error -> {
  30.             VolleyLog.d("TAG", "Error: " + error.getMessage());
  31.             Log.e("TAG", "Site Info Error: " + error.getMessage());
  32.  
  33.         });
  34.  
  35.         AppController.getInstance().addToRequestQueue(objectRequest);
  36.     }
  37. }
  38.  
  39. //cara manggil di activitynya
  40. public void getProfileAnggota(){
  41.      mProfileAnggotaViewmodel.getProfileAnggota(userId).observe(this, profileAnggota -> {
  42.             Log.e(TAG, "getProfileAnggota: "+profileAnggota.getUserName());
  43.      });
  44. }
Advertisement
Add Comment
Please, Sign In to add comment