ListView滚动重置

时间:2017-04-26 18:26:59

标签: android listview android-arrayadapter

我在列表中使用适配器,以及按钮的视图, 某些按钮被阻止,但如果您滚动此按钮的下拉列表启用了另一个不匹配的错误位置,有什么办法可以解决这个问题吗?

这是我的适配器

/**
 * Created by Isai on 15/03/17.
 */
public class AdapterFood extends ArrayAdapter<Charola.Producto> {
    ImageView imgFood;

    private String nameService = "AgregarItemCharola";
    private String usuarioId = "UsuarioId";
    private String productoId = "ProductoId";
    private String cantidad = "Cantidad";
    private String especificaciones = "Especificaciones";
    private String urlComplete = Constantes.URLMAIN + nameService + "?";
    private Fragment fragment;
    private List<Charola.Producto> productoList;
    private int contador;
    private String totalCharola;



    public AdapterFood(Context context, List<Charola.Producto> productoList, Fragment fragment) {
        super(context, 0, productoList);
        this.fragment = fragment;
        this.productoList = productoList;

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {

        LayoutInflater inflater = (LayoutInflater)
                getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View listViewInflate = convertView;
        if (convertView == null) {
            listViewInflate = inflater.inflate(R.layout.item_menu, viewGroup, false);
        }
        imgFood = (ImageView) listViewInflate.findViewById(R.id.imageFood);
        TextView tituloFood = (TextView) listViewInflate.findViewById(R.id.tittleFood);
        TextView descricionFood = (TextView) listViewInflate.findViewById(R.id.descriptionFood);
        TextView precio = (TextView) listViewInflate.findViewById(R.id.priceFood);

        final Button btnAnadir = (Button) listViewInflate.findViewById(R.id.buttonAnadir);
        final Charola.Producto producto = getItem(position);
        tituloFood.setText(producto.getNombre().toString());
        descricionFood.setText(producto.getDescripcion());
        precio.setText("$" + producto.getPrecio() + ".00");

        if(producto.isExistencia()){
            if (producto.getCantidad() != 0) {
                btnAnadir.setText("+ Añadir (" + producto.getCantidad() + ")");
                btnAnadir.setBackgroundResource(R.drawable.btns_dialogs);
            } else {
                btnAnadir.setText("+ Añadir");
                btnAnadir.setBackgroundResource(R.drawable.btn_anadir);
            }
            btnAnadir.setTag(position);
            btnAnadir.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    System.out.println("La etiqueta cambio del siguiente forma: "+ btnAnadir.getTag());
                    btnAnadir.setBackgroundResource(R.drawable.btns_dialogs);
                    new consumeServiceAgregarProductoACharola().execute(urlComplete + "#" + producto.getIdProducto());
                    System.out.println("+ Añadir("+(producto.getCantidad()+1)+")");
                    btnAnadir.setText("+ Añadir("+(producto.getCantidad()+1)+")");
                    producto.setCantidad((producto.getCantidad()+1));
                    //notifyDataSetChanged();
                }
            });

        }else{
            btnAnadir.setBackgroundResource(R.drawable.btn_agotado);
            btnAnadir.setBackgroundResource(R.drawable.btn_agotado);
            btnAnadir.setText("Agotado");
        }

        Glide.with(getContext()).
                load(producto.getUrlFoto()).placeholder(R.drawable.place_holder)
                .into(imgFood);
        //Escalar Vistas
       /* GuiTools guiTools = GuiTools.getCurrent();
        guiTools.scale(tituloFood);
        guiTools.scale(descricionFood);
        guiTools.scale(precio);
        guiTools.scale(btnAnadir);*/
       notifyDataSetChanged();


        return listViewInflate;
    }

    //recuperar id usuario
    public int recuperarIdUsario() {
        SharedPreferences sharedPreferences = getContext().getSharedPreferences("MIS_PREFRENCIAS", Context.MODE_PRIVATE);
        int valor = sharedPreferences.getInt("id", 1);
        return valor;
    }

    //Consume service AgregarProducto
    class consumeServiceAgregarProductoACharola extends AsyncTask<String, Void, Void> {
        private AlertDialogLoading loading;

        @Override
        protected Void doInBackground(String... params) {
            String delimiter = "#";
            String[] arrayLink = params[0].split("#");
            URL url = null;
            try {
                url = new URL(arrayLink[0]);
                Log.d("URL ---------->", url.toString());

                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setRequestMethod("POST");// La conexión es por el metodo POST
                urlConnection.setRequestProperty("Content-Type", "application/json");
                urlConnection.setDoInput(true);
                urlConnection.setDoOutput(true);
                urlConnection.setUseCaches(false);
                urlConnection.connect();

                JSONObject jsonParam = new JSONObject();
                jsonParam.put("UsuarioId", recuperarIdUsario());
                jsonParam.put("ProductoId", arrayLink[1]);
                jsonParam.put("Cantidad", 1);
                jsonParam.put("Especificaciones", "");

                // Send POST output.
                OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream());
                out.write(jsonParam.toString());
                out.close();

                StringBuilder sb = new StringBuilder();
                int HttpResult = urlConnection.getResponseCode();
                if (HttpResult == HttpURLConnection.HTTP_OK) {
                    BufferedReader br = new BufferedReader(new InputStreamReader(
                            urlConnection.getInputStream(), "utf-8"));
                    String line = null;
                    while ((line = br.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                    br.close();

                    JSONObject jsonObject = new JSONObject(sb.toString());
                    Log.d("RESPUESTA ----->",jsonObject.toString());
                    Log.d("TAMAÑO DEL CH------>", String.valueOf(jsonObject.getInt("TotalCharola")));
                    totalCharola =  String.valueOf(jsonObject.getInt("TotalCharola"));

                } else {
                    Log.e("NO SUCCESS", urlConnection.getResponseMessage());
                }


            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (ProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }


            return null;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loading = new AlertDialogLoading(fragment.getActivity());
            loading.messageDialog("", "");

        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);

            PrincipalViewController principalViewController = new PrincipalViewController();
            principalViewController.ahBottomNavigation.setNotification(totalCharola,2);


            loading.closeMessage();

        }


    }



}

0 个答案:

没有答案