Value of EditText in ListView duplicate itself every fourth row - android

I have ListView that has a an EditText in every row.When i enter the value in the EditText it duplicate itself in every fourth row in the ListView . How can I prevent that.
public class ItemsAdapter extends ArrayAdapter<Items>{
Double Price;
Double Quantity;
String _Quantity;
private int resource;
private LayoutInflater inflater;
private Context context;
Items items;
public ItemsAdapter(Context ctx, int resourceId, List<Items> objects){
super( ctx, resourceId, objects );
resource = resourceId;
inflater = LayoutInflater.from( ctx );
context=ctx;
}
#Override
public View getView ( int position, View convertView, ViewGroup parent ) {
ItemViewHolder viewHolder;
items = getItem( position );
if (convertView == null) {
viewHolder=new ItemViewHolder();
convertView = ( RelativeLayout ) inflater.inflate( resource, null );
viewHolder.txtQuantity =(EditText)convertView.findViewById(R.id.txtQuantity);
viewHolder.lblItemsName=(TextView) convertView.findViewById(R.id.lblItemName);
viewHolder.lblPrice = (TextView) convertView.findViewById(R.id.lblPrice);
} else {
viewHolder = (ItemViewHolder) convertView.getTag();
}
viewHolder.lblItemsName.setText(items.getItemName());
viewHolder.lblPrice.setText(items.getPrice().toString());
return convertView;
}
public class ItemViewHolder {
public EditText txtQuantity ;
public TextView lblItemsName;
public TextView lblPrice;
}
}`

When you start enter the quantity in each cell of ListView, you should save the quantity to the items
After that, you load the quantity from item
... your above code
}
viewHolder.txtQuantity.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
#Override
public void afterTextChanged(Editable s) {
items.setQuantity(s); // set quantity for each item when you enter quantity
}
});
viewHolder.lblItemsName.setText(items.getItemName());
viewHolder.lblPrice.setText(items.getPrice().toString());
viewHolder.txtQuantity.setText(items.getQuantity()); // load quantity for each item
...
Hope this help

The duplication happens because views are recycled in Listview when you scroll. To avoid this you should reset the text in Edittext in getView() method.
If you want to retain the text in EditText when you come back to the view again you should do something like Phan Văn Linh suggested.

I figure it out
public ItemAdapter(Context context, int textViewResourceId,
ArrayList<Cart> productList) {
super(context, textViewResourceId, productList);
this.productList = new ArrayList<Cart>();
this.productList.addAll(productList);
}
#Override
public int getCount() {
return this.productList.size();
};
#Override
public View getView(final int position, View view, ViewGroup parent) {
DecimalFormat df = new DecimalFormat("0.00##");
// productList= DBAdapter.getCartList();
final Cart product = productList.get(position);
if (view == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.list_cart, null);
EditText quantity = (EditText) view.findViewById(R.id.txtQuantity);
//attach the TextWatcher listener to the EditText
quantity.addTextChangedListener(new MyTextWatcher(view));
}
TextView ItemNumber=(TextView)view.findViewById(R.id.itemNumber);
ItemNumber.setVisibility(View.GONE);
EditText quantity = (EditText) view.findViewById(R.id.txtQuantity);
quantity.setTag(product);
if(product.getQuantity() != 0){
quantity.setText(String.valueOf(product.getQuantity()));
}
else {
quantity.setText("1");
}
TextView itemNumber = (TextView) view.findViewById(R.id.itemNumber);
itemNumber.setText(product.getItemNumber());
TextView ItemName = (TextView) view.findViewById(R.id.lblItemName);
ItemName.setText(product.getItemName());
TextView price = (TextView) view.findViewById(R.id.lblPrice);
price.setText( df.format(product.getPrice()));
TextView ext = (TextView) view.findViewById(R.id.lblTotal);
if(product.getQuantity() != 0){
ext.setText("$" + df.format(product.getTotal()));
}
else {
ext.setText("");
}
return view;
}
}
private class MyTextWatcher implements TextWatcher {
private View view;
private MyTextWatcher(View view) {
this.view = view;
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//do nothing
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
//do nothing
}
public void afterTextChanged(Editable s) {
DecimalFormat df = new DecimalFormat("0.00##");
String qtyString = s.toString().trim();
int quantity = qtyString.equals("") ? 0:Integer.valueOf(qtyString);
EditText qtyView = (EditText) view.findViewById(R.id.txtQuantity);
Cart product = (Cart) qtyView.getTag();
if(product.getQuantity() != quantity){
Double currPrice = product.getTotal();
Double extPrice = quantity * product.getPrice();
Double priceDiff = Double.valueOf(df.format(extPrice - currPrice));
product.setQuantity(quantity);
product.setTotal(extPrice);
TextView ext = (TextView) view.findViewById(R.id.lblTotal);
if(product.getQuantity() != 0){
ext.setText("$" + df.format(product.getTotal()));
}
else {
ext.setText("");
}
if(product.getQuantity() != 0){
qtyView.setText(String.valueOf(product.getQuantity()));
}
else {
qtyView.setText("");
}
orderTotal += priceDiff;
TextView cartTotal = (TextView) findViewById(R.id.cartTotal);
cartTotal.setText(df.format(orderTotal));
}
return;
}
}

Related

Listview Freeze with Textviews and Edit Text in one Row

Hi I am using freeze concept in listview,
Row contains Textview and Edit texts,If i change any Edit text it should update the other column Textview is it possible? Currenly i am using https://github.com/InQBarna/TableFixHeaders Android library
Here is my adapter class:
When i change the age in edittext i need to change height based on age input.
public class FamilyNexusAdapter extends BaseTableAdapter {
private final int[] widths = { 100, 100, 100 };
private final float density;
private Vector<ProductMasterBO> items;
public FamilyNexusAdapter(Context context, Vector<ProductMasterBO> items) {
density = context.getResources().getDisplayMetrics().density;
this.items = items;
}
#Override
public int getRowCount() {
return this.items.size();
}
#Override
public int getColumnCount() {
return 3;
}
#Override
public View getView(final int row, int column, View convertView,
ViewGroup parent) {
String headerString[] = { "column 1", "column 2", "column 3",
};
if (row == -1) {
convertView = getLayoutInflater().inflate(
R.layout.item_table_header_first, parent, false);
((TextView) convertView.findViewById(R.id.product_name))
.setText(headerString[column + 1]);
} else{
user= (UserMaster) items.get(row);
if(column==-1){
txt=user.getuserName();
((TextView) convertView.findViewById(R.id.user_name))
.setText(headerString[column + 1]);
}else if(column==1){
txt=user.getage()+"";
EditText age = (EditText) convertView
.findViewById(R.id.age);
age.setText(txt);
age .addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
String ageString= s.toString();
if (!qty.equals("")) {
}}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
//i want to handle when i am trying to change age height should be change
}
});
}else if(column==2){
txt=user.getheight()+"";
((TextView) convertView.findViewById(R.id.user_name))
.setText(txt);
}
return convertView;
}
Use a viewHolder pattern and set the tag property of the EditText to refer the TextView.

How to take the new values edited in the list to send it to server database.

Here is my adapter code where on edit field edited am calculating the 2 other fields and displaying through TextWatcher. There are 2 issues here.
1. On scroll the values are changing to default values
2. How can I take the new values updated on click of a button
Please help...thanks.
public class ItemListAdapter extends BaseAdapter {
private ArrayList<Itemlist> mProductList,medicineList;
private LayoutInflater mInflater;
String newEditValu[];
Itemlist curProduct;
Itemlist list;
DataTranrfer df;
public ItemListAdapter(ArrayList<Itemlist> list, LayoutInflater inflater) {
mProductList = list;
mInflater = inflater;
}
#Override
public int getCount() {
return mProductList.size();
}
#Override
public Object getItem(int position) {
return mProductList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewItem item;
final int pos=position;
medicineList=new ArrayList<Itemlist>();
if (convertView == null) {
convertView = mInflater.inflate(R.layout.activity_medicine_list,null);
item = new ViewItem();
item.medicineTitle = (TextView) convertView.findViewById(R.id.tvMedicineName);
item.medcineQuantity =(EditText)convertView.findViewById(R.id.etQuantity);
item.itemDaily=(TextView)convertView.findViewById(R.id.tvDailyText);
item.itemNdays=(TextView)convertView.findViewById(R.id.tvNodays);
item.itemTotal = (TextView)convertView.findViewById(R.id.tvCost);
convertView.setTag(item);
ImageButton b2 = (ImageButton) convertView.findViewById(R.id.thumbnail);
b2.setTag(position);
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
int pos = (int)arg0.getTag();
mProductList.remove(pos);
ItemListAdapter.this.notifyDataSetChanged();
}
});
convertView.setTag(item);
} else {
item = (ViewItem) convertView.getTag();
}
curProduct = mProductList.get(position);
item.ref=position;
item.medicineTitle.setText(curProduct.getmedicineName());
item.itemDaily.setText("Daily: " + curProduct.getMedicineDaily());
item.itemNdays.setText("Days: " + curProduct.medicineNoDays);
item.itemTotal.setText("Rs." + curProduct.getMedicineTotal());
item.medcineQuantity.setText("" + curProduct.getMedicineQuantity());
ItemListAdapter.this.notifyDataSetChanged();
item.medcineQuantity.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//ItemListAdapter.this.notifyDataSetChanged();
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
System.out.print("edit response" + s.toString());
Itemlist curProduct = mProductList.get(position);
if (item.medcineQuantity.getText().length() >= 0) {
double dItemcost = Double.parseDouble(curProduct.getMedicineCost());
double dItemDaily = Double.parseDouble(curProduct.getMedicineDaily());
double dItemQuantity = Double.parseDouble(item.medcineQuantity.getText().toString());
//double dItemQuantity = Double.parseDouble(myList.get(pos));
double dItemDays = (dItemQuantity / dItemDaily);
double dItemTotal = (dItemQuantity * dItemcost);
item.medcineQuantity.setText("" +myList.get(pos));
item.itemNdays.setText("Days: " + dItemDays);
item.itemTotal.setText("Rs." + dItemTotal);
list=new Itemlist();
list.setmedicineName(curProduct.getmedicineName());
list.setMedicineDaily(curProduct.getMedicineDaily());
list.setMedicineQuantity(dItemQuantity);
list.setMedicineCost("" + dItemcost);
list.setMedicineTotal(dItemTotal);
medicineList.add(list);
for(int i=0;i<medicineList.size();i++)
{
System.out.println("medicine list"+medicineList.toString());
System.out.println("medicine total at"+i+":"+medicineList.get(i).getMedicineTotal());
}
}
}
});
return convertView;
}
private class ViewItem {
TextView medicineTitle,itemDaily,itemNdays,itemTotal;
EditText medcineQuantity;
int ref;
}
}

Setting values into edittext in a list view

I am using a custom list view which contains two Buttons Yes or No. When I clicked on No button, a layout containing an edit text will be displayed. There are more than 15 items in the list view. When I tried to type and save the value in the edittext of the 1st item using TextWatcher(), then the value is save for both 5th and 1st position ie, the same value is saving in both 1st and 5th position.
The code I am using is:
holder.subQuestionAnswer.addTextChangedListener( new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
questionsModel.get(position).getQuestion_subquestion().get(0).setAnswer(holder.subQuestionAnswer.getText()
.toString());
Log.e("value", "no_active<>"+position+"<>"+questionsModel.get(position).getQuestion_subquestion().get(0).getAnswer().toString());
}
});
}
How can i avoid this?
It is because of recycling of view. Please check the following adapter. Use the logic according to your needs.
public class MultiSelectionProductAdapter extends ArrayAdapter<ProductListBean> {
private Context context;
private ArrayList<ProductListBean> productList;
private ArrayList<ProductListBean> mOriginalValues;
private LayoutInflater li;
public MultiSelectionProductAdapter(Context ctx, int simpleListItem1,
ArrayList<ProductListBean> data) {
super(ctx, simpleListItem1, data);
this.context = ctx;
this.productList = data;
li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return productList.size();
}
#Override
public ProductListBean getItem(int position) {
return productList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = li.inflate(R.layout.component_inventory_prod_list, null);
holder = new ViewHolder();
holder.tvProductName = (TextView) convertView.findViewById(R.id.tvProductName);
holder.tvProdShortCode = (TextView) convertView.findViewById(R.id.tvShortName);
holder.tvLastOrderedQty = (TextView) convertView.findViewById(R.id.tvLastOrderedQty);
holder.etQty = (EditText) convertView.findViewById(R.id.etQty);
holder.parentRL = (LinearLayout) convertView.findViewById(R.id.parentRL);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final ProductListBean tempBean = productList.get(position);
holder.etQty.setId(position);
final String productName = tempBean.getProductName();
final String productCode = tempBean.getProductCode();
final String productShortCode = tempBean.getProductShortCode();
if (mSelectedProd != null && mSelectedProd.contains(productCode)) {
final int indexOfProd = mSelectedProd.indexOf(productCode);
holder.etQty.setText(mSelectedProducts.get(indexOfProd).getEnteredQty());
}
holder.tvProductName.setText(productName);
holder.tvProdShortCode.setText(productShortCode);
holder.tvLastOrderedQty.setText(tempBean.getLastOrderedQty());
holder.etQty.setText(tempBean.getEnteredQty());
holder.parentRL.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
holder.etQty.requestFocus();
}
});
holder.etQty.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
final int pos = holder.etQty.getId();
final String qty = holder.etQty.getText().toString();
if (qty.length() > 0) {
if (String.valueOf(qty.charAt(0)).equals("0")) {
holder.etQty.setError("Invalid Quantity");
holder.etQty.setText("");
} else {
productList.get(pos).setEnteredQty(holder.etQty.getText().toString());
}
} else {
productList.get(pos).setEnteredQty("");
}
}
});
return convertView;
}
static class ViewHolder {
TextView tvProductName, tvProdShortCode, tvLastOrderedQty;
EditText etQty;
LinearLayout parentRL;
}

ListView Why edittext data lost when scroll listView

I follow some code in few page, but it not working. Here is my code of pageAdapter:
public class ViewHolder {
int ref;
TextView txtTimeToEat;
LinearLayout lnlDishListView, lnlDishToGetpos;
ImageView imgDish;
TextView txtDishName;
TextView txtDishWhen;
TextView txtActiGoal;
TextView txtActiActual;
EditText edtDishAtual;
}
public SelectDishActualAdapter(Context context, List<DishEntity> listDishInTime) {
mContext = context;
Current = new String[listDishInTime.size()];
this.listDishInTime = listDishInTime;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return listDishInTime.size();
}
#Override
public Object getItem(int position) {
return listDishInTime.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView( int position, View convertView, ViewGroup parent) {
View vi = convertView;
final int pos = position;
if (vi == null) {
vi = inflater.inflate(R.layout.update_actual_dish_custom, null);
holder = new ViewHolder();
holder.lnlDishListView = (LinearLayout) vi.findViewById(R.id.lnlDishListView);
holder.lnlDishToGetpos = (LinearLayout) vi.findViewById(R.id.lnlDishToGetPos);
holder.txtTimeToEat = (TextView) vi.findViewById(R.id.txtEatTime);
holder.imgDish = (ImageView) vi.findViewById(R.id.imgDishActual);
holder.txtDishName = (TextView) vi.findViewById(R.id.txtDishActualName);
holder.txtActiGoal = (TextView) vi.findViewById(R.id.txtManualGram);
holder.txtActiActual = (TextView) vi.findViewById(R.id.txtActualGram);
holder.edtDishAtual = (EditText) vi.findViewById(R.id.edtActualGram);
holder.edtDishAtual.setId(pos);
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
///// id = 989898,989897,989896 is to decrale footer,It is mean Breakfast,Lunch and Dinner
if (listDishInTime.get(position).getDishId() == 989898) {
holder.txtTimeToEat.setText(mContext.getResources().getString(R.string.sang));
holder.txtTimeToEat.requestLayout();
holder.txtTimeToEat.getLayoutParams().height = 40;
holder.lnlDishListView.requestLayout();
holder.lnlDishListView.getLayoutParams().height = 0;
} else if (listDishInTime.get(position).getDishId() == 989897) {
holder.txtTimeToEat.setText(mContext.getResources().getString(R.string.trua));
holder.txtTimeToEat.requestLayout();
holder.txtTimeToEat.getLayoutParams().height = 40;
holder.lnlDishListView.requestLayout();
holder.lnlDishListView.getLayoutParams().height = 0;
} else if (listDishInTime.get(position).getDishId() == 989896) {
holder.txtTimeToEat.setText(mContext.getResources().getString(R.string.chieu));
holder.txtTimeToEat.requestLayout();
holder.txtTimeToEat.getLayoutParams().height = 40;
holder.lnlDishListView.requestLayout();
holder.lnlDishListView.getLayoutParams().height = 0;
} else {
// Otherwise fill to listview with custom data
String temp = listDishInTime.get(position).getImageUrl();
final int imgID = mContext.getResources().getIdentifier(temp, "drawable", mContext.getPackageName());
holder.txtDishName.setText(listDishInTime.get(position).getName());
holder.imgDish.setImageResource(imgID);
int size = holder.imgDish.getLayoutParams().width;
holder.txtTimeToEat.requestLayout();
holder.txtTimeToEat.getLayoutParams().height = 0;
holder.lnlDishListView.requestLayout();
holder.lnlDishListView.getLayoutParams().height = size;
holder.edtDishAtual.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
holder.ref = pos;
Current[holder.ref] = s.toString(); // String[] Current with size = new size
myList.put(pos, s.toString().trim()); //mylist is hashmap
}
});
holder.edtDishAtual.setText(myList.get(pos));
}
return vi;
}
But when I scroll, editext still lose content data :(. please help me
ListView recycle it's children to optimise use of the memory. When your EditText item is moved out of the screen it gets used by another item from your list.
This short video from Udacity Android course explains nicely how it works. It takes just 1:40min.
If you want your EditText to remain the text you have to store it yourself within item data and then restore it when your item is shown again.

Listview edittext value changing on scrolling

I have a listview and in each row there are some edittext. On loading for the firsttime I have assigned values to every edittexts and are editable. I can edit the values in that edittext. My problem is that after changing the position of that rows ie: after scrolling I am getting the initial values in that edittexts, not the editted values. I am working on this for the past 6 hours. Any one please help me.
Here is my adapter class
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;
}
}
You have to update s.toString() value in your array.
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) { }
});
}
You need to use Viewholder class and initialize edittext every time while inflating list item layout in getview of listview.
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;
}
See these answers also
Android :EditText loses content on scroll in ListView?
EditText Content Changes on ListView Scroll
I resolved this issue by implementing following code.
put following code in getview method of adapter class
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();
}
try it.100 % working code

Categories

Resources