2017-03-22 64 views
0

這是我如何使用適配器執行GridView,有人能告訴我如何修改這些代碼以獲得多項選擇效果?以下是我的代碼,懇請提供,並提供一千多謝謝。如何使用GridView android做多選?

//privateList<Condiment> CondimentList; 
CondimentList = getCondimentDescription(productcode); 
condimentGrid.setAdapter(new condimentlist()); 

public class condimentlist extends BaseAdapter { 

     LayoutInflater mInflater; 

     public condimentlist() { 
      mInflater = LayoutInflater.from(cart_list.this); 
     } 

     @Override 
     public int getCount() { 
      return CondimentList.size(); 
     } 

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

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

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

      if (convertView == null) { 
       convertView = mInflater.inflate(R.layout.condiment_item, null); 
      } 

      Condiment myObj = CondimentList.get(position); 
     Button CondimentItem = (Button) convertView.findViewById(R.id.condimentItem); 
     CondimentItem.setText("" +myObj.getCondimentDescription()); 


     return convertView; 
    } 

} 
+0

爲什麼不嘗試回收利用者的看法呢? –

+0

看看這個http://www.grokkingandroid.com/statelistdrawables-for-recyclerview-selection/ –

+0

如果我使用回收視圖有什麼不同? – user3463131

回答

0

只需在代碼中添加一點,就可以在您的調味品類模型中提供選定的值,並像這樣使用。

//privateList<Condiment> CondimentList; 
CondimentList = getCondimentDescription(productcode); 
condimentGrid.setAdapter(new condimentlist()); 

public class condimentlist extends BaseAdapter { 

     LayoutInflater mInflater; 

     public condimentlist() { 
      mInflater = LayoutInflater.from(cart_list.this); 
     } 

     @Override 
     public int getCount() { 
      return CondimentList.size(); 
     } 

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

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

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

      if (convertView == null) { 
       convertView = mInflater.inflate(R.layout.condiment_item, null); 
      } 

      final Condiment myObj = CondimentList.get(position); 
      Button CondimentItem = (Button) convertView.findViewById(R.id.condimentItem); 
      CondimentItem.setText("" +myObj.getCondimentDescription()); 
      CondimentItem.setSelected(myObj.isSelected()); 
      CondimentItem.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        myObj.setSelected(!myObj.isSelected()); 
        v.setSelected(myObj.isSelected()); 
       } 
      }); 


     return convertView; 
    } 

}