问题:
以下是方案(拆分帐单应用),其中自定义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();
}
}
}
活动类:
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();
}
}
我正在尝试实现此功能,该功能位于&#34; SettleUp&#34;应用程序。 日志:
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}