mardi 21 juillet 2020

how to save multiple checkbox value (Retrofit, android studio)

I have 2 table in database: peminjaman_inventaris and inventaris, in peminjaman_inventaris table there is foreign key with inventaris table. i want to save the multiple value from recyclerview that filled with the invetaris table value on my peminjaman_inventaris if i selected the checkbox

table peminjaman_inventaris enter image description here

table inventaris enter image description here

enter image description here

Here is my fragment code

public class InventarisFragment extends Fragment {
private RecyclerView mRecycler;
private RecyclerView.Adapter mAdapter;
private android.content.Context Context;
ProgressDialog pd;
private String idUser;
private List<UserModel> mItemsUser = new ArrayList<>();
private List<String> namaKetua;
private List<String> userID;
private List<InventarisModel> mItems = new ArrayList<>();
private Spinner mNama;
private EditText msearchNama;
private Button mAwalPeminjaman, mAkhirPeminjaman, mCari, mSimpan, mCancel;
private PeminjamanInventarisModel mPeminjamanInventarisModel;
private static List<String> dataAlat;

public static InventarisFragment newInstance() {
    Bundle args = new Bundle();
    InventarisFragment fragment = new InventarisFragment();
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    pd = new ProgressDialog(getActivity());
    namaKetua = new ArrayList<>();
    userID = new ArrayList<>();
    dataAlat = new ArrayList<>();

}

@Override
public void onStop() {
    super.onStop();
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_peminjaman_inventaris, container, false);

    mNama = (Spinner) v.findViewById(R.id.spnNama);
    mAkhirPeminjaman = (Button) v.findViewById(R.id.btnAkhirPeminjaman);
    mAwalPeminjaman = (Button) v.findViewById(R.id.btnAwalPeminjaman);
    mSimpan = (Button) v.findViewById(R.id.btnSimpan);
    mCancel = (Button) v.findViewById(R.id.btnCancel);
    mRecycler = (RecyclerView) v.findViewById(R.id.recyclerIventaris);
    mRecycler.setLayoutManager(new LinearLayoutManager(getActivity()));


    ApiRequestUser api_user = RetroServerUser.getClient().create(ApiRequestUser.class);
    Call<ResponsUserModel> getDataUser = api_user.getUser();
    getDataUser.enqueue(new Callback<ResponsUserModel>() {
        @Override
        public void onResponse(Call<ResponsUserModel> call, Response<ResponsUserModel> response) {
            Log.d("RETRO", "RESPONSE : " + response.body().getKode());

            mItemsUser = response.body().getResult();
            for (int i = 0; i < mItemsUser.size(); i++) {
                namaKetua.add(mItemsUser.get(i).getNama());
                userID.add(mItemsUser.get(i).getNim());
            }

            final ArrayAdapter<String> adapterUser = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, namaKetua);
            mNama.setAdapter(adapterUser);
            mNama.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    idUser = userID.get(position);
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                }
            });
        }
        public void onFailure(Call<ResponsUserModel> call, Throwable t) {
            pd.hide();
            Log.d("RETRO", "FAILED : respon gagal");
        }
    });

    mSimpan.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            pd.setMessage("Loading ...");
            pd.setCancelable(false);
            pd.show();

            ApiRequestPeminjamanInventaris api_inventaris = RetroServerPemInventaris.getClient().create(ApiRequestPeminjamanInventaris.class);
            String nama_peminjam = idUser;
            String alat = "1";
            String dtStart = mAwalPeminjaman.getText().toString();
            String dtEnd = mAkhirPeminjaman.getText().toString();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

            try {
                Date Tanggal = formatter.parse(dtStart);
                Date Tanggal2 = formatter.parse(dtEnd);

                Call<ResponsePeminjamanInventaris> sendData = api_inventaris.sendData(alat,formatter.format(Tanggal), formatter.format(Tanggal2),nama_peminjam );
                sendData.enqueue(new Callback<ResponsePeminjamanInventaris>() {
                    @Override
                    public void onResponse(Call<ResponsePeminjamanInventaris> call, Response<ResponsePeminjamanInventaris> response) {
                        Log.d("RETRO", "response : " + response.body().toString());
                        String kode = response.body().getKode();
                        Toast.makeText(getActivity(), "Berhasil disimpan",
                                Toast.LENGTH_LONG).show();
                    }
                    @Override
                    public void onFailure(Call<ResponsePeminjamanInventaris> call, Throwable t) {
                        Log.d("RETRO", "Falure : " + "Gagal Mengirim Request");
                    }

                });
            } catch (ParseException e) {
                e.printStackTrace();
            }
            pd.hide();

        }
    });

    mCancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AppCompatActivity activity = (AppCompatActivity) v.getContext();
            Fragment myFragment = new PemInventarisListFragment();
            activity.getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, myFragment).addToBackStack(null).commit();
        }
    });

    mCari = (Button) v.findViewById(R.id.cari);
    mCari.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            TextView mAlat = (TextView) v.findViewById(R.id.txtSearchInvest);
            String alat = mAlat.getText().toString();

            ApiRequestInventaris api = RetroServerInventaris.getClient().create(ApiRequestInventaris.class);
            Call<ResponseInventarisModel> getData = api.getAlatByName(alat);
            getData.enqueue(new Callback<ResponseInventarisModel>() {
                @Override
                public void onResponse(Call<ResponseInventarisModel> call, Response<ResponseInventarisModel> response) {
                    Log.d("RETRO", "response : " + response.body().toString());
                    mItems = response.body().getResult();

                    mAdapter = new AdapterInventaris(getContext(), mItems);
                    mRecycler.setAdapter(mAdapter);
                    mAdapter.notifyDataSetChanged();
                }

                @Override
                public void onFailure(Call<ResponseInventarisModel> call, Throwable t) {
                    Log.d("RETRO", "FAILED : respon gagal");
                }
            });
        }
    });

    ApiRequestInventaris api = RetroServerInventaris.getClient().create(ApiRequestInventaris.class);
    Call<ResponseInventarisModel> getdata = api.getInventaris();
    getdata.enqueue(new Callback<ResponseInventarisModel>() {
        @Override
        public void onResponse(Call<ResponseInventarisModel> call, Response<ResponseInventarisModel> response) {
            Log.d("RETRO", "RESPONSE : " + response.body().getKode());
            mItems = response.body().getResult();

            mAdapter = new AdapterInventaris(getContext(), mItems);
            mRecycler.setAdapter(mAdapter);
            mAdapter.notifyDataSetChanged();
        }

        @Override
        public void onFailure(Call<ResponseInventarisModel> call, Throwable t) {
            Log.d("RETRO", "FAILED : respon gagal");
        }
    });

    mAkhirPeminjaman.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showDatePicker2();
        }
    });

    mAwalPeminjaman.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showDatePicker();
        }
    });
    return v;
}

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

