how to retrieve value from all EditText in ListView

时光总嘲笑我的痴心妄想 提交于 2019-12-06 14:47:30

问题


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;
        }
    }
}


来源:https://stackoverflow.com/questions/7495202/how-to-retrieve-value-from-all-edittext-in-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!