0

My problem is editing on click see http://imageshack.us/a/img30/3070/2d7a.jpg

But I need that on click "EDITAR" it does http://imageshack.us/a/img543/1012/be3o.jpg

I have a Button that set my DELETE button visible for each element of the ListView. But it only sets visible the last element. How can I set all the elements visible ?

MyButton is Ocult in my custom adapter and when I call the method that set my buttons visible ... This button Works Fine.

    //button "EDITAR"
    btnBoton2 = (Button)findViewById(R.id.eliminar_primero);
    btnBoton2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0)
        {
         personaAdapter.setVisibility();
        }
    });

My adapter :

public class PersonaCustomAdapter  extends ArrayAdapter<Persona> {

    Context context;
    int layoutResourceId;
    ArrayList<Persona> data = new ArrayList<Persona>();
    private PersonaDAO personaDao;
    UserHolder holder;
    int posicion;



    public PersonaCustomAdapter(Context context, int layoutResourceId,ArrayList<Persona> data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View row = convertView;
        holder = null;
        posicion = position;

        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            holder = new UserHolder();
            holder.textName = (TextView) row.findViewById(R.id.textView1);
            holder.btnEdit = (Button) row.findViewById(R.id.boton_eliminame);
            holder.btnEdit.setVisibility(View.INVISIBLE);
            row.setTag(holder);
        } else {
            holder = (UserHolder) row.getTag();
        }
        personaDao = new PersonaDAO(context);
        personaDao.open();

        holder.textName.setText(persona.getNombre_persona());


         holder.btnEdit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                if (persona.getId_persona() > 0) {
                    MyActivity hola = new MyActivity();
                    if ( personaDao.eliminarPersona(persona)){
                        data.remove(position);
                        notifyDataSetChanged();
                    } else {
                        System.out.println("No se logro eliminar");
                    }

                }
            }
        });

        return row;
    }
    public void setVisibility()
    {

     // Here is where i getVisibility of my elementos.. is INVISIBLE by Default but
    //only setVisible The lastElement


            if(holder.btnEdit.getVisibility() == View.INVISIBLE){
                holder.btnEdit.setVisibility(View.VISIBLE);
            } else if(holder.btnEdit.getVisibility() == View.VISIBLE){
                holder.btnEdit.setVisibility(View.INVISIBLE);
           }
            notifyDataSetChanged();

        }

    public int getCount() {
        return data.size();
    }
4

1 回答 1

1

添加如下字段:

public class PersonaCustomAdapter extends ArrayAdapter {
private boolean buttonsVisible = false;
    ... 
    public void setButtonsVisible(boolean isVisible) {
        buttonsVisible = isVisible;
    }
    ...
    public View getView(...) {
        ...
        holder.btnEdit.setVisibility(buttonsVisible ? View.VISIBLE : View.INVISIBLE)
        ...
    }  
}

现在设置您的按钮可见呼叫

adapter.setButtonsVisible(true);
adapter.notifyDataSetChanged();

这将导致您的 ListView 再次为所有视图调用 getView()(buttonVisibleFlag 现在设置为 true)

于 2013-09-13T17:39:02.733 回答