I'm looking for instantiate dynamically a numberpicker. I want to place a value (kitQty - YourQty) in numberpicker order.
The next code is now working but only for the last one Id=9.
Because this only working with the last record?
Try setTag and setId and I can not change the numberpicker that desire. Anyone know what may be wrong in my code?
Thank you very much!
public class MyKits extends Fragment {
private Button performAudit;
private Button submitOrder;
NumberPicker yourQty;
NumberPicker kitQty;
NumberPicker order;
Conexion basedatos;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_mykits, container, false);
Context context=v.getContext();
basedatos=new Conexion(v.getContext());
TextView tvModel = (TextView) v.findViewById(R.id.tvModel);
TableLayout table= (TableLayout) v.findViewById(R.id.Tabla);
tvModel.setText(getArguments().getString("model"));
for(int i=0;i<10;i++)
{
TableRow tR = new TableRow(v.getContext());
LinearLayout l1= new LinearLayout(v.getContext());
LinearLayout l2= new LinearLayout(v.getContext());
LinearLayout l3= new LinearLayout(v.getContext());
LinearLayout l4= new LinearLayout(v.getContext());
Button item = new Button(v.getContext());
kitQty = new NumberPicker(getActivity().getApplicationContext());
yourQty= new NumberPicker(getActivity().getApplicationContext());
order= new NumberPicker(getActivity().getApplicationContext());
item.setText("Prueba Imparjdsbfjbdsjfgbijsdfgijnsfdignsidfgh");
l1.addView(kitQty);
l2.addView(item);
l3.addView(yourQty);
l4.addView(order);
///Tamaño Texto
item.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
kitQty.setMaxValue(100);
kitQty.setValue(50);
kitQty.setMinValue(0);
kitQty.setEnabled(false);
order.setMinValue(0);
order.setMaxValue(kitQty.getMaxValue());
yourQty.setMaxValue(kitQty.getMaxValue());
yourQty.setMinValue(0);
yourQty.setTag(i);
yourQty.setId(i);
order.setTag(i);
order.setId(i);
kitQty.setTag(i);
kitQty.setId(i);
yourQty.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
Log.e("oldValue: " + oldVal + " ID: " + picker.getId(), " newVal: " + newVal + " ID: " + picker.getTag());
kitQty.setId(picker.getId());
yourQty.setId(picker.getId());
order.setId(picker.getId());
order.setValue(kitQty.getValue() - yourQty.getValue());
}
});
order.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
Log.e("oldValue: " + oldVal + " ID: " + picker.getId(), " newVal: " + newVal + " ID: " + picker.getTag());
kitQty.setTag(picker.getTag());
yourQty.setTag(picker.getTag());
order.setTag(picker.getTag());
order.setValue(kitQty.getValue() - yourQty.getValue());
// calcularQty(picker);
}
});
tR.addView(l1);
tR.addView(l2);
tR.addView(l3);
tR.addView(l4);
table.addView(tR);
}
return v;}
public static MyKits newInstance(String model) {
MyKits f = new MyKits();
Bundle b = new Bundle();
b.putString("model", model);
f.setArguments(b);
return f;} }
Well, there are a few things wrong in your code... But let's just focus on the main question in saying first that you should not assign id by yourself. I'd rather either inflate each of them with a granular xml layout file, or if you insist on creating your widgets using "new", then leave the id alone and Android should assign them properly.
But really the main reason why your code don't work is that you assign widget ids in a "for" loop with your counter variable (i) and you are using the same value as an id for order widget, yourQty widget and kitQty widget. You need to understand that each widget id must be unique in the same view hierarchy. First start by fixing this issue. But I won't tell you how: you should be able to fix this by yourself.