开发者

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

开发者 https://www.devze.com 2023-04-12 16:17 出处:网络
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.

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];
                开发者_如何学Pythonnew DownloadTask1().execute();
            break;  
        }
    }
}    

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


Use button.setVisibility(View.Invisible); for making your button invisible (The space occupied by the button remains as it is).

OR use button.setVisibility(View.GONE); (This removes the button from the layout and other views takes up the space)

So your onClick method will look like :

@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];
                btn1.setVisibility(View.INVISIBLE);   
                new DownloadTask1().execute();
            break;  
        }
    }

When you want to make them visible you have to use View.VISIBLE.

So you have to do like : button.setVisibility(View.VISIBLE);


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


@Kartik this worked ((ImageButton)v.findViewById(R.id.btn1)).setVisibility(View.INVISIBLE); thanks.

0

精彩评论

暂无评论...
验证码 换一张
取 消