Java 带有edittext的Listview包含textwatcher notify数据更改

Java 带有edittext的Listview包含textwatcher notify数据更改,java,android,listview,android-listview,textwatcher,Java,Android,Listview,Android Listview,Textwatcher,问题: 下面是一个场景(拆分账单应用程序),其中自定义ListView包含EditText,每行都附加了textwatcher。当我在任何列表项的EditText中更改值时,其他列表项的EditText值不会更改 例如,ListView包含五行,并在列表适配器中传递总金额。最初,每行中的EditText值设置为0 totalAmount = 100; 假设我将第一行的EditText值从0更改为20,则其他列表项的EditText值必须设置为20 这里的问题不在于拆分,而在于textwatch

问题:

下面是一个场景(拆分账单应用程序),其中自定义
ListView
包含
EditText
,每行都附加了textwatcher。当我在任何列表项的
EditText
中更改值时,其他列表项的
EditText
值不会更改

例如,
ListView
包含五行,并在列表适配器中传递总金额。最初,每行中的
EditText
值设置为0

totalAmount = 100;
假设我将第一行的
EditText
值从0更改为20,则其他列表项的
EditText
值必须设置为20

这里的问题不在于拆分,而在于textwatcher和更新模型

下面是自定义列表适配器类

public class CustomBillListAdapter extends BaseAdapter {
    int totalAmt;
    Context context;
    ArrayList<Integer> amtSplit;

    public CustomBillListAdapter(Context context, ArrayList<Integer> amtSplit, int totalAmt) {
        this.context = context;
        this.amtSplit = amtSplit;
        this.totalAmt = totalAmt;
    }

    @Override
    public int getCount() {
        return amtSplit.size();
    }

    @Override
    public Object getItem(int position) {
        return amtSplit.get(position);
    }

    @Override
    public long getItemId(int position) {
        return amtSplit.indexOf(getItem(position));
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null)  {
            LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.list_row_custom, null);
        }

        EditText txtAmount = (EditText) convertView.findViewById(R.id.amounts);
        txtAmount.setText(amtSplit.get(position).toString());
        txtAmount.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                getEqualAmount(s.toString(), position);
            }
        });

        return convertView;
    }

    private void getEqualAmount(String sAmt, int pos) {
        if(sAmt != "") {
            int amt = Integer.parseInt(sAmt);
            int len = 5; //hardcoded value, assume that there are 5 list items
            int remainingAmt = totalAmt - amt;
            int shareAmt = remainingAmt / len;
            int c = shareAmt * len;

            amtSplit.clear();

            for (int i = 0; i < len; i++) {
                if (pos == i) {
                    amtSplit.add(amt);
                } else {
                    amtSplit.add(shareAmt);
                }
            }
            notifyDataSetChanged();
        }
    }
}

我在Android Studio中创建了一个项目,将这些代码复制并粘贴到适配器中,这一切对我来说都很好。您能否发布活动和布局文件以及列表项行的布局。由于调用notifyDataSetChanged,我无法编辑文本,它会使键盘崩溃。同时notifyDataSetChanged也会使应用程序崩溃。所以每次你输入编辑文本时,会发生什么?它不会让你打字吗?请发布包含
列表视图的布局文件。确保您的ListView具有android:DegenantFocusability=“AfterDegenders”
Ya我的列表包含android:DegenantFocusability=“AfterDegenders”。当我点击edittext编辑value app stucks时,android monitor会重复记录上述消息。
public class TestTwo extends AppCompatActivity implements View.OnClickListener
{
    ListView view;
    CustomBillListAdapter adapter;
    ArrayList<Integer> amtList = new ArrayList<Integer>();

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

        LinearLayout addContacts = (LinearLayout)findViewById(R.id.add_contact);
        addContacts.setOnClickListener(this);

