Search code examples
androidlistviewbuttoninvisible

how to make a button get invisible in listview of an android app


in my app i have a list view. Each list consists of a image, text and three buttons. I have placed all this in a ListActivity.

When the user clicks a particular button i use to call the download function of my app. At that time i want that button to be get invisibled. Following is the part of my code

public class Content extends ListActivity 
{
  public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        adapter = new EventAdapter(this);
        setListAdapter(adapter);
    }

  public class InventoryAdapter extends BaseAdapter implements OnClickListener
{        
    private Context context;   
    ImageButton b1;
    public InventoryAdapter(Context ctx) 
    {     
        context = ctx;
    }
    public long getItemId(int position) 
    {
        return position;
    }
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View view;
        if(convertView == null) 
        {
            LayoutInflater inflater = (LayoutInflater)
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.list, null);
            b1 = (ImageButton)view.findViewById(R.id.Btn1);
            b1.setOnClickListener(this);
        }
        else 
        {
            view = convertView;
        }
        b1.setTag(position);            
        return view;
   }

    @Override
    public void onClick(View v) 
    {
        Log.e("onclick","onclick");
        Integer position = (Integer) v.getTag();
        switch(v.getId())
        {
            case R.id.Btn1:
                selected_url=url[position];
                new DownloadTask1().execute();
            break;  
        }
    }
}    

How to make the a particular button to get invisibled when the a position is clicked


Solution

  • create an array storing the list of clicks over the button according to their position.

    public static ArrayList<Integer> list_Btn    = new ArrayList<Integer>(); //to make the buy button either visible or invisible
    

    In the onclick of the button in list view to be as follows

    request_holder.button.setOnClickListener(new OnClickListener() 
                {
                    @Override
                    public void onClick(View v) 
                    {
                                       Appconstant.list_Btn.add(position,View.INVISIBLE);       
                    }
                });
                return convertView;
    

    Then in the show list data of list view to be as follows

    public void ListData(listViewHolder viewHolder, int position) 
            {
                    if(Appconstant.list_Btn.get(position) == View.VISIBLE)
                    {
                        request_holder.buy.setVisibility(View.VISIBLE);
                    }
                    else if(Appconstant.list_Btn.get(position)== View.INVISIBLE)
                    {
                        request_holder.buy.setVisibility(View.INVISIBLE);
                    }
            }