Listview edittext value changing on scrolling - android

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

Related

the listview items are not displayed in the order of entry

I try to implement this Android TextHighlighter in my application but the listview displays the data in a disordered way. That is, the entries do not appear in the list according to the order established in the code. All the elements of my listview are displayed but not in the desired order. I want to display my items in the exact order in which I entered them, ie the oldest entry is the first item in the list.
thats is my code:
public class MainActivity extends AppCompatActivity {
private static final int DEFAULT_BG_COLOR = Color.YELLOW;
private static final int DEFAULT_FG_COLOR = Color.RED;
private TextHighlighter textHighlighter = new TextHighlighter()
.setBackgroundColor(DEFAULT_BG_COLOR)
.setForegroundColor(DEFAULT_FG_COLOR);
private TextHighlighter.Matcher matcher =
TextHighlighter.CASE_INSENSITIVE_MATCHER;
private ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
QuoteListAdapter adapter = new QuoteListAdapter(QUOTES, textHighlighter);
listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);
EditText searchText = (EditText) findViewById(R.id.search_text);
searchText.addTextChangedListener(searchTextWatcher);
EditText fgColorText = (EditText) findViewById(R.id.fg_color_text);
fgColorText.addTextChangedListener(fgColorTextWatcher);
EditText bgColorText = (EditText) findViewById(R.id.bg_color_text);
bgColorText.addTextChangedListener(bgColorTextWatcher);
CheckBox boldText = (CheckBox) findViewById(R.id.bold_text);
boldText.setOnCheckedChangeListener(boldChangedListener);
CheckBox italicText = (CheckBox) findViewById(R.id.italic_text);
italicText.setOnCheckedChangeListener(italicChangedListener);
}
private TextWatcher searchTextWatcher = 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) {
String text = s.toString();
if (text.isEmpty()) {
QuoteListAdapter adapter = new QuoteListAdapter(QUOTES, textHighlighter);
listView.setAdapter(adapter);
} else {
HashMap<String, String> filtered = new HashMap<>();
for (HashMap.Entry<String, String> entry : QUOTES.entrySet()) {
if (matcher.isHighlightable(entry.getKey(), text)
|| matcher.isHighlightable(entry.getValue(), text)) {
filtered.put(entry.getKey(), entry.getValue());
}
}
QuoteListAdapter adapter = new QuoteListAdapter(filtered, textHighlighter);
listView.setAdapter(adapter);
}
textHighlighter.highlight(s.toString(), matcher);
}
#Override
public void afterTextChanged(Editable s) { }
};
private TextWatcher fgColorTextWatcher = 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) {
if (s.toString().isEmpty()) {
textHighlighter.setForegroundColor(DEFAULT_FG_COLOR);
} else {
try {
int color = Color.parseColor("#" + s.toString());
textHighlighter.setForegroundColor(color);
} catch (IllegalArgumentException iae) {
textHighlighter.resetForegroundColor();
}
}
textHighlighter.invalidate(matcher);
}
#Override
public void afterTextChanged(Editable s) { }
};
private TextWatcher bgColorTextWatcher = 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) {
if (s.toString().isEmpty()) {
textHighlighter.setBackgroundColor(DEFAULT_BG_COLOR);
} else {
try {
int color = Color.parseColor("#" + s.toString());
textHighlighter.setBackgroundColor(color);
} catch (IllegalArgumentException iae) {
textHighlighter.resetBackgroundColor();
}
}
textHighlighter.invalidate(matcher);
}
#Override
public void afterTextChanged(Editable s) { }
};
private CompoundButton.OnCheckedChangeListener boldChangedListener = new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
textHighlighter.setBold(isChecked);
textHighlighter.invalidate(matcher);
}
};
private CompoundButton.OnCheckedChangeListener italicChangedListener = new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
textHighlighter.setItalic(isChecked);
textHighlighter.invalidate(matcher);
}
};
private class QuoteListAdapter extends BaseAdapter {
private ArrayList<HashMap.Entry<String, String>> quotes;
private TextHighlighter textHighlighter;
QuoteListAdapter(HashMap<String, String> quotes, TextHighlighter textHighlighter) {
this.quotes = new ArrayList<>(quotes.entrySet());
this.textHighlighter = textHighlighter;
}
#Override
public int getCount() {
return QUOTES.size();
}
#Override
public Object getItem(int position) {
return quotes.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Context context = parent.getContext();
if (convertView == null) {
LayoutInflater inflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_view_item, parent, false);
TextView nameTextView = (TextView) convertView.findViewById(R.id.name);
TextView quoteTextView = (TextView) convertView.findViewById(R.id.quote);
textHighlighter.addTarget(nameTextView);
textHighlighter.addTarget(quoteTextView);
}
TextView nameTextView = (TextView) convertView.findViewById(R.id.name);
TextView quoteTextView = (TextView) convertView.findViewById(R.id.quote);
nameTextView.setText(quotes.get(position).getKey());
quoteTextView.setText(quotes.get(position).getValue());
textHighlighter.invalidate(matcher);
return convertView;
}
}
private static final HashMap<String, String> QUOTES;
static
{
QUOTES = new HashMap<>();
for (int i = 0; i < 1; i++) {
QUOTES.put("Edsger Dijkstra" + Integer.toString(i),
"If debugging is the process of removing software bugs, then programming must be the " +
"process of putting them in.");
QUOTES.put("Linus Torvalds" + Integer.toString(i),
"Most good programmers do programming not because they expect " +
"to get paid or get adulation by the public, but because it is fun to program.");
QUOTES.put("Chris Heilmann" + Integer.toString(i),
"Java is to JavaScript what Car is to Carpet.");
QUOTES.put("Edward V Berard" + Integer.toString(i),
"Walking on water and developing software from a " +
"specification are easy if both are frozen.");
QUOTES.put("Brian Kernighan" + Integer.toString(i),
"Debugging is twice as hard as writing the code in the " +
"first place. Therefore, if you write the code as cleverly as possible, you are, " +
"by definition, not smart enough to debug it.");
QUOTES.put("Rick Osborne" + Integer.toString(i),
"Always code as if the guy who ends up maintaining your code " +
"will be a violent psychopath who knows where you live.");
QUOTES.put("Nathan Myhrvold" + Integer.toString(i),
"Software sucks because users demand it to.");
}
}
}
What can be disturbing in this code? Any help will be appreciated!
I think the problem is here
QuoteListAdapter adapter = new QuoteListAdapter(filtered, textHighlighter);
you init "filtered" from hashmap but hashmap maintains no order.
so when you iterate through the keys you don't get the order you expect.

