列表视图edittext值在滚动时更改

时间:2014-02-18 04:36:58

标签: android listview android-edittext adapter

我有一个列表视图,每行都有一些edittext。在第一次加载时,我已为每个edittexts分配值并且可以编辑。我可以编辑该edittext中的值。我的问题是,在更改行的位置后,即:滚动后,我在edittexts中获取初始值,而不是编辑值。我在过去的6个小时里正在研究这个问题。任何人请帮助我。

这是我的适配器类

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;
}

}

3 个答案:

答案 0 :(得分:0)

每次在listview的getview中扩展列表项布局时,您需要使用Viewholder类并初始化edittext。

 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

答案 1 :(得分: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) { }
    });
}

答案 2 :(得分: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();
 }

试一试.100%的工作代码