ListView Why edittext data lost when scroll listView - android

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.

Related

RecyclerView shows previous values entered in an EditText in new rows

I'm creating an android app, in which I'm using recyclerView and the row of recyclerView is having editText.
This is my ReadingAdapter class
public class ReadingAdapter extends RecyclerView.Adapter<ReadingAdapter.ViewHolder> implements AdapterView.OnItemSelectedListener {
Context context;
String valOpenReading, valClosReading, valConsumption;
private List<ReadingData> readingList;
static String[] arrValOpenRead, arrValClosRead, arrValConsumption;
public ReadingAdapter(Context context, List<ReadingData> readingList) {
this.context = context;
this.readingList = readingList;
arrValOpenRead = new String[readingList.size()];
arrValClosRead = new String[readingList.size()];
arrValConsumption = new String[readingList.size()];
}
#Override
public ReadingAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.reading_sheet_layout, parent, false);
return new ReadingAdapter.ViewHolder(view);
}
#Override
public void onBindViewHolder(final ReadingAdapter.ViewHolder holder, final int position) {
ReadingData tempData = readingList.get(position);
holder.pdtName.setText(tempData.pdtName);
holder.keyId.setText("Key "+tempData.keyId);
holder.etClosRead.addTextChangedListener(new TextWatcher() {
boolean ignore = false;
#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) {
if (ignore)
return;
ignore = true;
valOpenReading = holder.etOpenRead.getText().toString();
arrValOpenRead[position] = valOpenReading;
valClosReading = s.toString().equals("") ? "0": s.toString();
arrValClosRead[position] = valClosReading;
if (!valOpenReading.equals("")) {
if (Integer.parseInt(valClosReading) < Integer.parseInt(valOpenReading)) {
Toast.makeText(context, "Check once! closing reading should be more than opening reading!", Toast.LENGTH_LONG).show();
valConsumption = "0";
holder.consumption.setText("");
} else {
valConsumption = (Integer.parseInt(valClosReading) - Integer.parseInt(valOpenReading))+"";
arrValConsumption[position] = valConsumption;
holder.consumption.setText(valConsumption);
}
} else
Toast.makeText(context, "Please fill the opening reading!", Toast.LENGTH_SHORT).show();
ignore = false;
}
});
}
#Override
public int getItemCount() {
return readingList.size();
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView pdtName, keyId, consumption;
EditText etOpenRead, etClosRead;
public ViewHolder(View view) {
super(view);
pdtName = (TextView)view.findViewById(R.id.txt_list_pdt_supp);
keyId = (TextView)view.findViewById(R.id.key_set);
etOpenRead = (EditText)view.findViewById(R.id.open_val_set);
etClosRead = (EditText)view.findViewById(R.id.clos_val_set);
consumption = (TextView)view.findViewById(R.id.consumption_val);
}
}
}
This is my ReadingData.java
public class ReadingData {
String pdtName, keyId, openReading, closReading, consumption;
public ReadingData(String pdtName, String keyId) {
this.pdtName = pdtName;
this.keyId = keyId;
}
}
Here, if I enter value in the starting items of the recyclerView then as I scroll up the items to the bottom of the list, the last item will have that value.
Please ignore the quality of image as we can't upload above of 2MiB of snap.
Here the views are recycled as the list is scrolled. How to prevent the copying values to the other item in the list.
And that Toast is also repeated several times. How to stop this.
update:
By the suggetion of LQ Gioan through the SO question How ListView's recycling mechanism works , I got the logic how ListView actually works with recycling of views.
But I'm not sure whether the recyclerView also works same.
But here in my case, how can I implement this process. pls someone help me here.
RecyclerView reuse views, in fact it only generate the as many as views that is visible on the screen. so it's expected if you can see a value you set for other rows
The solution would be set all attributes of the view that you are changing to default or whatever the row should present from your data set
So put addTextChangedListener insode ViewHolder constructor(you can get position by calling getAdapterPosition()) for better performance and set the editText value inside onBindViewHolder method from your data set
Your Activity Code:
ListView listview = (ListView) findViewById(R.id.list_view);
listview.setItemsCanFocus(true);
Adapter adapter = new Adapter (YourActivity.this, YourArrayList);
listview .setAdapter(adapter);
Adapter class
public class Adapter extends BaseAdapter {
// Declare Variables \\
Context mContext;
LayoutInflater inflater;
Activity act;
String[] temp;
public Adapter(Context context, ArrayList<String> list) {
mContext = context;
inflater = LayoutInflater.from(mContext);
act = (Activity) context;
//-------Temp String Array-------\\
temp = new String[this.count];
for (int i = 0; i < this.count; i++) {
temp[i] = list.get(i);
}
//---------------------------\\
}
public class ViewHolder {
TextView optionTitle;
EditText optionText;
int ref;
}
#Override
public int getCount() {
return list.size;
}
#Override
public Object getItem(int position) {
return temp[position];
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.lv_items_add_ques_options_mcq, null);
holder.optionTitle = (TextView) view.findViewById(R.id.add_ques_opts_count_mcq_tv);
holder.optionText = (EditText) view.findViewById(R.id.add_ques_opts_title_mcq_et);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.ref = position;
holder.optionTitle.setText(getCharForNumber(position) + ":");
holder.optionText.setText(temp[position]);
holder.optionText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
#Override
public void afterTextChanged(Editable arg0) {
temp[holder.ref] = arg0.toString().trim();
}
});
return view;
}
public void getList() {
StaticValues.arrayListOptions = new ArrayList<String>(Arrays.asList(temp));
StaticValues.arrayListOptionsCount = new ArrayList<String>();
for (int i = 0; i < count; i++) {
StaticValues.arrayListOptionsCount.add(String.valueOf(i+1));
Log.e("err_al", StaticValues.arrayListOptions.get(i));
Log.e("err_al", StaticValues.arrayListOptionsCount.get(i));
}
}
private String getCharForNumber(int i) {
char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
if (i > 25) {
return null;
}
return Character.toString(alphabet[i]);
}}

