Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing the view in onClick to a private class causes the wrong object to be sent
    text
    copied!<p>What im trying to do is display a checkbox, a button and a spinner in a TableLayout. The values are obtained from a web service and tableRows are added dynamically to the TableLayout.</p> <p>The first button creates a dialog with a listview. The listview contains the numbers 1-30. When the user clicks selects a value in the ListView, i need the text of the button to change to the value clicked. The code works for the first button that is clicked(The first time the listview is opened and a value is selected the text of the button is changed). But the second time i open the list view and select a button, the text of the first button changes and not the one which was clicked. What am i doing wrong?</p> <pre><code>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&lt;String&gt; arrayAda = new ArrayAdapter&lt;String&gt;(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&lt;?&gt; arg0, View arg1, int arg2, long arg3) { btn.setText(String.valueOf(arg3)); } } </code></pre> <p>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..</p> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload