2014-02-18 107 views
0

我有一個列表視圖,每行都有一些edittext。在第一次加載時,我已將值分配給每個edittext並且是可編輯的。我可以編輯該edittext中的值。我的問題是,改變了那些行的位置後,即:滾動後,我得到的是edittexts中的初始值,而不是編輯值。過去6個小時我正在爲此工作。任何人都請幫助我。Listview edittext值滾動

這裏是我的適配器類

public class ListAdapter_baradmin extends BaseAdapter{ 

Context ctx; 
LayoutInflater lInflater; 
public ArrayList myItems = new ArrayList(); 


public static String[] str_Id   = new String[datalength]; 
public static String[] str_Idoriginal = new String[datalength]; 
public static String[] str_Desc   = new String[datalength]; 
public static String[] str_UOM   = new String[datalength]; 
public static String[] str_Parlevel  = new String[datalength]; 
public static String[] str_Openingstock = new String[datalength]; 
public static String[] str_Reg   = new String[datalength]; 
public static String[] str_Intertransfer= new String[datalength]; 
public static String[] str_Closingstock = new String[datalength]; 
public static String[] str_Remark  = new String[datalength]; 

SeparatedListAdapter separatedListAdapter; 

ArrayList<String> Data_id   = new ArrayList<String>(); 
ArrayList<String> Data_name   = new ArrayList<String>(); 
ArrayList<String> Data_parlevel  = new ArrayList<String>(); 
ArrayList<String> Data_uom   = new ArrayList<String>(); 

ArrayList<String> Data_Openingstock = new ArrayList<String>(); 
ArrayList<String> Data_Reg   = new ArrayList<String>(); 
ArrayList<String> Data_Intertransfer= new ArrayList<String>(); 
ArrayList<String> Data_Closingstock = new ArrayList<String>(); 
ArrayList<String> Data_Remark  = new ArrayList<String>(); 


public ListAdapter_baradmin(Context context 
     ,ArrayList<String> Items_id 
     ,ArrayList<String> Items_desc 
     ,ArrayList<String> Items_perunitcost 
     ,ArrayList<String> Items_uom 
     ,ArrayList<String> Items_idoriginal 
     ,ArrayList<String> Items_os 
     ,ArrayList<String> Items_intertransfer 
     ,ArrayList<String> Items_cs 
     ,ArrayList<String> Items_remark 
     ,ArrayList<String> Items_reg) { 

    ctx = context; 

    Data_id   .addAll(Items_id); 
    Data_name  .addAll(Items_desc); 
    Data_parlevel .addAll(Items_perunitcost); 
    Data_uom  .addAll(Items_uom); 

    Data_Openingstock .addAll(Items_os); 
    Data_Reg   .addAll(Items_reg); 
    Data_Intertransfer .addAll(Items_intertransfer); 
    Data_Closingstock .addAll(Items_cs); 
    Data_Remark   .addAll(Items_remark); 


    for(int i=0;i<Items_id.size();i++){ 

     str_Id[i]   = Items_id.get(i); 
     str_Idoriginal[i] = Items_idoriginal.get(i); 
     str_Desc[i]   = Items_desc.get(i); 
     str_UOM[i]   = Items_uom.get(i); 
     str_Parlevel[i]  = Items_perunitcost.get(i); 

     str_Openingstock[i] = Items_os.get(i); 
     str_Reg[i]   = Items_reg.get(i); 
     str_Intertransfer[i] = Items_intertransfer.get(i); 
     str_Closingstock[i] = Items_cs.get(i); 
     str_Remark[i]  = Items_remark.get(i); 

     //System.out.println("str_Idoriginal "+str_Idoriginal[i]); 

    } 


    notifyDataSetChanged(); 



} 

@Override 
public int getCount() { 
    return Data_id.size(); 
} 

@Override 
public Object getItem(int position) { 
    return position; 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 



@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 

    lInflater = (LayoutInflater) ctx 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    final ViewHolder holder; 

    if (convertView == null) { 
     holder = new ViewHolder(); 
     //convertView = (View)lInflater.inflate(R.layout.baradmin_row, null); 
     convertView = (View) lInflater.inflate(R.layout.baradmin_row, parent, false); 

     //holder = new ViewHolder(); 

     holder.editText_id      = (EditText) convertView.findViewById(R.id.edittext_slno_baradmin); 
     holder.editText_desc     = (EditText) convertView.findViewById(R.id.edittext_desc_baradmin); 
     holder.editText_uom      = (EditText) convertView.findViewById(R.id.edittext_uom_baradmin); 
     holder.editText_parlevel    = (EditText) convertView.findViewById(R.id.edittext_parlevel_baradmin); 
     holder.edittext_openingstock_baradmin = (EditText) convertView.findViewById(R.id.edittext_openingstock_baradmin); 
     holder.edittext_reg_baradmin   = (EditText) convertView.findViewById(R.id.edittext_reg_baradmin); 
     holder.edittext_intertransfer_baradmin = (EditText) convertView.findViewById(R.id.edittext_intertransfer_baradmin); 
     holder.edittext_closingstock_baradmin = (EditText) convertView.findViewById(R.id.edittext_closingstock_baradmin); 
     holder.edittext_remark_baradmin   = (EditText) convertView.findViewById(R.id.edittext_remark_baradmin); 


     convertView.setTag(holder); 
    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    try { 

     holder.editText_id   .setText(position+1+""); 
     holder.editText_desc  .setText(Data_name.get(position)); 
     holder.editText_uom   .setText(Data_uom.get(position)); 
     holder.editText_parlevel .setText(Data_parlevel.get(position)); 

     holder.edittext_reg_baradmin   .setText(Data_Reg.get(position)); 
     holder.edittext_openingstock_baradmin .setText(Data_Openingstock.get(position)); 
     holder.edittext_intertransfer_baradmin .setText(Data_Intertransfer.get(position)); 
     holder.edittext_closingstock_baradmin .setText(Data_Closingstock.get(position)); 
     holder.edittext_remark_baradmin   .setText(Data_Remark.get(position)); 

     setNameTextChangeListener(holder); 

     holder.edittext_openingstock_baradmin.setOnFocusChangeListener(new OnFocusChangeListener() { 
      public void onFocusChange(View v, boolean hasFocus) { 
       if (!hasFocus){ 


        str_Openingstock[position] = holder.edittext_openingstock_baradmin.getText().toString(); 

       } 
      } 
     }); 




    } catch (Exception e) { 
     // TODO: handle exception 
    } 


    return convertView; 
} 
class ViewHolder { 
    EditText editText_id, editText_desc, editText_uom, editText_parlevel, 
    edittext_openingstock_baradmin, edittext_reg_baradmin, edittext_intertransfer_baradmin, 
    edittext_closingstock_baradmin, edittext_remark_baradmin; 
} 

private void setNameTextChangeListener(final ViewHolder holder) { 
    holder.edittext_openingstock_baradmin.addTextChangedListener(new TextWatcher() { 

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

      System.out.println("s.toString() "+s.toString()); 
      //holder.edittext_openingstock_baradmin.setText(s.toString()); 
     } 

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

     @Override 
     public void afterTextChanged(Editable s) { } 
    }); 
} 

class ListItem { 
    String caption; 
} 

}

