2016-04-12 30 views
-1

私は、ユーザがハイライト/チェックしてから「削除」ボタンを押すことができるアイテムでリストビューを作成しようとしています。それらの項目をリストから削除します。リストビュー内のアイテムの選択と削除

問題は、私はそれがまったく動作するように思えます、私のSparseBooleanArrayは、選択された項目を正しく返すようです。しかし、私は新しい色で背景を強調しようとします。私はscroolのときにそれがリスト内のランダムな項目を選択していると私は再びすべての項目がチェック/選択されているすべてのランダムをscroolしようとすると動作するようだ。

m_orders = new ArrayList<Products>(); 
    this.m_adapter = new OrderAdapter(this, R.layout.orderrow, m_orders); 
    lv = (ListView)findViewById(R.id.listView); 
    lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
    lv.setItemsCanFocus(false); 
    lv.setAdapter(m_adapter); 

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      SparseBooleanArray array = lv.getCheckedItemPositions(); 
      int len = lv.getCount(); 
      for (int i = 0; i < len; i++){ 
       if (array.get(i)) { 
        Products item = m_orders.get(i); 
        view.setBackgroundColor(Color.BLUE); 
       }else{ 
        view.setBackgroundColor(Color.WHITE); 
       } 
      } 
     } 
    }); 

OrderAdapter

private class OrderAdapter extends ArrayAdapter<Products> { 

    private ArrayList<Products> items; 

    public OrderAdapter(Context context, int textViewResourceId, ArrayList<Products> items) { 
     super(context, textViewResourceId, items); 
     this.items = items; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View v = convertView; 
     if (v == null) { 
      LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = vi.inflate(R.layout.orderrow, null); 
     } 
     Products o = items.get(position); 
     if (o != null) { 
      ImageView iv = (ImageView) v.findViewById(R.id.icon); 
      TextView tt = (TextView) v.findViewById(R.id.toptext); 
      TextView bt = (TextView) v.findViewById(R.id.bottomtext); 
      LinearLayout or = (LinearLayout) v.findViewById(R.id.orderrow); 

      if (tt != null) { 
       tt.setText(""+o.get_name()); 
      } 
      if(bt != null){ 
       bt.setText(""+ new BigDecimal(o.get_price()).movePointLeft(2)); 
       //bt.setText(""+o.get_price()); 
      } 
      if(iv != null){ 
       if(o.get_orderStatus()){ 
        iv.setImageResource(R.drawable.checkmark); 
       }else{ 
        // dont do anything as the item has not been paid yet 
       } 
      } 
     } 
     return v; 
    } 
} 
+0

メインリストのレイアウトを表示します。次に、背景色を設定します。 LinearLayout mainLayout =(LinearLayout)view.findViewById(レイアウトID)。 if(mainLayout!= null) mainLayout.setBackgroundColor(Color。BLUE); –

+0

アダプタのgetViewコード –

+0

が私のOrderAdapterを他のコードの下に追加しました –

答えて

0

条件ならば、アダプタのGetViewメソッドで削除します。

if (v == null) 
関連する問題