2012-08-06 77 views
0

我使用此代碼獲取列表視圖中edittext的值。ListView中的EditText?

 View view = listview.getChildAt(position); 
    EditText text = (EditText)view.findViewById(R.id.myEditBox); 
    String contents = text.getText().toString(); 

但問題是,當我滾動列表視圖,位置的變化,我得到一個不同的result.ie如果我滾動,在第五位置的元素得到的頂部和它的位置變得0.Is那有什麼補救辦法嗎?

回答

0

如果你還沒有這樣做,你將不得不爲你的列表視圖創建一個適配器,並且當你設置你的視圖時,在你的適配器上添加一個textwatcher並在文本更改爲你的活動或片段時生成報告。

在您的適配器:

edittext.addTextChangedListener(new TextWatcher() { 

       @Override 
       public void onTextChanged(CharSequence s, int start, int before, int count) { 
        // TODO Auto-generated method stub 

       } 

       @Override 
       public void beforeTextChanged(CharSequence s, int start, int count, 
         int after) { 
        // TODO Auto-generated method stub 

       } 

       @Override 
       public void afterTextChanged(Editable s) { 
        ((MyActivity) getContext()).textChanged(s.toString()); 

       } 
      }); 

您的活動

public void textChanged(String s){ 

     //do somthing with the string 
} 

這樣,你不必猜測位置FO您編輯文本

+0

但是我怎麼確定哪些行的編輯文本值我越來越。 – orange 2012-08-06 22:37:02

+0

如果您實現了適配器getView(int position,View convertView,ViewGroup parent)方法,您還可以獲取當前工作的項目的位置,以便您可以輕鬆地將新參數傳遞給活動的textChanged方法 – 2012-08-06 22:40:28

+0

謝謝,有效。 – orange 2012-08-06 23:06:05