android - 这个 ListAdapter 编辑文本有什么问题?

标签 android android-edittext

我认为我在这里犯了一些非常基本的错误。我无法编辑我的列表适配器生成的任何 editText 字段(显示文本框,我可以将光标放在该字段上,但我无法键入/删除任何文本).这是我的代码。

我的列表适配器 公共(public)类 LoanRepaymentListAdapter 扩展 ArrayAdapter {

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) {
                String repaymentAmount = arg0.toString();
                loansForRepayment.get(position).setRepaymentAmount(Float.parseFloat(repaymentAmount));
            }

            @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) {
                // TODO Auto-generated method stub

            }


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

我的 XML loan_repayments

<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="text"
    android:textSize="12sp"
     >
</EditText>

</RelativeLayout>

我的 Activity 代码

public void onCreate(Bundle savedInstanceState){
    lastKnownLocation = Utility.getLastKnownLocation(this);
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();

    loansForRepayment = (ArrayList<CurrentRepaymentInstallment>)intent.getSerializableExtra(Constants.CURRENT_REPAYMENT_INSTALLMENT);
    ArrayAdapter<CurrentRepaymentInstallment> adapter = new LoanRepaymentListAdapter(this, loansForRepayment);

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

    setListAdapter(adapter);

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

还想知道如何在 onChange 列表中设置已编辑文本的值。谢谢

最佳答案

你不应该使用 EditText 而不是 TextView 吗?

更新:看起来我在第一次通过时误解了。您可能是说 repaymentAmount 不可编辑。正确的?如果是,则尝试注释掉以下行:

viewHolder.repaymentAmount.addTextChangedListener( ...
viewHolder.repaymentAmount.setEditableFactory( ...

它还会保持不可编辑状态吗?

关于android - 这个 ListAdapter 编辑文本有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11155475/

相关文章:

java - 重新打开android时解析崩溃

Android:单击 EditText 时启动 Activity

android - 默认 Android EditText 高亮颜色不是橙色

android - 在对话框中打开时删除编辑文本的默认矩形边框

java - 如何在android中处理队列? java

android - Gradle DSL 方法未找到 : compile() when switched computers

java - 从 Android 设备通过 Wifi 访问打印机

android - CALL_PHONE 与 CALL_PHONE_PRIVLEGED

android - 在android中的编辑文本边框中添加渐变

android - 无法从对话框中的 EditText 对象获取文本