Android:在ListView中启用最后一个Spinner并禁用其余的Spinner。

时间:2014-01-11 06:16:47

标签: android android-listview android-spinner

我正在使用自定义ListView,其中有一个Spinner。

代码: -

    <?xml version="1.0" encoding="utf-8"?>
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/lightGray2"
    android:shrinkColumns="*"
    android:stretchColumns="*" >

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Spinner
            android:id="@+id/spinnerPaymentType"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_span="17" />

        <EditText
            android:id="@+id/eTextPaymentAmount"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_span="7"
            android:inputType="number" />

        <Button
            android:id="@+id/btnPaymentDetail"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_span="6" />
    </TableRow>
</TableLayout>

在ListView外面有一个按钮(假设按钮A),单击此按钮后,在ListView中添加了一行。

现在我想在点击buttonA时禁用所有其他行微调器。

假设已经存在三行,在ListView中添加第四行之前我想禁用前三行内的spinner。

适配器getView代码: -

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    System.out.println("*** getView() ***");
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(rowResourceId, parent, false);

    Spinner spinnerPaymentType = (Spinner) rowView
            .findViewById(R.id.spinnerPaymentType);
    EditText eTextPaymentAmount = (EditText) rowView
            .findViewById(R.id.eTextPaymentAmount);
    eTextPaymentAmount.setText(String.valueOf(position));
    btnPaymentDetail = (Button) rowView.findViewById(R.id.btnPaymentDetail);
    btnPaymentDetail.setTag(position);
    btnPaymentDetail.setOnLongClickListener(this);

    btnPaymentDetail.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            System.out.println("*** btnPaymentDetail is clicked");
            getPaymentDetail();
        }
    });

    ArrayList<PaymentType> aListpaymentTypes = new ArrayList<PaymentType>();
    for (int i = 0; i < paymentCollection.get(0).getaListPaymentOptions()
            .size(); i++) {
        aListpaymentTypes.add(new PaymentType(paymentCollection.get(0)
                .getaListPaymentOptions().get(i).getIntPaymentId(),
                paymentCollection.get(0).getaListPaymentOptions().get(i)
                        .getStrPaymentType()));
    }
    ArrayAdapter<PaymentType> aAdapterPaymentType = new ArrayAdapter<PaymentType>(
            context, android.R.layout.simple_spinner_item,
            aListpaymentTypes);
    aAdapterPaymentType
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerPaymentType.setAdapter(aAdapterPaymentType);
    spinnerPaymentType.setOnItemSelectedListener(this);
    return rowView;
}

1 个答案:

答案 0 :(得分:1)

我认为您正在使用ArrayAdapter的getView()方法扩展此布局。在此方法中,您可以检查当前列表项是否是最后一个,如果不是,请禁用微调器。添加新项目时,使ArrayAdapter无效,以便重新绘制列表。

<强>更新

尝试在返回rowView之前添加此行:

//if spinner is last spinner, enable. Otherwise disbale.
spinnerPaymentType.setEnabled( (position == this.getCount()-1) );