jeudi 18 août 2016

onClick to edit CheckBox data on ListView

I saved some text by using FileOutputStream to a file and then displayed them on a ListView.

I'm trying to edit the content by first check the checkbox of the item, then click a button to either Edit or Delete the data.

I did a lot of research but still couldn't understand how to implement this

Here are my codes

To show the data and display them on ListView

void ShowSavedFiles() {
    //SavedFiles = getApplicationContext().fileList();
    SavedFiles = getApplication().fileList();
    ArrayAdapter<String> adapter
            = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1,
            SavedFiles);

    listView.setAdapter(adapter);
    if (adapter.isEmpty()) {
        Toast.makeText(getBaseContext(), "There is no Address !", Toast.LENGTH_LONG).show();
    }
}

To edit the data by checking CheckBox, then press Edit button

void editData(){
    imgEditData.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (choiceMode == false) {

                choiceMode = true;
                clickAble = false;
                multiSelectBar.setVisibility(View.VISIBLE);
                Toast.makeText(addressActivity.this, "Edit ON", Toast.LENGTH_SHORT).show();
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        // change the checkbox state
                        CheckedTextView checkedTextView = ((CheckedTextView) view);
                        checkedTextView.setChecked(!checkedTextView.isChecked());
                    }
                });
                ArrayAdapter<String> adapter
                        = new ArrayAdapter<String>(addressActivity.this,
                        android.R.layout.simple_list_item_multiple_choice,
                        SavedFiles);
                listView.setAdapter(adapter);
            }}
    });
}

a picture of it to better understand

enter image description here

*Ignore the other buttons

Aucun commentaire:

Enregistrer un commentaire