使用自定义适配器编辑listview中的项目,具体取决于值

时间:2015-08-07 16:46:16

标签: android listview android-listview custom-adapter

当我点击listview中的一个按钮时,我会调用一个函数,我会根据点击的按钮清楚地发送一个值。当我得到项目字段更改值的肯定响应函数时,第一个实例是validado = 0,如果单击按钮,则更改为validado = 1。如果将此验证为1,则该按钮应具有背景图像,以警告此项已经过验证。所有这一切对我来说都很适合,但如果我点击任何按钮,无论我的列表中有多少项,总是更改单击它的项目以及列表中的第一项。非常有趣的是,当我的数据基于本地和我的服务器时,第一项的验证值仍为0

我的适配器中的getView

@Override
public View getView(int position, View convertView, final ViewGroup parent) {
    ViewHolder holder = null;
    RowItem rowItem = getItem(position);
    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.lista_validacion_multiple, null);
        holder = new ViewHolder();
        holder.txtNombre = (TextView)convertView.findViewById(R.id.txtNombre);
        holder.txtAsiento = (TextView)convertView.findViewById(R.id.txtAsiento);
        holder.txtTicket = (TextView)convertView.findViewById(R.id.txtTicket);
        holder.txtNumero = (TextView)convertView.findViewById(R.id.txtNumero);
        holder.btn = (Button)convertView.findViewById(R.id.button1);
        convertView.setTag(holder);
    } else
        holder = (ViewHolder) convertView.getTag();

    holder.txtNombre.setText("Nombre :"+rowItem.getNombre());
    holder.txtTicket.setText("Ticket :"+rowItem.getTicket());
    if (!rowItem.getAsiento().equals("") && !rowItem.getAsiento().equals("null") && rowItem.getAsiento() != null) {
        holder.txtAsiento.setText("Asiento :"+rowItem.getAsiento());
    }
    if (!rowItem.getNumero().equals("") && !rowItem.getNumero().equals("null") && rowItem.getNumero() != null) {
        holder.txtNumero.setText("Número :"+rowItem.getNumero());
    }
    System.out.println("item "+rowItem.getId_inscripcion()+" validado = "+rowItem.getValidado());
    if(rowItem.getValidado()==1){
        System.out.println("ENTRO");
        holder.btn.setBackgroundResource(R.drawable.icon_big_alert);
        holder.btn.setText("");
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int)LayoutParams.WRAP_CONTENT, (int)LayoutParams.WRAP_CONTENT);
        params.width = 50;
        params.height = 50;
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        holder.btn.setLayoutParams(params);
    }else{
        holder.btn.setTag(position);
        holder.btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                int position=(Integer)v.getTag();
                RowItem item_click = getItem(position);
                Connection cn = new Connection();
                SessionManager manager = new SessionManager();
                BaseDeDatos nueva = new BaseDeDatos();
                JSONObject json = new JSONObject();
                if(cn.isNetworkAvailable(parent.getContext())){
                    String nombreCliente = manager.getValue(parent.getContext(), "nombreCliente");
                    String user = manager.getValue(parent.getContext(), "nombreUser");
                    String folioEvento = manager.getValue(parent.getContext(), "folioEvento");
                    String codigoEvento = manager.getValue(parent.getContext(), "codigoEvento");
                    String seleccionValidadora = manager.getValue(parent.getContext(), "opcionVerificadora");
                    String nombreUser = manager.getValue(parent.getContext(), "nombreUser");
                    String hashUser = manager.getValue(parent.getContext(), "hashUsuario");
                    String URL_TICKET = Config.URL_BASE + nombreCliente
                            + "/" + Config.URL_VALIDACION_TICKET
                            + nombreUser + "/" + hashUser + "/"
                            + folioEvento + "/" + item_click.getHash()
                            + "/0/";
                    System.out.println(URL_TICKET);
                    if (manager.getValue(parent.getContext(),
                            "checkin") != null) {
                        int id_checkin = nueva.idCheckin(parent.getContext(), manager.getValue(parent.getContext(),"checkin"));
                        String url = URL_TICKET + id_checkin;
                        HttpClient httpClient = new DefaultHttpClient();
                        HttpPost post = new HttpPost(url);
                        post.setHeader("content-type",
                                "application/json");
                        try {
                            // Construimos el objeto cliente en formato
                            // JSON
                            JSONObject dato = new JSONObject();
                            StringEntity entity = new StringEntity(
                                    dato.toString());
                            post.setEntity(entity);
                            HttpResponse resp = httpClient
                                    .execute(post);
                            String respStr = EntityUtils.toString(resp
                                    .getEntity());
                            JSONObject respJSON = new JSONObject(
                                    respStr);
                            json = respJSON;
                        } catch (Exception ex) {
                            Log.e("ServicioRest", "Error!", ex);
                        }
                    } else {
                        String url = URL_TICKET;
                        HttpClient httpClient = new DefaultHttpClient();
                        HttpPost post = new HttpPost(url);
                        post.setHeader("content-type",
                                "application/json");
                        try {
                            // Construimos el objeto cliente en formato
                            // JSON
                            JSONObject dato = new JSONObject();
                            StringEntity entity = new StringEntity(
                                    dato.toString());
                            post.setEntity(entity);
                            HttpResponse resp = httpClient
                                    .execute(post);
                            String respStr = EntityUtils.toString(resp
                                    .getEntity());
                            JSONObject respJSON = new JSONObject(
                                    respStr);
                            json = respJSON;
                        } catch (Exception ex) {
                            Log.e("ServicioRest", "Error!", ex);
                        }
                    }
                    System.out.println(json);
                }else{

                }
            }
        });

    }
    return convertView;
}