        view = (ListView)findViewById(R.id.list);
        adapter = new CustomBillListAdapter(TestTwo.this, amtList, 100);
        view.setAdapter(adapter);
    }

    private void pickContact()
    {
        amtList.add(0);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v)
    {
        pickContact();
    }
}
01-19 00:38:13.308 19367-19367/com.splitbill.kurukshetran.splitbillprototype V/TextView: stopSelectionActionMode()
01-19 00:38:13.309 19367-19367/com.splitbill.kurukshetran.splitbillprototype V/InputMethodManager: START INPUT: android.support.v7.widget.AppCompatEditText{56dbaa8 VFED..CL .F....I. 8,8-532,70 #7f0d007c app:id/amounts} ic=com.android.internal.widget.EditableInputConnection@c778c6b tba=android.view.inputmethod.EditorInfo@17193c8 controlFlags=#0
01-19 00:38:13.312 19367-19367/com.splitbill.kurukshetran.splitbillprototype V/TextView: stopSelectionActionMode()
01-19 00:38:13.316 19367-19367/com.splitbill.kurukshetran.splitbillprototype V/TextView: stopSelectionActionMode()
01-19 00:38:13.317 19367-19367/com.splitbill.kurukshetran.splitbillprototype D/ListView: mSelectorRect.setEmpty in layoutChildren this=android.widget.ListView{94612c7 VFED.VC. ......ID 0,134-540,291 #7f0d0071 app:id/list}
01-19 00:38:13.320 19367-19367/com.splitbill.kurukshetran.splitbillprototype W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
01-19 00:38:13.322 19367-19367/com.splitbill.kurukshetran.splitbillprototype W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
01-19 00:38:13.323 19367-19367/com.splitbill.kurukshetran.splitbillprototype W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
01-19 00:38:13.353 19367-19367/com.splitbill.kurukshetran.splitbillprototype D/ListView: measureHeightOfChildren adapter = com.splitbill.kurukshetran.splitbillprototype.adapter.TestListAdapter@38d3801d, startPosition = 0, endPosition = 1, maxHeight = 370, this = android.widget.ListView{94612c7 VFED.VC. ......I. 0,134-540,291 #7f0d0071 app:id/list}
01-19 00:38:13.356 19367-19367/com.splitbill.kurukshetran.splitbillprototype V/TextView: stopSelectionActionMode()
01-19 00:38:13.356 19367-19367/com.splitbill.kurukshetran.splitbillprototype V/TextView: stopSelectionActionMode()
01-19 00:38:13.356 19367-19367/com.splitbill.kurukshetran.splitbillprototype V/TextView: stopSelectionActionMode()
01-19 00:38:13.356 19367-19367/com.splitbill.kurukshetran.splitbillprototype V/TextView: stopSelectionActionMode()
01-19 00:38:13.356 19367-19367/com.splitbill.kurukshetran.splitbillprototype V/TextView: stopSelectionActionMode()
01-19 00:38:13.357 19367-19367/com.splitbill.kurukshetran.splitbillprototype V/TextView: stopSelectionActionMode()
01-19 00:38:13.357 19367-19367/com.splitbill.kurukshetran.splitbillprototype V/TextView: stopSelectionActionMode()
01-19 00:38:13.358 19367-19367/com.splitbill.kurukshetran.splitbillprototype V/TextView: stopSelectionActionMode()
01-19 00:38:13.358 19367-19367/com.splitbill.kurukshetran.splitbillprototype V/TextView: stopSelectionActionMode()
01-19 00:38:13.359 19367-19367/com.splitbill.kurukshetran.splitbillprototype V/TextView: stopSelectionActionMode()
01-19 00:38:13.359 19367-19367/com.splitbill.kurukshetran.splitbillprototype V/TextView: stopSelectionActionMode()
01-19 00:38:13.359 19367-19367/com.splitbill.kurukshetran.splitbillprototype V/TextView: stopSelectionActionMode()
01-19 00:38:13.359 19367-19367/com.splitbill.kurukshetran.splitbillprototype V/TextView: stopSelectionActionMode()
01-19 00:38:13.360 19367-19367/com.splitbill.kurukshetran.splitbillprototype V/TextView: stopSelectionActionMode()
01-19 00:38:13.360 19367-19367/com.splitbill.kurukshetran.splitbillprototype V/TextView: stopSelectionActionMode()
01-19 00:38:13.361 19367-19367/com.splitbill.kurukshetran.splitbillprototype V/TextView: stopSelectionActionMode()
01-19 00:38:13.362 19367-19367/com.splitbill.kurukshetran.splitbillprototype V/InputMethodManager: START INPUT: android.support.v7.widget.AppCompatEditText{56dbaa8 VFED..CL .F....I. 8,8-532,70 #7f0d007c app:id/amounts} ic=com.android.internal.widget.EditableInputConnection@20130d9d tba=android.view.inputmethod.EditorInfo@1ab0d212 controlFlags=#0
01-19 00:38:13.372 19367-19367/com.splitbill.kurukshetran.splitbillprototype V/TextView: stopSelectionActionMode()
01-19 00:38:13.378 19367-19367/com.splitbill.kurukshetran.splitbillprototype V/TextView: stopSelectionActionMode()
01-19 00:38:13.378 19367-19367/com.splitbill.kurukshetran.splitbillprototype D/ListView: mSelectorRect.setEmpty in layoutChildren this=android.widget.ListView{94612c7 VFED.VC. ......ID 0,134-540,291 #7f0d0071 app:id/list}
01-19 00:38:13.383 19367-19367/com.splitbill.kurukshetran.splitbillprototype W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
01-19 00:38:13.390 19367-19367/com.splitbill.kurukshetran.splitbillprototype D/ActivityThread: ACT-AM_ON_PAUSE_CALLED ActivityRecord{28d782da token=android.os.BinderProxy@1c3d530b {com.splitbill.kurukshetran.splitbillprototype/com.splitbill.kurukshetran.splitbillprototype.TestTwo}}
01-19 00:38:13.413 19367-19367/com.splitbill.kurukshetran.splitbillprototype D/ActivityThread: ACT-PAUSE_ACTIVITY handled : 1 / android.os.BinderProxy@1c3d530b
01-19 00:38:13.413 19367-19367/com.splitbill.kurukshetran.splitbillprototype W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
01-19 00:38:13.417 19367-19367/com.splitbill.kurukshetran.splitbillprototype W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
01-19 00:38:13.429 19367-19367/com.splitbill.kurukshetran.splitbillprototype V/TextView: stopSelectionActionMode()
01-19 00:38:13.429 19367-19367/com.splitbill.kurukshetran.splitbillprototype D/AbsListView: onWindowFocusChanged: hasWindowFocus=false, this=android.widget.ListView{94612c7 VFED.VC. ........ 0,134-540,291 #7f0d0071 app:id/list}