gpt4 book ai didi

android - 当编辑单个框时,如何防止我的 EditText 列表编辑多个框?

转载 作者:搜寻专家 更新时间:2023-11-01 09:06:54 24 4
gpt4 key购买 nike

我正在尝试使用 EditText 控件实现 listView。问题是当我编辑其中一个字段时, View 中的其他一些字段也会被修改。但是,我的 Java 对象只反射(reflect)了我所做的更改(而不是设置的其他 EditText)

我的安卓 Activity

public class LoanRepaymentActivity extends ListActivity implements View.OnClickListener {

private Location lastKnownLocation;
private ArrayList<CurrentRepaymentInstallment> loansForRepayment;
@Override
public void onCreate(Bundle savedInstanceState){
lastKnownLocation = Utility.getLastKnownLocation(this);
super.onCreate(savedInstanceState);

Intent intent = getIntent();

RepaymentInfo repaymentInfo = (RepaymentInfo)intent.getSerializableExtra(Constants.CURRENT_REPAYMENT_INSTALLMENT);
loansForRepayment = repaymentInfo.getLoansForRepayment();
boolean repaymentsNotPresent = loansForRepayment == null || loansForRepayment.isEmpty();
if(repaymentsNotPresent){
Dialog dialog = Utility.getDialogWithText(LoanRepaymentActivity.this, getText(R.string.noLoansForRepayment).toString());
Utility.getDialogButton(dialog, LoanRepaymentActivity.this, PartnerGroupListActivity.class, intent.getStringExtra(Constants.ACECSS_TOKEN_PARAM));
dialog.show();
}

ArrayAdapter<CurrentRepaymentInstallment> adapter = new LoanRepaymentListAdapter(this, loansForRepayment);

View footer = getLayoutInflater().inflate(R.layout.loan_disbursement_footer, null);
getListView().addFooterView(footer);

setListAdapter(adapter);
/* if(!repaymentsNotPresent) {
TextView textView = (TextView)findViewById(R.id.screenTitle);
String currentInstallmentLabel = getText(R.string.currentInstallmentLabel).toString() + repaymentInfo.getCurrentGroupInstallment();
textView.setText(currentInstallmentLabel);
}*/

Button button = (Button)findViewById(R.id.disburse);
button.setOnClickListener(this);
}

我的 XML 布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>

<TextView android:id="@+id/screenTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TextView>

<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+layout/loan_disbursement_footer"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/borrowerName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="14sp"
>
</TextView>

<TextView
android:id="@+id/loanAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="14sp"
>
</TextView>

<TextView
android:id="@+id/installmentNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="12sp"
>
</TextView>

<TextView
android:id="@+id/estimatedTotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="12sp"
>
</TextView>

</LinearLayout>
<EditText
android:id="@+id/repaymentAmount"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:inputType="numberDecimal"
android:textSize="12sp"
>
</EditText>
</RelativeLayout>

自定义数组适配器用于设置ListView

public class LoanRepaymentListAdapter extends ArrayAdapter<CurrentRepaymentInstallment> {

private final List<CurrentRepaymentInstallment> loansForRepayment;
private final Activity context;

public LoanRepaymentListAdapter(Activity context, List<CurrentRepaymentInstallment> loansForRepayment) {
super(context, R.layout.loan_repayments, loansForRepayment);
this.context = context;
this.loansForRepayment = loansForRepayment;
}

static class ViewHolder {
protected TextView borrowerName;
protected TextView loanAmount;
protected TextView installmentNumber;
protected TextView estimatedTotal;
protected EditText repaymentAmount;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.loan_repayments, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.borrowerName = (TextView) view.findViewById(R.id.borrowerName);
viewHolder.loanAmount = (TextView) view.findViewById(R.id.loanAmount);
viewHolder.installmentNumber = (TextView) view.findViewById(R.id.installmentNumber);
viewHolder.estimatedTotal = (TextView) view.findViewById(R.id.estimatedTotal);
viewHolder.repaymentAmount = (EditText) view.findViewById(R.id.repaymentAmount);
// viewHolder.repaymentAmount.setEditableFactory(Editable.Factory.getInstance());
viewHolder.repaymentAmount.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable arg0) {

}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
// TODO Auto-generated method stub

}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
CurrentRepaymentInstallment loanRepayment = (CurrentRepaymentInstallment) viewHolder.repaymentAmount.getTag();
String repaymentAmount = s.toString();
loanRepayment.setRepaymentAmount(repaymentAmount);
}
});
view.setTag(viewHolder);
viewHolder.repaymentAmount.setTag(loansForRepayment.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).repaymentAmount.setTag(loansForRepayment.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.borrowerName.setText(loansForRepayment.get(position).getLoanProfileBasicInfo().getBorrowerBasicInfo().getFirstName());
holder.loanAmount.setText("Rs. " + Float.toString(loansForRepayment.get(position).getLoanProfileBasicInfo().getLoanAmountInPaisa()/100));
holder.estimatedTotal.setText("Rs. " + Float.toString(loansForRepayment.get(position).getEstimatedTotalAmount()/100));
holder.installmentNumber.setText("Inst no : " + Integer.toString(loansForRepayment.get(position).getInstallmentNumber()));
float repaymentAmt = loansForRepayment.get(position).getRepaymentAmount();
if(repaymentAmt != 0.0) holder.repaymentAmount.setText(Float.toString(repaymentAmt));
return view;
}
}

最佳答案

原因是 Android ListView 回收..请查看以下链接中接受的答案 EditText in ListView without it recycling input

希望这对您有所帮助。

关于android - 当编辑单个框时,如何防止我的 EditText 列表编辑多个框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11209154/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com