Fay Zan Fay Zan - 3 months ago 36
Android Question

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

Answer

Make a public class utils and in it create a public static ArrayList<Integer> positions. And in adapter of listview add setOnClickListener on CheckBox that when it is clicked and checked you add that position to public static array.

public class Utils {
    public static ArrayList<Integer> positions =new ArrayList<>();
}

In getView() Function of your adapter after inflating layout add this

CheckBox cBox= (CheckBox) view.findViewById(R.id.myCheckBox); // view is you view inflated
final int myPos= position; //position of cuurent item will be passed into getwiew function

        cBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckBox temp=(CheckBox)v;
                if(temp.isChecked()){
                    Utils.positions.add(myPos);
                }
                else{
                    Utils.positions.remove(myPos);
                }
            }
        });

In this way you can get all list of indexes in Utils.postions which are checked when you press button like edit delete etc. AND on click on these buttons you perform your required task on these indexes of main array passed to adapter

Make sure you clear that public static Arraylist everytime you populate list view YOU can do this by calling

Utils.positions.clear()

before notifying adapter everytime.