+1

爲什麼ü採取單個陣列的每一個項目? – Richa

+1

,因爲您設置了來自array的編輯文本值。所以,當您更改edit-ext值時,您可以更新數組。 – Yugesh

回答

0

您需要更新您的數組中s.toString()值。

Data_Openingstock.set(position, s.toString());

setNameTextChangeListener(Holder holder, int position); 

private void setNameTextChangeListener(final ViewHolder holder, final int position) { 
    holder.edittext_openingstock_baradmin.addTextChangedListener(new TextWatcher() { 

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

    //do null check first before converting to string 

if(s.length() > 0) 
{ 
System.out.println("s.toString() "+s.toString()); 
Data_Openingstock.set(position, s.toString()); 
} 

     } 

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

     @Override 
     public void afterTextChanged(Editable s) { } 
    }); 
} 
0

您需要使用Viewholder類,每一次初始化的EditText而在列表視圖的getview充氣列表項的佈局。

if (convertView == null) { 

      holder = new ViewHolder(); 

      LayoutInflater vi = (LayoutInflater)  
      getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = vi.inflate(R.layout.element_in_game, null); 

      holder.scoreToUpdate = (EditText) convertView 
        .findViewById(R.id.elementUpdateScore); 


      convertView.setTag(holder); 

     } else { 

      holder = (ViewHolder) convertView.getTag(); 

     } 
     initScoresToUpdateEditTexts(holder.scoreToUpdate, hint); 
     holder.scoreToUpdate.setText(scoresToUpdate[tmp_position]); 
     holder.scoreToUpdate.addTextChangedListener(new TextWatcher() { 

       @Override 
       public void onTextChanged(CharSequence s, int start, 
         int before, int count) { 
        scoresToUpdate[tmp_position] = s.toString(); 
       } 

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

       } 

       @Override 
       public void afterTextChanged(Editable s) { 

       } 
      }); 

     return convertView; 
    } 

看到這些答案也

Android :EditText loses content on scroll in ListView?

EditText Content Changes on ListView Scroll

0

我通過執行以下代碼解決了這個問題。 放按照適配器類的getview方法代碼

viewHolder.invAmt.setTag(position);//set tag 

holder.edittext_openingstock_baradmin.addTextChangedListener(new TextWatcher() { 
    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, int after)   { 

    } 

@Override 
    public void onTextChanged(CharSequence s, int start, int before, int count) { 
     int pos = (Integer) holder.edittext_openingstock_baradmin.getTag(); 
     scoresToUpdate[pos] = s.toString(); 
    } 

    @Override 
    public void afterTextChanged(Editable s) { 

    } 
}); 
} else { 
     viewHolder = (ViewHolder) rowView.getTag(); 
} 

嘗試it.100%的工作代碼