old get view change data in other listview

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

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

ArrayAdapter filter return wrong items

I have problem with filtering array adapter with custom object. I owerride custom object toString() so it returns name- which I want filter. Problem is that it filer correct count of items but wrong items. E.g. I have list with items a1,a2, b1,b2. When I enter b it filter and I see a1,a2. Here is my adapter code. I guess problem is there:
class CustomerAdapter extends ArrayAdapter<Customer> {
private LayoutInflater mInflater;
private List<Customer> customers;
public CustomerAdapter(Context context, int textViewResourceId,
List<Customer> customers) {
super(context, textViewResourceId, customers);
mInflater = LayoutInflater.from(context);
this.customers = customers;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.customers_list_item, null);
holder = new ViewHolder();
holder.text = (TextView) convertView
.findViewById(R.id.textViewCustomerName);
holder.icon = (LinearLayout) convertView
.findViewById(R.id.linearLayoutCustomersListEdit);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// Bind the data efficiently with the holder.
holder.text.setText(customers.get(position).getCustomerName());
return convertView;
}
static class ViewHolder {
TextView text;
LinearLayout icon;
}
}
Here is filter setup and call:
customerAdapter = new CustomerAdapter(this,
R.layout.customers_list_item, repository.getCustomers());
setListAdapter(customerAdapter);
etSearch = (EditText) findViewById(R.id.editText_customers_search);
etSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int arg1, int arg2,
int arg3) {
customerAdapter.getFilter().filter(s.toString());
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
}
#Override
public void afterTextChanged(Editable arg0) {
}
});
getListView().setTextFilterEnabled(true);
List<Customer> customers = new List<Customer>();
List<Customer> tempcustomers = new List<Customer>();
customers = repository.getCustomers();
etSearch .addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
System.out.println("onTextChanged Key presed..."+s);
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
System.out.println("beforeTextChanged Key presed..."+filterText.getText());
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
System.out.println("afterTextChanged Key presed..."+filterText.getText());
if (!etSearch .getText().toString().equalsIgnoreCase("")){
tempcustomers = new List<Customer>();
String text = etSearch .getText().toString();
for(int i=0 ;i< customers .size();i++){
if(customers .get(i).toUpperCase().toString().contains(text.toUpperCase())){
tempcustomers .add(customers .get(i));
}
}
}
customerAdapter = new CustomerAdapter(this,
R.layout.customers_list_item,tempcustomers );
setListAdapter(customerAdapter);
}else{
customerAdapter = new CustomerAdapter(this,
R.layout.customers_list_item,customers );
setListAdapter(customerAdapter);
}
}
});
}

Categories

Resources