2015-11-25 38 views
0

當TextWatcher觸發EditText中的更改時,我遇到了問題以便在ListView中獲取ListView的位置。當TextWatcher觸發時獲取列表視圖中元素的位置

每個CardView都有兩個EditText和兩個Spinners。當我對產品名稱(左側的EditText)和spinners的值進行一些更改時,我的代碼正確地獲取了CardView在列表中的位置。

The list described. my Toast should print "posicao = 0", because I've just changed the first element. However, it prints the last iterated value of the variable posicao. In this case, the last element.

然而,當我通過鍵入更改價格的價值,我的代碼無法得到它的位置。 的CardView的位置在該線得到...

  • 最終詮釋posicao =的Integer.parseInt(consumableInfo.getName()),其consumableInfo是在我的Adapater列出的類,和consumableInfo。 getName獲取卡的名稱,該名稱等於卡的位置。像 「0」, 「1」, 「2」 ...

這是因爲每次我打電話......

  • holder.mAutoCompleteTextView.setOnItemClickListener爲AutoCompleteEditText左側;
  • holder.mDivideConsumableSpinner.setOnItemSelectedListener for each spinner;

...我的代碼再次通過BindData迭代。然而,當我打電話......

  • holder.mConsumablePriceTextView.addTextChangedListener(priceTextWatcher)爲的EditText上的權利;

...我的代碼不會再次迭代。

我試圖找到另一種方式來獲得它的位置,但我遇到了問題。也許強制通過posicao獲取值,或創建一個實現TextWatcher的customTextWatcher並獲取consumableInfo作爲參數。

public class ConsumableAdapter extends RecyclerView.Adapter<ConsumableAdapter.ConsumableViewHolder> { 
/*...some code ommited...*/ 
int posicaoGlobal; 

public ConsumableAdapter(Context context, List<ConsumableInfo> contactList) {...}/*...some code ommited...*/ 
} 

public class ConsumableViewHolder extends RecyclerView.ViewHolder { 
    public AutoCompleteTextView mAutoCompleteTextView; 
    public Spinner mDivideConsumableSpinner; 
    public Spinner mUnitsConsumableSpinner; 
    public EditText mConsumablePriceTextView; 

    public ConsumableViewHolder(View itemView) { 
     /*...*/ 
    } 


    public void bindData(ConsumableInfo consumableInfo, ConsumableViewHolder holder, Context context) { 

     final int posicao = Integer.parseInt(consumableInfo.getName()); 
     posicaoGlobal = posicao; 

     ArrayAdapter adapter = new ArrayAdapter(mContext, android.R.layout.select_dialog_item, 
       Constants.CONSUMABLE_CONSTANTS); 
     holder.mAutoCompleteTextView.setAdapter(adapter); 

     /* position is updated withmAutoCompleteTextView.setOnItemClickListener */ 
     holder.mAutoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener({ 
        updateTotalPrice(posicao); 
        /*...*/ 
     }); 


        /*position is NOT updated with addTextChangedListener*/ 
    holder.mConsumablePriceTextView.addTextChangedListener(priceTextWatcher); 



        /*position is updated with setOnItemSelectedListener in both Spinners*/ 
     holder.mDivideConsumableSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
        updateTotalPrice(posicao); 
        /*...*/ 
     }); 
     //product units 
     holder.mUnitsConsumableSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
        updateTotalPrice(posicao); 
        /*...*/ 
     }); 

    } 

    private void updateTotalPrice(int posicao) { 

     /*...*/ 
     mTotalPrice = getTotalPrice(BotequimActivity.mProductList, mPercent); 
     BotequimActivity.mTotalPriceTextView.setText(getTotalPriceString()); 
     FormatStringAndText.setPriceTextViewSize(mTotalPrice, BotequimActivity.mTotalPriceTextView); 
    } 

} 

private void updateTotalPrice(int posicao, String priceString) { 
    /*...*/ 
    BotequimActivity.mTotalPriceTextView.setText(getTotalPriceString()); 
    FormatStringAndText.setPriceTextViewSize(mTotalPrice, BotequimActivity.mTotalPriceTextView); 
} 

private final TextWatcher priceTextWatcher = new TextWatcher() { 

    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
     if (count != 0) { 
      if (FormatStringAndText.isNumeric(s.toString())) { 
       mProductPriceBeforeChange = Double.parseDouble(s.toString()); 
      } 
     } 
    } 

    @Override 
    public void onTextChanged(CharSequence s, int start, int before, int count) { 


     Toast.makeText(mContext, "posicao =" + posicaoGlobal, Toast.LENGTH_SHORT).show(); 
     if (s.toString().length() == 0) { 
      updateTotalPrice(posicaoGlobal, "0.00"); 
     } else { 
      if (!isAutoCompleteClicked) { 
       if (FormatStringAndText.isNumeric(s.toString())) { 
        mProductPriceAfterChange = Double.parseDouble(s.toString());BotequimActivity.mTotalPriceTextView.setText(getTotalPriceString()); 
        //     FormatStringAndText.setPriceTextViewSize(mTotalPrice, BotequimActivity.mTotalPriceTextView); 
        updateTotalPrice(posicaoGlobal, s.toString()); 
       } else { 
       } 

      } else { 
       isAutoCompleteClicked = false; 
      } 
     } 
    } 

    @Override 
    public void afterTextChanged(Editable s) { 


    } 
}; 


public Double getTotalPrice(ArrayList<Product> productList, Double percent) { 
    mTotalPrice = 0; 
    for (Product product : productList) { 
     mTotalPrice = mTotalPrice + percent * (product.getUnits() * (product.getDoublePrice())/product.getDividedBy()); 
    } 
    return mTotalPrice; 
} 

}

+0

謝謝,@EGHM! –

回答

0

已解決。我不得不打電話priceTextWatcher作爲它的構造函數的參數,就像AdapterView.OnItemSelectedListener()一樣。正確的是:

holder.mConsumablePriceTextView.addTextChangedListener(new TextWatcher() { 

       @Override 
       public void onTextChanged(CharSequence s, int start, int before, int count) { 
//... 
} 
//...methods inside here 
} 
0

你需要在創建TextWatcher保存位置。我將與內子類這樣做:

// this is an inner class so it will have an implicit reference to 
    // the adapter (ConsumableAdapter.this) 
    public class PriceTextWatcher implements TextWatcher { 

     private int mPos; 

     public PriceTextWatcher(int position) { 
      super(); 
      mPos = position; 
     } 

     // now add your TextWatcher implementation here and use mPos for position 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
      // ... 
     } 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      // ... 
     } 

     @Override 
     public void afterTextChanged(Editable s) {} 

    } 

現在,當您創建TextWatcher可以初始化位置:

 holder.mConsumablePriceTextView.addTextChangedListener(new PriceTextWatcher(posicao)); 

您將有多個TextWatchers,而不是你目前擁有的單最終TextWatcher,但這是取得位置價值所需的權衡。

相關問題