如何在自定义适配器中单击按钮使同一行不可见?

时间:2015-10-14 04:38:11

标签: android

I have a delete button in my row, and while clicking it i want to make the current layout to be replaced by another which i have made gone in my xml class. But my problem is only the last row is getting replaced.

public class CustomGroupListAdapter extends BaseAdapter {



    private Activity activity               =   null;
    private static LayoutInflater inflater  = null;
    public Resources res                    = null;
    ArrayList<String> data                  = null;
    private String mfruits[];
    ViewHolder holder;
    String[] fruits;


    boolean isEdit=false;
    CustomGroupListAdapter(Activity a,boolean isEdit, Resources resLocal, String[] fruits){
        this.activity=a;
       // this.data=data1;
        this.res=resLocal;
        this.mfruits=fruits;
        this.isEdit=isEdit;

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

    }

    @Override
    public int getCount() {
        return mfruits.length;
    }


    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

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

        View vi = convertView;


        if(convertView==null){

            /****** Inflate tabitem.xml file for each row ( Defined below ) *******/
            vi = inflater.inflate(R.layout.list_item_child, null);   //*************************** ACTUAL*******
          //  vi = inflater.inflate(R.layout.addsharepoint_child, null);

            /****** View Holder Object to contain tabitem.xml file elements ******/

            holder = new ViewHolder();
            holder.txtTitle = (TextView) vi.findViewById(R.id.lblListItem);
            holder.txtTime=(TextView)vi.findViewById(R.id.txttime);
            holder.txtVersion=(TextView)vi.findViewById(R.id.txtversion);
            holder.image=(ImageView)vi.findViewById(R.id.image);
            holder.button=(Button)vi.findViewById(R.id.delete_icon);
            holder.relativeLayout=(RelativeLayout)vi.findViewById(R.id.delete);

            if(isEdit){
                holder.button.setVisibility(View.VISIBLE);
            }else
            {
                holder.button.setVisibility(View.GONE);
            }

            /************  Set holder with LayoutInflater ************/
            vi.setTag( holder );
           // holder.relativeLayout.setTag(holder);
        }
        else
            holder=(ViewHolder)vi.getTag();

        holder.txtTitle.setText(mfruits[position]);




        ///**************************************Trying for dialog box

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


                holder.relativeLayout.setVisibility(View.VISIBLE);

            }
        });




        return vi;
    }





    /********* Create a holder Class to contain inflated xml file elements *********/
    public static class ViewHolder{

        public TextView txtTitle;
        public TextView txtTime;
        public TextView txtVersion;
        public ImageView image;
        public Button button;

        public RelativeLayout relativeLayout;

    }


}

2 个答案:

答案 0 :(得分:0)

删除此代码;

if(isEdit){
    holder.button.setVisibility(View.VISIBLE);
}else
{
    holder.button.setVisibility(View.GONE);
}



并添加此代码;

holder.txtTitle.setText(mfruits[position]);
holder.button.setVisivility(View.GONE); // default setting
if (isEdit)
    holder.button.setVisivility(View.VISIBLE); // condition setting

答案 1 :(得分:0)

使用此代码

holder.txtTitle.setText(mfruits[position]);

holder.button.setVisivility(View.GONE);

if (isEdit){

    holder.button.setVisivility(View.VISIBLE); 
}