mercredi 18 décembre 2019

ListView Checked Checkboxes

I am creating an Android application for communication learning

i make a List of word, it is contains a CheckBoxes and TextView, TextView is to display the the word, the use of the CheckBox is to determine what item in the ListView that I selected.

i store the id of the word in sqlite with "submit" button. when i close and open the activity again i want to see the checked checkboxes but this code only save to my database but not show the selected item when i open the activity again

this is the Activity

public class PerkembanganLevel1 extends AppCompatActivity{

    private ContentDao contentDao;
    private ChildDao childDao;
    private Child anakYangDipilih;

    public Global global;

    private AssessmentDao assessmentDao;
    private Assessment assessment;

    String idYangSudahBisa;

    PerkembanganAdapter perkembanganAdapter = null;

    ContentHistory contentHistory;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_perkembangan_level1);

        contentDao = new ContentDao(this);
        contentDao.open();

        childDao = new ChildDao(this);
        childDao.open();

        anakYangDipilih = ((Global) getApplicationContext()).getChild();

        idYangSudahBisa = anakYangDipilih.getLevel1();

        ArrayList<ContentHistory> listKataLevel1 = (ArrayList<ContentHistory>) contentDao.getAllWordByLevel(1);

        perkembanganAdapter = new PerkembanganAdapter(this, R.layout.list_perkembangan, listKataLevel1);

        ListView level1 =  findViewById(R.id.LVPlv1);
        level1.setAdapter(perkembanganAdapter);
        Log.i("listKataLevel1 ", String.valueOf(listKataLevel1));
        level1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                contentHistory = (ContentHistory) parent.getItemAtPosition(position);

            }
        });
        findViewById(R.id.BtnPLv1Submit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                idYangSudahBisa = perkembanganAdapter.idYangSudahBisa();

                anakYangDipilih.getId();
                anakYangDipilih.setLevel1(idYangSudahBisa);
                anakYangDipilih.setCreatedBy(((Global) getApplicationContext()).getTherapist().getId());
                anakYangDipilih.setUpdatedBy(((Global) getApplicationContext()).getTherapist().getId());
                anakYangDipilih.setCreatedDate(new Date());
                anakYangDipilih.setUpdatedDate(new Date());
                anakYangDipilih = childDao.saveChild(anakYangDipilih);

                Toast.makeText(PerkembanganLevel1.this, "Perkembangan Anak pada level 1 Berhasil Diperbarui", Toast.LENGTH_SHORT).show();
            }
        });


    }

}

this is the adapter


public class PerkembanganAdapter extends ArrayAdapter<ContentHistory> {


    String idYangSudahBisa;

    private ChildDao childDao;
    private Child anakYangDipilih;

    public ArrayList<ContentHistory> contentHistories;

    public PerkembanganAdapter(Context context, int id, ArrayList<ContentHistory> contentHistories){
        super(context, id, contentHistories);
        this.contentHistories = new ArrayList<>();
        this.contentHistories.addAll(contentHistories);



        childDao = new ChildDao(getContext());
        childDao.open();

        anakYangDipilih = ((Global) context.getApplicationContext()).getChild();
        this.idYangSudahBisa = anakYangDipilih.getLevel1();

    }

    private class ViewHolder{
        TextView title;
        CheckBox checkBoxes;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        ViewHolder viewHolder = null;
        if (convertView == null){
            LayoutInflater layoutInflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.list_perkembangan, null);

            viewHolder = new ViewHolder();
            viewHolder.title = convertView.findViewById(R.id.TVListPerkembangan);
            viewHolder.checkBoxes = convertView.findViewById(R.id.CBListPerkembangan);

            convertView.setTag(viewHolder);

            viewHolder.checkBoxes.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    CheckBox checkBox = (CheckBox) v;
                    ContentHistory contentHistory = (ContentHistory) checkBox.getTag();
                    contentHistory.setSelected(checkBox.isChecked());
                    if (checkBox.isChecked()){
                        idYangSudahBisa = idYangSudahBisa + ","+contentHistory.getId();
                        //Toast.makeText(getContext(), ""+idYangSudahBisa,Toast.LENGTH_SHORT).show();
                    } else {
                        idYangSudahBisa = idYangSudahBisa.replace(","+contentHistory.getId(), "");
                        //Toast.makeText(getContext(), ""+idYangSudahBisa,Toast.LENGTH_SHORT).show();
                    }

                }
            });



        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }


        ContentHistory contentHistory = contentHistories.get(position);
        viewHolder.title.setText(contentHistory.getTitle());

        viewHolder.checkBoxes.setChecked(contentHistory.getSelected());
        viewHolder.checkBoxes.setTag(contentHistory);


        return convertView;

    }

    public String idYangSudahBisa(){

        return this.idYangSudahBisa;
    }

}
...



Aucun commentaire:

Enregistrer un commentaire