old get view change data in other listview - android

hello every one i am really struck from last 3 days in a very tiny place i have 3 custom listview in my application what does my application is when i check value in one listvew and then scoll other list view the data of first edit box automatically pass to other and i dont want that . . my first adapter
public class Doneyesadapter extends BaseAdapter {
private List<ApplicationInfo> appInfoList;
private LayoutInflater mInflater;
private PackageManager pm;
ArrayList<Boolean> positionArray;
private Context ctx;
int[] visiblePosArray;
private volatile int positionCheck;
String[] strItecode=null;
String[] strItem;
String[] strQuantity,text;
int temp;
private int editingPosition = 0;
ArrayList<String> listItem;
ArrayList<String> listAddress;
ArrayList<String> quantity;
ArrayList<String> idlist;
public static ArrayList<String> ItemID_list=new ArrayList<String>();
public static ArrayList<String> StockCode_list=new ArrayList<String>();
public static ArrayList<String> Description_list=new ArrayList<String>();
public static ArrayList<String> Quantity_list=new ArrayList<String>();
Context mContext;
ArrayList<String> dates;
//constructor
public Doneyesadapter(Context mContext, ArrayList<String> listItem, ArrayList<String> listAddress,ArrayList<String> quantity
,ArrayList<String> idlist ) {
this.mContext = mContext;
this.listItem = listItem;
this.listAddress = listAddress;
this.quantity = quantity;
this.idlist = idlist;
text= new String[idlist.size()];
pm = mContext.getPackageManager();
positionArray = new ArrayList<Boolean>(listItem.size());
for(int i =0;i<listItem.size();i++){
positionArray.add(false);
}
//this.dates=date;
}
private TextWatcher watcher = new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
text[editingPosition] = s.toString();
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
public void afterTextChanged(Editable s) { }
};
public int getCount() {
return listItem.size();
}
public void cleardata(){
listAddress.clear();
listItem.clear();
}
public Object getItem(int arg0) {
return null;
}
public String getName(int pos){
int name =pos;
String ll = listAddress.get(name);
return ll;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View arg1, ViewGroup viewGroup) {
final ViewHolder1 holder;
temp=position;
if (arg1 == null) {
holder = new ViewHolder1();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
arg1 = inflater.inflate(R.layout.stocklistcustom, viewGroup, false);
holder.checkbox1 = (CheckBox) arg1.findViewById(R.id.checkbox1);
holder.Des = (TextView) arg1.findViewById(R.id.description1);
holder.stockid = (TextView) arg1.findViewById(R.id.stockid1);
holder.done = (EditText) arg1.findViewById(R.id.quantity1);
holder.stockid.setText(listItem.get(position));
holder.Des.setText(listAddress.get(position));
holder.done.setText(quantity.get(position));
arg1.setTag(holder);
} else {
holder = (ViewHolder1) arg1.getTag();
}
holder.checkbox1.setOnCheckedChangeListener(null);
holder.checkbox1.setFocusable(false);
// holder.checkbox1.setImageDrawable(appInfoList.get(position).loadIcon(pm));
holder.checkbox1.setChecked(positionArray.get(position));
// holder.checkbox1.setText(appInfoList.get(position).loadLabel(pm));
holder.checkbox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int i = 0;
// Toast.makeText(mContext, "here is commom"+buttonView, Toast.LENGTH_SHORT).show();
if (holder.checkbox1.isChecked()) {
quantity.set(position,holder.done.getText().toString());
positionArray.set(position, true);
String id, qu, des, stc;
id = idlist.get(position);
qu = quantity.get(position);
des = listAddress.get(position);
stc = listItem.get(position);
ItemID_list.add(i, id);
StockCode_list.add(i, stc);
Description_list.add(des);
Quantity_list.add(i,text[temp]);
quantity.set(position,holder.done.getText().toString());
holder.done.setText(quantity.get(position));
i++;
} else {
positionArray.set(position, false);
ItemID_list.remove(i);
StockCode_list.remove(i);
Description_list.remove(i);
Quantity_list.remove(i);
holder.done.setText(quantity.get(position));
i--;
}
}
});
holder.done.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
text[temp] = s.toString();
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
text[temp] = s.toString();
}
});
holder.done.setText(text[temp]);
//date.setText(dates);
holder.stockid.setText(listItem.get(position));
holder.Des.setText(listAddress.get(position));
holder.done.setText(quantity.get(position));
return arg1;
}
private class ViewHolder1{
CheckBox checkbox1;
TextView Des;
TextView stockid;
EditText done;
}
}
my second adapter
public Doneyesadapter2(Context mContext, ArrayList<String> listItem, ArrayList<String> listAddress,ArrayList<String> quantity
,ArrayList<String> idlist ) {
this.mContext = mContext;
this.listItem = listItem;
this.listAddress = listAddress;
this.quantitys = quantity;
this.idlist = idlist;
text= new String[idlist.size()];
pm = mContext.getPackageManager();
positionArray = new ArrayList<Boolean>(listItem.size());
for(int i =0;i<listItem.size();i++){
positionArray.add(false);
}
//this.dates=date;
}
private TextWatcher watcher = new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
text[editingPosition] = s.toString();
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
public void afterTextChanged(Editable s) { }
};
public int getCount() {
return listItem.size();
}
public void cleardata(){
listAddress.clear();
listItem.clear();
}
public Object getItem(int arg0) {
return null;
}
public String getName(int pos){
int name =pos;
String ll = listAddress.get(name);
return ll;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View arg11, ViewGroup viewGroup) {
final ViewHolders2 holder;
int a=3;
Log.e("second getview called", String.valueOf(a));
temp=position;
if (arg11 == null) {
holder = new ViewHolders2();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
arg11 = inflater.inflate(R.layout.stocklistcustom2, viewGroup, false);
holder.checkbox1 = (CheckBox) arg11.findViewById(R.id.checkbox2);
holder.Des = (TextView) arg11.findViewById(R.id.description2);
holder.stockid = (TextView) arg11.findViewById(R.id.stockid2);
holder.done = (EditText) arg11.findViewById(R.id.quantity2);
holder.stockid.setText(listItem.get(position));
holder.Des.setText(listAddress.get(position));
holder.done.setText(quantitys.get(position));
arg11.setTag(holder);
}
else {
holder = (ViewHolders2) arg11.getTag();
}
holder.checkbox1.setOnCheckedChangeListener(null);
holder.checkbox1.setFocusable(false);
// holder.checkbox1.setImageDrawable(appInfoList.get(position).loadIcon(pm));
holder.checkbox1.setChecked(positionArray.get(position));
holder.checkbox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int i = 0;
if (holder.checkbox1.isChecked()) {
quantitys.set(position,holder.done.getText().toString());
positionArray.set(position, true);
String id, qu, des, stc;
id = idlist.get(position);
qu = quantitys.get(position);
des = listAddress.get(position);
stc = listItem.get(position);
ItemID_list2.add(i, id);
StockCode_list2.add(i, stc);
Description_list2.add(des);
//Quantity_list2.add(, holder.done.getText().toString());
Quantity_list2.add(i,holder.done.getText().toString());
i++;
general=i;
} else {
positionArray.set(position, false);
ItemID_list2.remove(i);
StockCode_list2.remove(i);
Description_list2.remove(i);
Quantity_list2.remove(i);
i--;
general=i;
}
}
});
holder.done.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
text[temp] = s.toString();
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
text[temp] = s.toString();
}
});
holder.done.setText(text[temp]);
holder.stockid.setText(listItem.get(position));
holder.Des.setText(listAddress.get(position));
holder.done.setText(quantitys.get(position));
//date.setText(dates);
return arg11;
}
private class ViewHolders2{
CheckBox checkbox1;
TextView Des;
TextView stockid;
EditText done;
}
}

