0

我想做的是在 TableLayout 中显示一个复选框、一个按钮和一个微调器。这些值是从 Web 服务获取的,并且 tableRows 会动态添加到 TableLayout。

第一个按钮创建一个带有列表视图的对话框。列表视图包含数字 1-30。当用户单击在 ListView 中选择一个值时,我需要将按钮的文本更改为单击的值。该代码适用于单击的第一个按钮(第一次打开列表视图并选择一个值时,按钮的文本会更改)。但是第二次打开列表视图并选择一个按钮时,第一个按钮的文本会发生变化,而不是被点击的那个。我究竟做错了什么?

protected void fillTableView() {
    for (MedicineInfo temp : orderedMedList) {
        LayoutInflater inflater = getLayoutInflater();
        TableRow tr = (TableRow) inflater.inflate(
                R.layout.neworderrestockmedicinelist2, tlOrderInfo, false);
        CheckBox cbMedicine = (CheckBox) tr
                .findViewById(R.id.cbNewOrderRestockMedName);
        cbMedicine.setText(temp.vcProduct);
        Button btnQty = (Button) tr
                .findViewById(R.id.btnNewOrderRestockQty);
        Spinner spnIntakeUnit = (Spinner) tr
                .findViewById(R.id.spnNewOrderRestockIntakeUnit);
        IntakeUnitAdapter intakeUnitAda = new IntakeUnitAdapter(this);
        spnIntakeUnit.setAdapter(intakeUnitAda);
        ArrayAdapter<String> arrayAda = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, numberArray);
        arrayAda.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        tlOrderInfo.addView(tr);
        btnQty.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                dlgQty = new Dialog(mContext);
                TableRow tr = (TableRow) v.getParent();
                ListView lvQty = new ListView(mContext);
                QuantityAdapter qtyAda = new QuantityAdapter(mContext);
                lvQty.setAdapter(qtyAda);
                dlgQty.addContentView(lvQty,
                        new LinearLayout.LayoutParams(
                                LayoutParams.FILL_PARENT,
                                LayoutParams.WRAP_CONTENT));
                ItemSelectTest test = new ItemSelectTest(tr);
                lvQty.setOnItemClickListener(test);
                showDialog(DLG_QTY);
                test = null;

            }
        });
    }
}

private class ItemSelectTest implements OnItemClickListener {
    Button btn = null;

    public ItemSelectTest(TableRow tr) {
        Button bt = (Button) tr.findViewById(R.id.btnNewOrderRestockQty);
        btn = bt;
    }

    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        btn.setText(String.valueOf(arg3));
    }

}

In the same shown above, i'm passing the TableRow that contains the button. I've also tried passing v from onClick. The problem is the same either way..

Thanks

4

1 回答 1

1

I think that the problem is the way that you're showing the dialog. If you look at the docs for showDialog (here):

Show a dialog managed by this activity. A call to onCreateDialog(int, Bundle) will be made with the same id the first time this is called for a given id. From thereafter, the dialog will be automatically saved and restored.

Presumably you're returning dlgQty in onCreateDialog. The thing is, that dialog will be cached after the first time. A quick fix for this is to always call removeDialog when you are done with it.

于 2012-08-18T14:43:35.260 回答