2013-04-18 66 views
0

當顯示在視圖中時,如果我滾動通過第一個EditText框,則我在第一個和最後一個EditText框中都會顯示我輸入最後一個EditText的值。當將值保存到數據庫中時,它只保存最後的EditText值,但是如預期的那樣。ListView中的第一個和最後一個EditText顯示相同的值

是什麼導致了這種行爲?這裏是我的完整代碼:

package com.example.expensemanager1; 

import java.util.List; 

import android.app.Activity; 
import android.content.Context; 
import android.graphics.Color; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.View.OnFocusChangeListener; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.CheckBox; 
import android.widget.CompoundButton; 
import android.widget.EditText; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
import android.widget.Toast; 



public class InteractiveArrayAdapter extends ArrayAdapter<Model> { 
    private final List<Model> list; 
    private final Activity context; 
    final int i; 
    LayoutInflater mInflater; 
    public InteractiveArrayAdapter(Activity context, List<Model> list) { 
     super(context, R.layout.spared_list_row, list); 
     this.context = context; 
     this.list = list; 
     i=list.size(); 
     notifyDataSetChanged(); 
    } 




    static class ViewHolder { 
     protected TextView membname; 
     protected EditText membamount; 
    } 



    public class MyTextWatcher implements TextWatcher 
    { 
     private EditText editText; 
     Model elemente; 
     public MyTextWatcher(EditText editText, Model m) 
     { 
      this.editText = editText; 
      this.elemente = m; 
     } 

     @Override 
     public void afterTextChanged(Editable s) { 
      // TODO Auto-generated method stub 

      Log.d("TEST", "In afterTextChanged"); 

      editText.setFocusable(true); 

      String ssss= s.toString(); 

      if(editText.getText().toString().length()>0) 
      list.get((Integer)editText.getTag()).setamount(Double.parseDouble(s.toString())); 
      else 
       list.get((Integer)editText.getTag()).setamount(0); 



      double sum=0.0; 
      for (int j=0; j<i ; j++) 
      { 
       Model m1= list.get(j);      

       sum += m1.getamount();   

      } 

      Log.d("TEST", "sum" +sum); 

      double balance = New_spared_list.getTextView(); 
      Log.d("TEST", "balance = "+ balance); 
      sum = balance - sum; 
      New_spared_list.setX(sum); 

      Log.d("TEST", "sum" +sum); 



     } 

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

     } 

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

     } 
    } 



    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View view = null; 
     ViewHolder viewHolder = new ViewHolder(); 


     if (convertView == null) { 
      final Model elemente = list.get(position); 
      LayoutInflater inflator = context.getLayoutInflater(); 
      convertView = inflator.inflate(R.layout.spared_list_row, null); 


      viewHolder.membname = (TextView) convertView.findViewById(R.id.membersTextView2); 
      viewHolder.membamount = (EditText) convertView.findViewById(R.id.amount); 
      final ViewHolder holder = viewHolder; 
      viewHolder.membamount.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        //do something 
        LinearLayout parent = (LinearLayout) v.getParent();    
        EditText membamount = (EditText) parent.findViewById(R.id.amount); 

        membamount.setFocusable(true); 
       membamount.setFocusableInTouchMode(true); 
       membamount.requestFocus(); 

       } 

      }); 




      viewHolder.membamount.addTextChangedListener(new MyTextWatcher(viewHolder.membamount, (Model) viewHolder.membamount.getTag())); 



      convertView.setTag(viewHolder); 
      convertView.setTag(R.id.membersTextView2, viewHolder.membname); 
      convertView.setTag(R.id.amount, viewHolder.membamount); 
      // viewHolder.membamount.setTag(list.get(position)); 
      // viewHolder.membamount.setHint(String.valueOf((list.get(position).getamount()))); 

     } else { 

      viewHolder = (ViewHolder) convertView.getTag(); 
      ((ViewHolder) convertView.getTag()).membamount.setTag(list.get(position)); 
     } 

     viewHolder.membamount.setTag(position); 
     if(position % 2 == 0){ 
      convertView.setBackgroundColor(Color.rgb(231, 247, 253)); 
      } 

     // viewHolder.membamount.setTag(list.get(position)); 


     viewHolder.membamount.setHint(String.valueOf(list.get(position).getamount())); 
     viewHolder.membname.setText(list.get(position).getName()); 


     return convertView; 
    } 

} 
+0

這是listview的常見行爲,它重用您的意見。所以你的edittext不是一個新的,它是一樣的。你應該設置你的edittext的文本以避免這種情況。 – yahya

回答

0

在getView()的結尾處,您還應該更改membamount中的文本;否則,如果它重新使用convertview,它會保留以前的文本。

viewHolder.membamount.setText(list.get(position).getAmount()); 
+0

但我不包括,因爲,我想價值0.0在提示,這將很容易爲用戶改變,如果我包括這setText的金額,那麼他們將是可見的,用戶會感到不便 – user2295455

+0

好吧;那麼你應該:'viewHolder.membamount.setText(「」)' – etienne

+0

如果我添加viewHolder.membamount.setText(「」),那麼在滾動後listview中的edittext總是空的。 – user2295455

相關問題