private void showDatePicker() {
    DatePickerFragment date = new DatePickerFragment();
    /**
     * Set Up Current Date Into dialog
     */
    Calendar calender = Calendar.getInstance();
    Bundle args = new Bundle();
    args.putInt("year", calender.get(Calendar.YEAR));
    args.putInt("month", calender.get(Calendar.MONTH));
    args.putInt("day", calender.get(Calendar.DAY_OF_MONTH));
    date.setArguments(args);
    /**
     * Set Call back to capture selected date
     */

    date.setCallBack(ondate2);
    date.show(getFragmentManager(), "Date Picker");
}
private void showDatePicker2() {
    DatePickerFragment date = new DatePickerFragment();
    /**
     * Set Up Current Date Into dialog
     */
    Calendar calender = Calendar.getInstance();
    Bundle args = new Bundle();
    args.putInt("year", calender.get(Calendar.YEAR));
    args.putInt("month", calender.get(Calendar.MONTH));
    args.putInt("day", calender.get(Calendar.DAY_OF_MONTH));
    date.setArguments(args);
    /**
     * Set Call back to capture selected date
     */

    date.setCallBack(ondate);
    date.show(getFragmentManager(), "Date Picker");
}

DatePickerDialog.OnDateSetListener ondate = new DatePickerDialog.OnDateSetListener() {

    public void onDateSet(DatePicker view, int year, int monthOfYear,
                          int dayOfMonth) {

        mAkhirPeminjaman.setText(String.valueOf(year) + "-" + String.valueOf(monthOfYear+1)
                + "-" + String.valueOf(dayOfMonth));
    }
};
DatePickerDialog.OnDateSetListener ondate2 = new DatePickerDialog.OnDateSetListener() {

    public void onDateSet(DatePicker view, int year, int monthOfYear,
                          int dayOfMonth) {

        mAwalPeminjaman.setText(String.valueOf(year) + "-" + String.valueOf(monthOfYear+1)
                + "-" + String.valueOf(dayOfMonth));
    }
};

}

Adapter inventaris code

public class AdapterInventaris extends RecyclerView.Adapter<AdapterInventaris.HolderData> {
private List<InventarisModel> mList ;
private Context ctx;
private CheckBox checkbox;

public AdapterInventaris (Context ctx, List<InventarisModel> mList)
{
    this.ctx = ctx;
    this.mList = mList;
}

@Override
public AdapterInventaris.HolderData onCreateViewHolder(ViewGroup parent, int viewType) {
    View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_list_inventaris, parent, false);
    AdapterInventaris.HolderData holder = new AdapterInventaris.HolderData(layout);
    return holder;
}

@Override
public void onBindViewHolder(AdapterInventaris.HolderData holder, int position) {
    InventarisModel um = mList.get(position);
    holder.status.setText(String.valueOf(um.getStatus()));
    holder.alat.setText(um.getAlat());

    holder.um = um;
    holder.checkbox.setChecked(um.getStatus() == 1);
}

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


class HolderData extends RecyclerView.ViewHolder {
    TextView  alat,status;
    InventarisModel um;
    public CheckBox checkbox;

    public HolderData(View v) {
        super(v);
        status  = (TextView) v.findViewById(R.id.txtNama);
        alat = (TextView) v.findViewById(R.id.txtAlat);
        checkbox = v.findViewById(R.id.checkbox);

    }
}

}




Aucun commentaire:

Enregistrer un commentaire