我显示日志以显示该值仍为0

08-07 13:39:21.890: I/System.out(14317): item 936 validado = 0
08-07 13:39:21.890: I/System.out(14317): item 937 validado = 0
08-07 13:39:21.890: I/System.out(14317): item 938 validado = 0

当你第一次进入列表视图的视图时,这将注销,然后我将验证一个铭文,无论如何,这就是我得到的

08-07 13:44:08.300: I/System.out(14317): item 936 validado = 0
08-07 13:44:08.300: I/System.out(14317): item 937 validado = 1
08-07 13:44:08.305: I/System.out(14317): item 938 validado = 0

没关系,除了第一项加上按钮的费用,该按钮验证了我只想向rowItem.getValidado()==1

中的元素收费的图像

修改

我回答了我的问题。

2 个答案:

答案 0 :(得分:0)

所以你点击ListView上的1个项目会改变它的外观(这是预期的),因为你的值会变为1.但无论如何,ListView中的第一个可见项也是变化(无意)?

我不是专家,但我认为你的ViewHolder有问题。尝试删除ViewHolder逻辑并查看是否仍然出现问题(用于测试)。 ListView对于回收视图并使用新数据重新填充它们非常棘手。您的getItem()可能会遇到问题。但我怀疑它是ViewHolder逻辑。

每次调用Inflater时,您还需要声明getView()吗?问题应该转移到构造函数,并在getView()中实例化它。

答案 1 :(得分:0)

错误是代码的位置,而不是如何。

