2011-11-13 30 views
0

我有一個自定義的數組適配器列表視圖,該視圖允許用戶與一些加號和減號按鈕,我實現Android的列表視圖,觀察室將只更新一次

現在的問題是,改變一個變量上下加/減按鈕將改變視圖ONCE,但在此之後它不會更新,直到視圖以不同的方式刷新。例如在外部操作中使用listview.notifydatasetchanged()

,當它更新,它表明了加/減按鈕WERE的點擊次數和更新變量,因爲對數據的更新設置後立即將正確的變量視圖

爲什麼不是個人查看自己更新?

private class MyListAdapter extends ArrayAdapter<Item> { 

    private ArrayList<Item> items; 
    TextView quantity; 
    int i; 

    public MyListAdapter(Context context, int textViewResourceId, ArrayList<Item> 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.list_scanneditems, null); 
     } 

      final Item allItems = items.get(position); 




     //declare all parts of the cell 
     TextView product = (TextView)v.findViewById(R.id.productName); 
     quantity = (TextView)v.findViewById(R.id.listNum); 
     TextView category = (TextView)v.findViewById(R.id.categoryName); 
     TextView price = (TextView)v.findViewById(R.id.productCost); 

     product.setText(allItems.product); 
     category.setText(allItems.category); 
     price.setText("$" + allItems.price); 
     quantity.setText(String.valueOf(allItems.quantity)); //might want to do ceiling and floor functions here, IF weight not present 

     //set properties within on click listeners 
     //items.set(position, allItems.quantity); 

     ImageView selectLeft = (ImageView)v.findViewById(R.id.selectleft_list); 
     ImageView selectRight = (ImageView)v.findViewById(R.id.selectright_list); 

     CheckBox itemCheckBox = (CheckBox)v.findViewById(R.id.itemCheckBox);   

     itemCheckBox.setChecked(selectallListener); 

     itemCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){ 

      @Override 
      public void onCheckedChanged(CompoundButton buttonView, 
        boolean isChecked) { 
       // TODO Auto-generated method stub 
       if(isChecked==false && selectallListener==true) 
       { 
        selectallListener=false; 

        CheckBox selectAll = (CheckBox)findViewById(R.id.selectAll); 
        selectAll.setChecked(false); 
       } 
      } 

     }); 


     selectLeft.setOnClickListener(new OnClickListener(){ 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

       if(allItems.quantity > 0) //i greater than 0, decrement -1 
       { 
        allItems.setQuantity(allItems.quantity-1); 
        quantity.setText(String.valueOf(allItems.quantity)); 
       } 
       else //i less or equal to 0 
       { 

        allItems.setQuantity(0); 
        quantity.setText(String.valueOf(allItems.quantity)); 

        //turn icon into an X here an if it is X delete the item 

       } 

      }//onclick 

     });//selectleft listener 

     selectRight.setOnClickListener(new OnClickListener(){ 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

       allItems.setQuantity(allItems.quantity+1); 
       quantity.setText(String.valueOf(allItems.quantity)); 

      }//onclick 

     });//selectRight listener 

     //do I need viewholder here? 

     //set listeners 


     return v; 
    } 

回答

1

數量變量可能與發生點擊的當前視圖無關。現在它始終是在上次getView調用中創建/處理的TextView。

我認爲必須像allItems變量。 in getView函數

final TextView quantity = ...

+0

你建議我做什麼? – CQM

+0

而不是類變量使用本地/函數變量。就像allItems(多麼令人誤解的名字)變量一樣。 ... //聲明單元格的所有部分 TextView product =(TextView)v.findViewById(R.id.productName); final TextView quantity =(TextView)v.findViewById(R.id.listNum); ... – Deucalion

+0

這工作,謝謝 – CQM