如何在自定义适配器中设置可见的不可见按钮

时间:2013-09-13 17:30:25

标签: android android-listview android-arrayadapter android-custom-view

我的问题是编辑点击查看 http://imageshack.us/a/img30/3070/2d7a.jpg

但我需要点击“EDITAR”它 http://imageshack.us/a/img543/1012/be3o.jpg

我有一个Button,可以为ListView的每个元素设置我的 DELETE 按钮。 但它只设置最后一个元素。如何设置所有可见元素?

MyButton在我的自定义适配器中是Ocult,当我调用设置我的按钮的方法时...这个按钮工作正常。

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

我的适配器:

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();
    }

1 个答案:

答案 0 :(得分:1)

Sadd这样的领域:

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()(现在,buttonsVisibleFlag设置为true)