【问题标题】:Display hide button in a listView在 listView 中显示隐藏按钮
【发布时间】:2012-10-22 10:18:26
【问题描述】:

对不起,我是法国人^^

所以,我有一个小问题要问你!

我想在 listView 中显示一个隐藏按钮,但是当代码显示所有按钮(不仅仅是一个)时。

看看我的代码:

public class ListViewShoplistAdapter extends BaseAdapter {

private ArrayList<Product> listCategory;
private Activity activity;
private ShopList shoplist;

public ListViewShoplistAdapter(Activity activity,ArrayList<Product> listCategory, ShopList shoplist) {
    super();
    this.activity = activity;
    this.listCategory = listCategory;
    this.shoplist = shoplist;
}

public int getCount() {
    return listCategory.size();
}

public Product getItem(int position) {
    return listCategory.get(position);
}

public long getItemId(int position) {
    return position;
}

private class ViewHolder {
    public TextView productName;
    public TextView productPrice;
    public TextView productBrand;
    public ImageView product;
    public Button changeQuantity; 
    public RelativeLayout background;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder view;
    LayoutInflater inflator = activity.getLayoutInflater();

    if(convertView == null) {
        view = new ViewHolder();
        convertView = inflator.inflate(R.layout.listviewshoplist, parent, false);

        view.productName = (TextView) convertView.findViewById(R.id.name);
        view.productPrice = (TextView) convertView.findViewById(R.id.price);
        view.productBrand = (TextView) convertView.findViewById(R.id.brand);
        view.changeQuantity = (Button) convertView.findViewById(R.id.changequantity);
        view.background = (RelativeLayout) convertView.findViewById(R.id.relativeLayout1);
        view.product = (ImageView) convertView.findViewById(R.id.imgproduct);
        convertView.setTag(view);
    }else {
        view = (ViewHolder) convertView.getTag();
    }

    String productName = listCategory.get(position).getName();
    view.productName.setText(productName);

    if (position==0){ // JUST for the first position, display the changeQuantity button ! => Button is displayed for all rows...
        view.changeQuantity.setVisibility(View.VISIBLE);
    }

    String price = String.valueOf(listCategory.get(position).getPrice())+"€";
    view.productPrice.setText(price);

    return convertView;

}

}

感谢您的帮助!

【问题讨论】:

    标签: android listview button adapter


    【解决方案1】:

    根据我的经验,您总是需要将else 放在if 之后。

    所以你需要类似的东西

    if (position==0){ // JUST for the first position, display the changeQuantity button ! => Button is displayed for all rows...
            view.changeQuantity.setVisibility(View.VISIBLE);
        } else {
    view.changeQuantity.setVisibility(View.INVISIBLE); // or View.GONE
    }
    

    【讨论】:

      【解决方案2】:

      您也应该将按钮设置为GONE

       if (position==0){ 
          view.changeQuantity.setVisibility(View.VISIBLE);
       } else 
          view.changeQuantity.setVisibility(View.GONE);
      

      【讨论】:

        【解决方案3】:

        在你的代码中试试这个..

        if (position==0){ // JUST for the first position, display the changeQuantity button ! => Button is displayed for all rows...
            view.changeQuantity.setVisibility(View.VISIBLE);
        }
        else   // visibility gone
        {
            view.changeQuantity.setVisibility(View.Gone);
        
        }
        

        【讨论】:

          猜你喜欢
          • 2014-01-13
          • 2019-08-23
          • 1970-01-01
          • 2012-10-12
          • 2021-06-05
          • 1970-01-01
          • 1970-01-01
          • 2023-04-10
          • 1970-01-01
          相关资源
          最近更新 更多