Value of EditText in ListView duplicate itself every fourth row

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

EditText inside Listview and its reclycling issue in android

"Don't use edittext inside a listview" this is what most of the people say.. now I am thinking should have listened to them. I had a problem earlier when I added an EditText inside a ListView and encountered a problem related to click and I also posted a question regarding this(EditText and ListView are not working together) and also found a solution for that in(Focus on EditText in ListView when block descendants (Android)). After having so many obstacles I have been able to implement this,
But now I am having a recycle issue problem, I have an addTextChangedListener for the EditText in the ListView. Most of the people are suggesting to use setOnFocusChangeListener, but that is not an option for me as i have to change the EditText value of one row depending the value entered in the EditText of the other row.
The problem currently I am facing is when I enter the value in the last row of the EditText, the same thing is reflecting in the first row. Can anybody help me to solve this issue.
Here is the code snippet (Sorry for bad english)
public class MyBaseAdapter extends BaseAdapter {
ArrayList < ListData > myList = new ArrayList < > ();
LayoutInflater inflater;
Activity activity;
ListView lvDetail;
TextView total_amount;
TextView spendAmount;
Button proceed;
HashMap < Integer, ListData > selectedRow = new HashMap < > ();
public MyBaseAdapter(Activity activity, ArrayList < ListData > myList, ListView lvDetail) {
this.myList = myList;
this.activity = activity;
this.lvDetail = lvDetail;
inflater = LayoutInflater.from(this.activity);
total_amount = (TextView) activity.findViewById(R.id.total_amount);
spendAmount = (TextView) activity.findViewById(R.id.spendAmount);
proceed = (Button) activity.findViewById(R.id.proceed);
}
#Override
public int getCount() {
return myList.size();
}
#Override
public Object getItem(int position) {
return myList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
public void setItem(int position, ListData data) {
myList.set(position, data);
}#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final MyViewHolder mViewHolder;
final ListData currentListData = (ListData) getItem(position);
if (convertView == null) {
convertView = inflater.inflate(R.layout.layout_list_item, parent, false);
mViewHolder = new MyViewHolder(convertView);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (MyViewHolder) convertView.getTag();
mViewHolder.enterAmount.setText("");
mViewHolder.enterAmount.setVisibility(View.INVISIBLE);
mViewHolder.tvDesc.setText("");
mViewHolder.tvTitle.setText("");
mViewHolder.ivIcon.setImageResource(R.drawable.ic_launcher_1);
}
if (selectedRow.containsKey(position)) {
mViewHolder.enterAmount.setVisibility(View.VISIBLE);
mViewHolder.enterAmount.setText(currentListData.getEditTextValue());
mViewHolder.ivIcon.setImageResource(R.drawable.ic_check_orange);
} else if (currentListData.getEditTextValue().toString().length() == 0) {
mViewHolder.enterAmount.setVisibility(View.INVISIBLE);
mViewHolder.enterAmount.setText(currentListData.getEditTextValue());
mViewHolder.ivIcon.setImageResource(currentListData.getImgResId());
}
mViewHolder.tvTitle.setText(currentListData.getTitle());
mViewHolder.tvDesc.setText(currentListData.getDescription());
mViewHolder.enterAmount.addTextChangedListener(new TextWatcher() {
String enterAmount, enterDescption;#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
enterAmount = s.toString();
enterDescption = mViewHolder.tvDesc.getText().toString().trim();
if ((count == 1 && start == 0) && (before == 1)) {
mViewHolder.enterAmount.setText("");
currentListData.setEditTextValue("");
setItem(position, currentListData);
}
}
#Override
public void afterTextChanged(Editable s) {
String[] separated;
int totalBal = 0;
if ((enterAmount.length() != 0 && enterDescption.length() != 0)) {
separated = enterDescption.split(":");
totalBal = Integer.parseInt(separated[1]);
if ((Integer.parseInt(enterAmount) > totalBal)) {
mViewHolder.enterAmount.setText("");
currentListData.setEditTextValue("");
setItem(position, currentListData);
new AlertDialog.Builder(activity)
.setTitle("Alert")
.setMessage("You do not have sufficient balance")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.setCancelable(false)
.show();
} else if (Integer.parseInt(enterAmount) >= 0) {
System.out.println(position + "positioninside else if enterAmount:" + Integer.parseInt(enterAmount));
currentListData.setEditTextValue(enterAmount);
setItem(position, currentListData);
}
}
}
});
convertView.setOnClickListener(new View.OnClickListener() {#Override
public void onClick(View view) {
int i = selectedRow.size();
if (selectedRow.containsKey(position)) {
selectedRow.remove(position);
mViewHolder.enterAmount.setVisibility(View.INVISIBLE);
mViewHolder.enterAmount.setText("");
mViewHolder.ivIcon.setImageResource(R.drawable.ic_launcher1);
} else if (i < 2) {
ListData currentListData = (ListData) getItem(position);
selectedRow.put(position, currentListData);
mViewHolder.enterAmount.setVisibility(View.VISIBLE);
mViewHolder.ivIcon.setImageResource(R.drawable.ic_check_orange);
} else if (i >= 2) {
Toast.makeText(activity, "Select only two cards", Toast.LENGTH_SHORT).show();
}
}
}
);
return convertView;
}
private static class MyViewHolder {
TextView tvTitle, tvDesc;
ImageView ivIcon;
EditText enterAmount;
public MyViewHolder(View item) {
tvTitle = (TextView) item.findViewById(R.id.tvTitle);
tvDesc = (TextView) item.findViewById(R.id.tvDesc);
ivIcon = (ImageView) item.findViewById(R.id.ivIcon);
enterAmount = (EditText) item.findViewById(R.id.edittext);
}
}
}

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

