how to retrieve value from all EditText in ListVie

2019-04-15 05:45发布

问题:

i have a structure like this in my ListView

TextView EditText

TextView EditText

TextView EditText

   Btton

when i click on ok button. how to retrieve value from each EditText and print Sum of it on dialog box...any idea....?

回答1:

Assuming your row layout is something like

LinearLayout - TextView - Edittext

You can use something like

for(int i =0;i<getListView.getChildCount();i++){
    LinearLayout layout = getListView.getChildAt(i);

    String text = layout.getChildAt(1).getText();
}


回答2:

I would do it this way

Inside my ListAdapter, while creating rows, save a reference to list view in an ArrayList

On click of the button, get the array list from the adapter and iterate through all edittexts



回答3:

Check out TextWatcher. Add it using:

myEditText1.addTextChangedListener(new GenericTextWatcher(myEditText1));
myEditText2.addTextChangedListener(new GenericTextWatcher(myEditText2));
myEditText3.addTextChangedListener(new GenericTextWatcher(myEditText3));

...

private class GenericTextWatcher implements TextWatcher{

    private View view;
    private GenericTextWatcher(View view) {
        this.view = view;
    }

    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

    public void afterTextChanged(Editable editable) {
        String text = editable.toString();
        switch(view.getId()){
            case R.id.editText1_id:
                // do something
                break;
            case R.id.editText2_id:
                // do something
                break;
            case R.id.editText3_id:
                // do something
                break;
        }
    }
}