Related

Edittext in Listview when scroll so lost data [duplicate]

This question already has answers here:
EditText loses content on scroll in ListView
(11 answers)
Closed 5 years ago.
I try make listview with edittext for in. But my apdater not work as expected. When i scroll down then scroll up, so values in first editext is lost.
My dapter:
public class ArrayAdapterListProduct extends BaseAdapter implements Filterable {
private ArrayList<ProductItem> listProductItems = new ArrayList<>();
private ArrayList<ProductItem> mOriginalValues = new ArrayList<>();
private Context context = null;
private Activity a = null;
static class ViewHolder {
TextView text;
TextView timestamp;
ImageView icon;
EditText caption;
ProgressBar progress;
int position;
}
public ArrayAdapterListProduct(Activity a, Context context, ArrayList<ProductItem> productItems) {
this.listProductItems = productItems;
this.mOriginalValues = productItems;
this.context = context;
this.a = a;
}
public void swapItem(ArrayList<ProductItem> productItems) {
this.listProductItems = productItems;
notifyDataSetChanged();
}
#Override
public int getCount() {
return listProductItems.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(this.context);
view = inflater.inflate(R.layout.layout_lv_item, null);
holder.caption = (EditText) view.findViewById(R.id.earnpoit_listview_item_number);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.caption = (EditText) view.findViewById(R.id.earnpoit_listview_item_number);
view.setTag(holder);
if (position % 2 != 0) {
view.setBackgroundColor(parent.getResources().getColor(R.color.earnpoint_color_bg_item_lv));
} else {
view.setBackgroundColor(parent.getResources().getColor(R.color.WHITE));
}
final ProductItem productItem = listProductItems.get(position);
TextView txt_name = (TextView) view.findViewById(R.id.earnpoit_listview_item_productname);
TextView txt_exist = (TextView) view.findViewById(R.id.earnpoit_listview_item_exist);
txt_name.setText(productItem.getName());
txt_exist.setText(productItem.getQuantity() + "");
holder.caption.setText(productItem.getNumber_choose() + "");
holder.caption.setId(position);
holder.caption.addTextChangedListener(new MyTextWatcher(holder.caption));
holder.caption.setFilters(new InputFilter[]{new InputFilterMinMax(0, Integer.parseInt(txt_exist.getText().toString()))});
return view;
}
private class MyTextWatcher implements TextWatcher {
private EditText editText;
private MyTextWatcher(EditText editText) {
this.editText = editText;
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
final int position = this.editText.getId();
final EditText Caption = (EditText) this.editText;
listProductItems.get(position).setNumber_choose(Integer.parseInt(Caption.getText().toString()));
TextView txt_point_to_product = (TextView) a.findViewById(R.id.sellout_txt_point_in_order);
Double total_point = 0.0;
for (int i = 0; i < listProductItems.size(); i++) {
ProductItem productItem1 = listProductItems.get(i);
if (productItem1.getNumber_choose() > 0) {
total_point = total_point + productItem1.getNumber_choose()*productItem1.getPoint();
}
}
txt_point_to_product.setText(total_point + "");
}
#Override
public void afterTextChanged(Editable s) {
}
}
#Override
public Filter getFilter() {
Filter filter = new Filter() {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
ArrayList<ProductItem> filterArray = new ArrayList<ProductItem>();
if (mOriginalValues == null) {
mOriginalValues = new ArrayList<>(listProductItems);
}
if (constraint == null || constraint.length() == 0) {
results.count = mOriginalValues.size();
results.values = mOriginalValues;
} else {
constraint = constraint.toString().toLowerCase();
for (int i = 0; i < mOriginalValues.size(); i++) {
String data = mOriginalValues.get(i).getName();
if (data.toLowerCase().startsWith(constraint.toString())) {
filterArray.add(mOriginalValues.get(i));
}
}
// set the Filtered result to return
results.count = filterArray.size();
results.values = filterArray;
}
return results;
}
#Override
protected void publishResults(CharSequence constraint, FilterResults results) {
listProductItems = (ArrayList<ProductItem>) results.values;
notifyDataSetChanged();
}
};
return filter;
}
}
Please help me. Thanks you!
Please refer below
holder.edtitext.setTag(position); // add this line
holder.edtitext.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
int pos = (Integer) holder.edtitext.getTag();
ProductItem productItem1 = listProductItems.get(pos);
// do your work
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});

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