Get the EditText values from ListView

I'm trying to get the values from the EditTexts in a ListView, but its not working. The values set to my array of 'lista' in the afterTextChanged method are apparently lost. I'm new in android programming, someone can help me? (sorry for bad English)
Heres the code for the getView in my adapter:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
final int posicao = position;
if (convertView == null) {
LayoutInflater inflater = contexto.getLayoutInflater();
convertView = inflater.inflate(R.layout.produtos, null);
holder = new ViewHolder();
holder.texto = (TextView) convertView.findViewById(R.id.txtDescricao);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.chkProduto);
holder.edit = (EditText) convertView.findViewById(R.id.txtValor);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.texto.setText(getItem(position).getTexto());
holder.edit.setText("0");
holder.edit.setTag(new LinhaItemTag(getItem(position), position));
//here a get an exception just setting the focus on edit
holder.edit.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
int posicao = v.getId();
EditText e = ((EditText) v);
if (!e.getText().toString().equals("")) {
if (hasFocus) {
if(e.getText().toString().length() != 0)
lista.get(posicao).setValor(Double.parseDouble(e.getText().toString()));
}
}
}
});
holder.edit.addTextChangedListener( new TextWatcher() {
public void afterTextChanged(Editable s)
{
if(s.length() != 0)
{
lista.get(posicao).setValor(Double.parseDouble(s.toString()));
}
}
#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) {
// TODO Auto-generated method stub
}
});
holder.checkbox.setOnCheckedChangeListener(null);
holder.checkbox.setChecked(getItem(position).Selecionado());
holder.checkbox.setTag(new LinhaItemTag(getItem(position), position));
holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
LinhaItemTag elemento = (LinhaItemTag) buttonView.getTag();
elemento.item.setSelecionado(isChecked);
if (isChecked) {
pegos[elemento.position] = true;
} else {
pegos[elemento.position] = false;
}
boolean checados = true;
for (int i = 0; i < lista.size(); i++) {
if (!pegos[i]) {
checados = false;
break;
}
}
if (checados) {
for(int i = 0; i < lista.size(); i++)
{
total += lista.get(i).getValor();
}
Toast.makeText(contexto, "Compra finalizada - Valor Total: " + total, Toast.LENGTH_LONG).show();
}
}
});
return convertView;
}
}
I managed to get values from the EditTexts looking at this post: how to retrieve value from all EditText in ListView
Bu now, i faced with other problem: when i roll my list down, the values from the EditTexts are lost.
This is like my code looks now:
public class MeuAdapter extends ArrayAdapter<LinhaItem> {
private final List<LinhaItem> lista;
private final Activity contexto;
private final boolean[] pegos;
private final double[] valores;
double total;
public MeuAdapter(Activity contexto, List<LinhaItem> lista) {
super(contexto, 0, lista);
this.contexto = contexto;
this.lista = lista;
pegos = new boolean[lista.size()];
valores = new double[lista.size()];
for (int i = 0; i < lista.size(); i++) {
pegos[i] = false;
valores[i] = 0;
}
total = 0;
}
public class ViewHolder
{
protected TextView texto;
protected CheckBox checkbox;
protected EditText edit;
}
public class LinhaItemTag {
LinhaItem item;
int position;
LinhaItemTag(LinhaItem item, int position) {
this.item = item;
this.position = position;
}
}
private class MeuTextWatcher implements TextWatcher {
private View v;
public MeuTextWatcher(View v)
{
this.v = v;
}
#Override
public void afterTextChanged(Editable e) {
String s = e.toString();
LinhaItemTag lTag = (LinhaItemTag) v.getTag();
if(s.length() != 0)
{
valores[lTag.position] = Double.parseDouble(s);
}
}
#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) {
// TODO Auto-generated method stub
}
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = contexto.getLayoutInflater();
convertView = inflater.inflate(R.layout.produtos, null);
holder = new ViewHolder();
holder.texto = (TextView) convertView.findViewById(R.id.txtDescricao);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.chkProduto);
holder.edit = (EditText) convertView.findViewById(R.id.txtValor);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.texto.setText(getItem(position).getTexto());
holder.edit.setText("");
holder.edit.setTag(new LinhaItemTag(getItem(position), position));
holder.edit.addTextChangedListener(new MeuTextWatcher(holder.edit));
holder.checkbox.setOnCheckedChangeListener(null);
holder.checkbox.setChecked(getItem(position).Selecionado());
holder.checkbox.setTag(new LinhaItemTag(getItem(position), position));
holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
LinhaItemTag elemento = (LinhaItemTag) buttonView.getTag();
elemento.item.setSelecionado(isChecked);
if (isChecked) {
pegos[elemento.position] = true;
} else {
pegos[elemento.position] = false;
}
boolean checados = true;
for (int i = 0; i < lista.size(); i++) {
if (!pegos[i]) {
checados = false;
break;
}
}
String vlrs = "";
if (checados) {
for(int i = 0; i < lista.size(); i++)
{
total += valores[i];
}
Toast.makeText(contexto, "Total da compra: " + total, Toast.LENGTH_LONG).show();
}
}
});
return convertView;
}
}
I change the line:
holder.edit.setText("");
to:
//the valores array is where i store the values in the afterTextChanged method in the TextWatcher class implementation.
holder.edit.setText(**Double.toString(valores[position])**);
But this not working as i expect, for example, if i set a value to the EditText of the first row of the ListView, this value, after i roll down the list, is setted to EditText in the third row of the list...
To get the values from the EditText in your ListView, you could do that in your activity. You must need the values on some view click or something. So whenever that happens, just write the following code
View view=listView.getChildAt(position);
EditText editText=view.findViewById(R.id.editText);
String string=editText.getText().toString();
Here, the above code will give you the text of the EditText that is present in the position position of your ListView. It seems that you need the sum of the of the values of the EditTexts when all are checked right? You can use something like this then
for(int i=0;i<items.size();i++){
View view=listView.getChildAt(i);
EditText editText=view.findViewById(R.id.editText);
String string=editText.getText().toString();
if(!string.equals(""))
total+=Double.parseDouble(string);
}
change in adapter class getView() methods look like.
#Override
public View getView(final int position, View view, ViewGroup parent) {
TextView txt_id, txt_courny_symbol, txt_member_view;
EditText edit_member_amount;
final int posicao = position;
Log.v("ConvertView", String.valueOf(posicao));
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.list_member_layout, null);
txt_id = (TextView) view.findViewById(R.id.txt_id);
txt_courny_symbol = (TextView) view.findViewById(R.id.txt_courny_symbol);
edit_member_amount = (EditText) view.findViewById(R.id.edit_member_amount);
edit_member_amount.setTag(new LinhaItemTag(getItem(posicao), posicao));
edit_member_amount.addTextChangedListener(new MeuTextWatcher(edit_member_amount));
if (al_contact.size() != 0) {
MemberClass memberClass = al_contact.get(position);
txt_id.setText(memberClass.getName());
txt_courny_symbol.setText(sym);
}
return view;
}
erase the our ViewHolder class
hi you can use the code below to do this, listview cannot save the controllers state on the row, so you need to use settag and gettag of controls.
for source: https://dl.dropbox.com/u/68130108/ListViewEditItem.rar
Losing the values from EditTexts can be annoying as far. I have found this solution while working.
In your android manifest, pick the activity which your list view is in. Then put this code-block there.
android:windowSoftInputMode="adjustPan"
Then go to your ListView, and navigate to the xml layout and put this block there.
android:descendantFocusability="beforeDescendants"

Categories

Resources