2016-02-05 198 views
-2

我有一個listview與這麼多按鈕。當我點擊一個按鈕的listview另一個按鈕也點擊?我怎麼能阻止它。當我點擊一個按鈕的listview另一個按鈕也點擊我怎麼能阻止它

請解決此問題。 給我任何訣竅如何停止在列表視圖中的另一個按鈕。

這是我的代碼

private class MenuItemsAdapter extends ArrayAdapter<MenuItem> { 

    private static final String TAG = "MenuItemsAdapter"; 

    public MenuItemsAdapter(Context context, List<MenuItem> menuItems) { 
     super(context, 0, menuItems); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
    final MenuItem menuItem = getItem(position); 



     View view = convertView; 
     final ViewHolder viewHolder; 
     LayoutInflater inflater; 

      inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      view = inflater.inflate(R.layout.menu_item, parent, false); 
      viewHolder = new ViewHolder(); 
    //  viewHolder.half = (TextView) view.findViewById(R.id.half); 
      viewHolder.name = (TextView) view.findViewById(R.id.name); 
      viewHolder.description = (TextView) view.findViewById(R.id.description); 
      viewHolder.price = (TextView) view.findViewById(R.id.price); 
      viewHolder.add = (Button) view.findViewById(R.id.add); 
      viewHolder.selectedView = view.findViewById(R.id.selectedView); 
      viewHolder.remove = (Button) view.findViewById(R.id.remove); 
      viewHolder.total = (TextView) view.findViewById(R.id.itemTotal); 
      viewHolder.quantity = (TextView) view.findViewById(R.id.quantity); 

      view.setTag(viewHolder); 



     try 
     { 






     viewHolder.name.setText(menuItem.name); 
     viewHolder.description.setText(menuItem.description); 

     viewHolder.price.setText(String.valueOf(menuItem.price)); 



      viewHolder.add.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 


       mApplication.createNewCartIfPossibleAndAskIfNot(
         mActivity, mRestaurant, 
         new MainApplication.OnCreateCartListener() { 
          @Override 
          public void onCreateCart(Cart cart) { 


              cart.addOne(menuItem); 

              updateItemFromCart(menuItem, viewHolder); 
              updateCart(); 


          } 


         }); 

      } 
     }); 


     viewHolder.remove.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       if (!mApplication.isCartCreated()) { 
        return; 
       } 
       mApplication.getCart().removeOne(menuItem); 

       updateItemFromCart(menuItem, viewHolder); 
       updateCart(); 
      } 
     }); 

     }catch(NullPointerException e){e.printStackTrace();} 
     return view; 
    } 


    private void updateItemFromCart(MenuItem menuItem, ViewHolder viewHolder) { 


     if (!mApplication.isCartCreated()) { 
      return; 
     } 
     int quantity = mApplication.getCart().getNOfItemsOfType(menuItem); 
     if (quantity > 0) { 

      viewHolder.selectedView.setVisibility(View.VISIBLE); 



     } else { 

      viewHolder.selectedView.setVisibility(View.GONE); 

     } 

     viewHolder.quantity.setText(String.valueOf(quantity)); 

     viewHolder.total.setText(String.valueOf(quantity 
       * menuItem.price)); 

    } 

    class ViewHolder { 
     TextView name; 
     TextView description; 
     TextView price; 
     Button add; 
     View selectedView; 
     Button remove; 
     TextView total; 
     TextView quantity; 
     TextView half; 

    } 

} 
+4

哪裏是你的代碼 –

+1

被這意味着是一個笑話嗎? – pvn

+0

你可能會在按鈕的id之間做不匹配,這就是爲什麼它會發生。請發佈您的代碼,以便我們解決它。 – user5716019

回答

0

我能想到的你缺少裏面適配器靜態視圖架回收適配器實現。再次,這是基於文本和我過去的經驗的假設答案。更詳細的調查需要代碼研究。


你可以把空檢查你的觀點

final MenuItem menuItem = getItem(position); 
    View view = convertView; 
    final ViewHolder viewHolder; 

if (convertView == null) { 

    LayoutInflater inflater; 

     inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     view = inflater.inflate(R.layout.menu_item, parent, false); 
     viewHolder = new ViewHolder(); 
//  viewHolder.half = (TextView) view.findViewById(R.id.half); 
     viewHolder.name = (TextView) view.findViewById(R.id.name); 
     viewHolder.description = (TextView) view.findViewById(R.id.description); 
     viewHolder.price = (TextView) view.findViewById(R.id.price); 
     viewHolder.add = (Button) view.findViewById(R.id.add); 
     viewHolder.selectedView = view.findViewById(R.id.selectedView); 
     viewHolder.remove = (Button) view.findViewById(R.id.remove); 
     viewHolder.total = (TextView) view.findViewById(R.id.itemTotal); 
     viewHolder.quantity = (TextView) view.findViewById(R.id.quantity); 
     view.setTag(viewHolder); 
}else{ 
    viewHolder= (ViewHolder)convertView.getTag(); 
}