ListView with two edit text on each row

How to store and retrieve editText from a listview that have 2 editText on each row?
With this code, when I write something to one editText, it copy the value to all others.
public class CheckoutAdapter extends BaseAdapter{
private Context mContext;
private List<JsonObject> mObjs;
private ViewHolder holder;
private SessionManager session;
private float Value = 0;
private int tax = 0;
private float Total = 0;
private int viewID = 0;
private EnhancedListView listview;
private ImageLoader imageLoader = ImageLoader.getInstance();
static DisplayImageOptions options;
private ArrayList<String> arrayFirstName = new ArrayList<String>();
private ArrayList<String> arrayLastName = new ArrayList<String>();
public CheckoutAdapter(Context context, List<JsonObject> objs, EnhancedListView mListView) {
// TODO Auto-generated constructor stub
this.mContext = context;
this.mObjs = objs;
this.listview = mListView;
for (int i = 0; i < mObjs.size(); i++) {
arrayFirstName.add("");
arrayLastName.add("");
}
}
//Classe ViewHolder
public final class ViewHolder {
public TextView ticketValue;
public TextView ticketItem;
public TextView ticketOpcao;
public TextView ticketTax;
public TextView ticketTitle;
public TextView ticketDate;
public TextView ticketFullName;
public EditText ticketFirstName;
public EditText ticketLastName;
public ImageView ticketImageThumb;
int position;
}
public void remove(int position) {
mObjs.remove(position);
}
public void insert(int position, JsonObject item) {
mObjs.add(position, item);
}
#Override
//Contar a quntidade de linhas.
public int getCount() {
// TODO Auto-generated method stub
return mObjs.size();
}
#Override
//Pegar o item conforme a posição.
public Object getItem(int position) {
// TODO Auto-generated method stub
return mObjs.get(position);
}
#Override
//ID do item (refere-se a posição em que ele se encontra).
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi=convertView;
//Se não houver nenhuma View, uma nova view é criada.
if(convertView==null)
{
vi = LayoutInflater.from(mContext).inflate(R.layout.row_checkout, null);
Typeface font = Typeface.createFromAsset(mContext.getAssets(), "fonts/Oswald-Bold.otf");
final View origView = vi;
vi.findViewById(R.id.action_delete).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listview.delete(((ViewHolder)origView.getTag()).position);
}
});
holder = new ViewHolder();
holder.ticketItem = (TextView)vi.findViewById(R.id.tvItem);
holder.ticketOpcao = (TextView)vi.findViewById(R.id.tvOpcao);
holder.ticketTax = (TextView)vi.findViewById(R.id.tvTaxa);
holder.ticketValue = (TextView)vi.findViewById(R.id.tvValue);
holder.ticketValue.setTypeface(font);
holder.ticketTitle = (TextView)vi.findViewById(R.id.tvTitle);
holder.ticketTitle.setTypeface(font);
holder.ticketDate = (TextView)vi.findViewById(R.id.tvDate);
holder.ticketDate.setTypeface(font);
holder.ticketFullName = (TextView)vi.findViewById(R.id.tvFullName);
holder.ticketFullName.setTypeface(font);
holder.ticketFirstName = (EditText)vi.findViewById(R.id.etFirstName);
holder.ticketLastName = (EditText)vi.findViewById(R.id.etLastName);
holder.ticketImageThumb = (ImageView)vi.findViewById(R.id.imgFlyerSub);
holder.position = position;
holder.ticketFirstName.addTextChangedListener(FirstNameChangedListener(position));
holder.ticketLastName.addTextChangedListener(LastNameChangedListener(position));
holder.ticketFirstName.setOnFocusChangeListener(FirstNameFocusChangeListener(position));
holder.ticketLastName.setOnFocusChangeListener(LastNameFocusChangeListener(position));
vi.setTag(holder);
//Caso já exista uma view o objeto holder recebe o getTag da View vi.
} else {
holder = (ViewHolder)vi.getTag();
}
holder.ticketItem.setText(mObjs.get(position).getAsJsonObject().get(Constants.JSON.JSON_ITEM).getAsString());
holder.ticketOpcao.setText(mObjs.get(position).getAsJsonObject().get(Constants.JSON.JSON_OPCAO).getAsString());
holder.ticketTax.setText("Taxa de serviço:" + mObjs.get(position).getAsJsonObject().get(Constants.JSON.JSON_TAX).getAsString() + "%");
holder.ticketValue.setText("R$" + String.valueOf(mObjs.get(position).getAsJsonObject().get(Constants.JSON.JSON_VALUE).getAsInt()) + ",00");
holder.ticketDate.setText(mObjs.get(position).getAsJsonObject().get(Constants.KEY.KEY_EVENTDATE).getAsString());
holder.ticketTitle.setText(mObjs.get(position).getAsJsonObject().get(Constants.JSON.JSON_EVENTTITLE).getAsString());
holder.ticketFirstName.setText(arrayFirstName.get(holder.position));
holder.ticketLastName.setText(arrayLastName.get(holder.position));
options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.ic_stub)
.showImageForEmptyUri(R.drawable.ico_load_imagem)
.cacheInMemory().cacheOnDisc()
.displayer(new RoundedBitmapDisplayer(5))
.build();
imageLoader.displayImage(mObjs.get(position).getAsJsonObject().get("thumbPrincipal").getAsString(), holder.ticketImageThumb, options);
return vi;
}
private View.OnFocusChangeListener LastNameFocusChangeListener(final int position) {
return new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus){
viewID = holder.position;
}
}
};
}
private View.OnFocusChangeListener FirstNameFocusChangeListener(final int position) {
return new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus){
viewID = holder.position;
}
}
};
}
private TextWatcher FirstNameChangedListener(final int position) {
return 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) {
}
#Override
public void afterTextChanged(Editable s) {
String itemName = s.toString();
arrayFirstName.add(viewID, itemName);
}
};
}
private TextWatcher LastNameChangedListener(final int position) {
return 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) {
}
#Override
public void afterTextChanged(Editable s) {
String itemName = s.toString();
arrayLastName.add(viewID, itemName);
}
};
}
}
EDIT: Removed setId and JSON.
Your holder is a member of your class, it's wrong.
Remove it from this place. Every line of your listview has its own holder.
This holder is "sticked" to the view by the setTag method.
In listeners that used holder, you have to get the holder from the view that send the event.
For this, the following code should work:
private View.OnFocusChangeListener FirstNameFocusChangeListener(final int position) {
return new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus){
ViewHolder holder = (ViewHolder ) v.getTag();
viewID = holder.position;
}
}
};
}

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