@Override
public View getView(int position, View convertView, final ViewGroup parent) {
    ViewHolder holder = null;
    RowItem rowItem = getItem(position);
    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.lista_validacion_multiple, null);
        holder = new ViewHolder();
        holder.txtNombre = (TextView)convertView.findViewById(R.id.txtNombre);
        holder.txtAsiento = (TextView)convertView.findViewById(R.id.txtAsiento);
        holder.txtTicket = (TextView)convertView.findViewById(R.id.txtTicket);
        holder.txtNumero = (TextView)convertView.findViewById(R.id.txtNumero);
        holder.btn = (Button)convertView.findViewById(R.id.button1);
        holder.txtNombre.setText("Nombre :"+rowItem.getNombre());
        holder.txtTicket.setText("Ticket :"+rowItem.getTicket());
        if (!rowItem.getAsiento().equals("") && !rowItem.getAsiento().equals("null") && rowItem.getAsiento() != null) {
            holder.txtAsiento.setText("Asiento :"+rowItem.getAsiento());
        }
        if (!rowItem.getNumero().equals("") && !rowItem.getNumero().equals("null") && rowItem.getNumero() != null) {
            holder.txtNumero.setText("Número :"+rowItem.getNumero());
        }
        System.out.println("item "+rowItem.getId_inscripcion()+" validado = "+rowItem.getValidado());
        if(rowItem.getValidado()==1){
            holder.btn.setBackgroundResource(R.drawable.icon_big_alert);
            holder.btn.setText("");
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int)LayoutParams.WRAP_CONTENT, (int)LayoutParams.WRAP_CONTENT);
            params.width = 50;
            params.height = 50;
            params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            holder.btn.setLayoutParams(params);
        }else{
            holder.btn.setTag(position);
            holder.btn.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    int position=(Integer)v.getTag();
                    RowItem item_click = getItem(position);
                    Connection cn = new Connection();
                    SessionManager manager = new SessionManager();
                    BaseDeDatos nueva = new BaseDeDatos();
                    JSONObject json = new JSONObject();
                    if(cn.isNetworkAvailable(parent.getContext())){
                        String nombreCliente = manager.getValue(parent.getContext(), "nombreCliente");
                        String user = manager.getValue(parent.getContext(), "nombreUser");
                        String folioEvento = manager.getValue(parent.getContext(), "folioEvento");
                        String codigoEvento = manager.getValue(parent.getContext(), "codigoEvento");
                        String seleccionValidadora = manager.getValue(parent.getContext(), "opcionVerificadora");
                        String nombreUser = manager.getValue(parent.getContext(), "nombreUser");
                        String hashUser = manager.getValue(parent.getContext(), "hashUsuario");
                        String URL_TICKET = Config.URL_BASE + nombreCliente
                                + "/" + Config.URL_VALIDACION_TICKET
                                + nombreUser + "/" + hashUser + "/"
                                + folioEvento + "/" + item_click.getHash()
                                + "/0/";
                        System.out.println(URL_TICKET);
                        if (manager.getValue(parent.getContext(),
                                "checkin") != null) {
                            int id_checkin = nueva.idCheckin(parent.getContext(), manager.getValue(parent.getContext(),"checkin"));
                            String url = URL_TICKET + id_checkin;
                            HttpClient httpClient = new DefaultHttpClient();
                            HttpPost post = new HttpPost(url);
                            post.setHeader("content-type",
                                    "application/json");
                            try {
                                // Construimos el objeto cliente en formato
                                // JSON
                                JSONObject dato = new JSONObject();
                                StringEntity entity = new StringEntity(
                                        dato.toString());
                                post.setEntity(entity);
                                HttpResponse resp = httpClient
                                        .execute(post);
                                String respStr = EntityUtils.toString(resp
                                        .getEntity());
                                JSONObject respJSON = new JSONObject(
                                        respStr);
                                json = respJSON;
                            } catch (Exception ex) {
                                Log.e("ServicioRest", "Error!", ex);
                            }
                        } else {
                            String url = URL_TICKET;
                            HttpClient httpClient = new DefaultHttpClient();
                            HttpPost post = new HttpPost(url);
                            post.setHeader("content-type",
                                    "application/json");
                            try {
                                // Construimos el objeto cliente en formato
                                // JSON
                                JSONObject dato = new JSONObject();
                                StringEntity entity = new StringEntity(
                                        dato.toString());
                                post.setEntity(entity);
                                HttpResponse resp = httpClient
                                        .execute(post);
                                String respStr = EntityUtils.toString(resp
                                        .getEntity());
                                JSONObject respJSON = new JSONObject(
                                        respStr);
                                json = respJSON;
                            } catch (Exception ex) {
                                Log.e("ServicioRest", "Error!", ex);
                            }
                        }
                        System.out.println(json);
                    }else{

                    }
                }
            });

        }
        convertView.setTag(holder);
    } else
        holder = (ViewHolder) convertView.getTag();
    return convertView;
}