это мой первый пост, и у меня есть сомнения относительно управления элементами списка, я видел сообщение о recycleView, но я не знаю, Не понимаю, как это реализовать в моей проблеме.Android Listview item, когда я реализую setCheck (true) в любом флажке, какой-то элемент изменен случайно
Ну, у меня есть listView, который показывает некоторые элементы, такие как торговый автомобиль, где пользователь нажимает кнопку autorizarDetalleSolicitud, которая открывает alertDialog, где пользователь выбирает желаемое количество соответствующего элемента, поэтому, если сумма больше чем ноль, флажок изменяется в состояние TRUE, но когда я проверяю эту функцию, я вижу, что другие checkBoxes активируются (или изменяются в состояние TRUE) случайным образом; т. е. когда я ввожу сумму в первый элемент, состояние предпоследнего checkBox изменяется на значение ИСТИНА. Спасибо за внимание.
//Button of each listview item
public void autorizarDetalleSolicitud(View v) {
LinearLayout layoutboton = (LinearLayout) v.getParent();
checkBoxelemento = (CheckBox) layoutboton.getChildAt(0);
RelativeLayout relativeLayout = (RelativeLayout) layoutboton.getParent();
TableLayout tableLayout = (TableLayout) relativeLayout.getChildAt(1);
TextView tipoelementotextview = (TextView) tableLayout.findViewById(R.id.tipoelem);
TextView unidadelem = (TextView) tableLayout.findViewById(R.id.unidadelem);
TextView idelem = (TextView) tableLayout.findViewById(R.id.idelem);
TextView cantpedida = (TextView) tableLayout.findViewById(R.id.cantidadelem);
cantautorizadatextview = (TextView) tableLayout.findViewById(R.id.cantautorizado);
final String tipoelemento = tipoelementotextview.getText().toString();
cantidadpedida = Double.parseDouble(cantpedida.getText().toString());
cantidadautorizada = Double.parseDouble(cantautorizadatextview.getText().toString());
idelemento = idelem.getText().toString();
// configura el alert dialog
builder = new AlertDialog.Builder(this);
builder.setMessage(unidadelem.getText() + " a autorizar:");
builder.setNegativeButton(R.string.cancelar, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
if (tipoelemento.equals("Material")) {
numberPickerCantidad = new NumberPicker(getApplicationContext());
numberPickerCantidad.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
numberPickerCantidad.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
String[] nums = new String[100];
for (int i = 0; i < nums.length; i++) {
nums[i] = Integer.toString(i);
}
numberPickerCantidad.setMinValue(0);
numberPickerCantidad.setMaxValue(nums.length - 1);
numberPickerCantidad.setWrapSelectorWheel(false);
numberPickerCantidad.setDisplayedValues(nums);
if(cantidadautorizada==0){
numberPickerCantidad.setValue(cantidadpedida.intValue());
}else{
numberPickerCantidad.setValue(cantidadautorizada.intValue());
}
builder.setPositiveButton(R.string.agregar, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
int cantidadautorizada = numberPickerCantidad.getValue();
autorizarCantidad((double)cantidadautorizada,tipoelemento);
}
});
builder.setView(numberPickerCantidad);
} else {
cantreactivosoli = new EditText(getApplicationContext());
cantreactivosoli.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
cantreactivosoli.setFilters(new InputFilter[]{new InputFilter.LengthFilter(4)});
cantreactivosoli.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
if(cantidadautorizada==0){
cantreactivosoli.setText(cantidadpedida + "");
}else{
cantreactivosoli.setText(cantidadautorizada + "");
}
cantreactivosoli.setText(cantidadpedida + "");
builder.setView(cantreactivosoli);
builder.setPositiveButton(R.string.aceptar, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String cantidadedittext = cantreactivosoli.getText().toString();
if (!cantidadedittext.equals("")) {
Double cantidadingresada = Double.parseDouble(cantidadedittext);
autorizarCantidad(cantidadingresada,tipoelemento);
} else {
Toast toast = Toast.makeText(getApplicationContext(), R.string.error_sincantidad, Toast.LENGTH_SHORT);
toast.show();
}
}
});
builder.setView(cantreactivosoli);
}
builder.show();
}
// Method that saves the input value in an arraylist and implements a setCheck(true) of that itemstrong text
public void autorizarCantidad(Double cantidadingresada, String tipoelemento){
if (cantidadingresada <= cantidadpedida) {
int flag = 0;
for (int indice = 0; indice < elementosrevisados.size(); indice++) {
if (elementosrevisados.get(indice).getId().equals(idelemento)) {
elementosrevisados.get(indice).setCantidadAutorizada(cantidadingresada+"");
if (tipoelemento.equals("Material")) {
cantautorizadatextview.setText(cantidadingresada.intValue()+"");
}else{
cantautorizadatextview.setText(cantidadingresada+"");
}
cantidadpedida = cantidadingresada;
flag=1;
break;
}
}
if(flag==0) {
Elemento elem = new Elemento(idelemento, cantidadpedida + "", cantidadingresada + "");
elementosrevisados.add(elem);
checkBoxelemento.setChecked(true);
if (tipoelemento.equals("Material")) {
cantautorizadatextview.setText(cantidadingresada.intValue()+"");
}else{
cantautorizadatextview.setText(cantidadingresada+"");
}
}
for(int i=0;i<elementosrevisados.size();i++){
Log.e("Elemento revisado: ",elementosrevisados.get(i).toString());
}
} else {
Toast toast = Toast.makeText(getApplicationContext(), R.string.error_cantidadautorizada, Toast.LENGTH_SHORT);
toast.show();
}
}
Необходимо сохранить позицию щелкнутого виджета. Для получения дополнительной информации см. Http://stackoverflow.com/a/38887338/2404262 –
@AnshulTyagi спасибо, я думал о чем-то подобном. –
дайте мне знать, если это работает –