ImageView in GridView on working properly - android

I have a stock check application wherein the materials are displayed in a grid and each grid element has an image of the material, edittext of quantity , check button and an image which turns red or green depending on whether stock is available or not.
There is also a textview which be set to "Available" if image is green else to "Not Available" if image is red.
I am using a custom adapter which is extending a base adapter.
Everything is working fine until i press the check button
I used edittext textwatcher to get the text changed at which position but when i m pressing the check button the image is set to green at the 9th element of the 3*3 Grid.
I am using viewHolder but still its not working.
Can anyone please help.
Please see the below codes
private class CustomAdapter extends ArrayAdapter<HashMap<String, Object>> {
private Context context;
boolean[] checkboxstate;
ViewHolder viewHolder;
ArrayList<HashMap<String, Object>> testdata;
public CustomAdapter(Context context, int textViewResourceId,
ArrayList<HashMap<String, Object>> testdata) {
super(context, textViewResourceId, testdata);
this.context=context;
this.testdata=testdata;
checkboxstate = new boolean[testdata.size()];
}
private class ViewHolder {
TextView txt_product;
ImageView image_available;
ImageView image_product;
TextView txt_available;
EditText quantity;
Button check;
}
#Override
public View getView(final int position, View convertView,
ViewGroup parent) {
View gridView;
if (convertView == null) {
gridView=new View(context);
convertView = inflater.inflate(R.layout.salesdummy, null);
viewHolder = new ViewHolder();
// cache the views
viewHolder.quantity=(EditText)convertView.findViewById(R.id.quantity);
viewHolder.image_product=(ImageView)convertView.findViewById(R.id.image1);
viewHolder.image_available=(ImageView)convertView.findViewById(R.id.imagecheck);
viewHolder.txt_available=(TextView)convertView.findViewById(R.id.txt_available);
viewHolder.check=(Button)convertView.findViewById(R.id.btncheck);
viewHolder.txt_product=(TextView)convertView.findViewById(R.id.product);
// link the cached views to the convertview
convertView.setTag(viewHolder);
} else
viewHolder = (ViewHolder) convertView.getTag();
// set the data to be displayed
viewHolder.txt_product.setText(testdata.get(position).get("MATERIAL").toString());
int photoId=(Integer) testdata.get(position).get("IMAGE");
viewHolder.image_product.setImageDrawable(getResources().getDrawable(photoId));
/* viewHolder.quantity.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
quant=s.toString();
}
});
*/
viewHolder.check.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("VAlue of string",quant);
int i_quant;
i_quant=Integer.parseInt(quant);
String material=testdata.get(position).get("MATERIAL").toString();
Log.d("Check material",material);
Cursor c=db.GetMaterial(material);
rec_quantity=c.getString(2);
System.out.println("Value of received quantity is" + rec_quantity);
i_rec_quantity=Integer.parseInt(rec_quantity);
if(i_quant>i_rec_quantity)
{
System.out.println("===============Value is greater than stock");
viewHolder.image_available.setBackgroundDrawable(getResources().getDrawable(R.drawable.red));
viewHolder.txt_available.setText("Not Available");
}
else
{
viewHolder.image_available.setBackgroundDrawable(getResources().getDrawable(R.drawable.green));
viewHolder.txt_available.setText("Available");
}
}
});
viewHolder.quantity.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
quant=s.toString();
}
});
/*
viewHolder.txt_name.setText(testdata.get(position)
.get("NAME").toString());
viewHolder.txt_salesvolume.setText(testdata.get(position)
.get("SALESVOLUME").toString());
viewHolder.txt_overdue.setText(testdata.get(position)
.get("OVERDUE").toString());
viewHolder.txt_outstanding.setText(testdata.get(position)
.get("OUTSTANDING").toString());
viewHolder.txt_creditlimit.setText(testdata.get(position)
.get("CREDIT_LIMIT").toString());
viewHolder.txt_creditutilization.setText(testdata.get(position)
.get("CREDIT_UTILIZATION").toString());
String creditlimit=testdata.get(position).get("CREDIT_LIMIT").toString();
String creditutilized=testdata.get(position).get("CREDIT_UTILIZATION").toString();
*/
/*
String remcom=testdata.get(position).get("CREDIT_UTILIZATION").toString();
Log.d("Remcom value",remcom);
String remcom1=remcom.replace(",","");
int inint=Integer.parseInt(remcom1);
if(inint>150000)
{
viewHolder.txt_creditutilization.setTextColor(Color.RED);
}
*/
// VITAL PART!!! Set the state of the
// CheckBox using the boolean array
// //HAVE OPENED THE BELOW COMMENT AND HAVE DONE THIS TO CHECK
// WHETHER DUPLICATE CHECKS ARE NOT IMPLEMENTED
// return the view to be displayed
return convertView;
}
}

Related

EditText in ListView changes or duplicates content while scrolling

I have two EditTexts in my ListView. All I want is adding a TextWatcher to both the EditTexts , so I can avoid duplicating and changing values in it while scrolling. I can successfully add the TextWatcher to one of the EditTexts, but could not implement for two EditTexts. I tried other answers from stackoverflow. But I didn't get desired output.
This is my code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.orders);
list = (ListView)findViewById(R.id.list1);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
SelectItems();
ViewItems();
}
public List<Map<String, String>> SelectItems() {
// TODO Auto-generated method stub
try {
datas = new ArrayList<Map<String, String>>();
DatabaseHelper dbHelper = new DatabaseHelper(this.getApplicationContext());
newDB = dbHelper.getWritableDatabase();
Cursor c = newDB.rawQuery("select distinct im_code ,im_desc ,im_srp"
+ " from itemmaster", null);
c.moveToFirst();
while (c.moveToNext()){
datanums.put("name",c.getString(c.getColumnIndex("im_desc")));
datanums.put("code", c.getString(c.getColumnIndex("im_code")));
datanums.put("imsrp",c.getString(c.getColumnIndex("im_srp")));
datas.add(datanums);
}
} catch (SQLiteException se ) {
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
}
return datas;
}
public void ViewItems() {
// TODO Auto-generated method stub
arrTemp = new String[datas.size()];
MyListAdapter myListAdapter = new MyListAdapter();
list.setAdapter(myListAdapter);
}
public class MyListAdapter extends BaseAdapter{
#Override
public int getCount() {
// TODO Auto-generated method stub
if(datas != null && datas.size() != 0){
return datas.size();
}
return 0;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return datas.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
//ViewHolder holder = null;
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = Orders.this.getLayoutInflater();
convertView = inflater.inflate(R.layout.order_simple_row, null);
holder.textView1 = (TextView) convertView.findViewById(R.id.name);
holder.textView2 = (TextView) convertView.findViewById(R.id.srp);
holder.textview3 = (TextView) convertView.findViewById(R.id.code);
holder.editText1 = (EditText) convertView.findViewById(R.id.cases);
holder.editText2 = (EditText) convertView.findViewById(R.id.pcs);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.ref = position;
String [] names = new String[550];
String [] codes = new String[550];
String [] prize = new String[550];
DatabaseHelper dbHelper = new DatabaseHelper(Orders.this.getApplicationContext());
newDB = dbHelper.getWritableDatabase();
Cursor c = newDB.rawQuery("select distinct im_code ,im_desc ,im_srp "
+ " from itemmaster", null);
Log.v("item detailss", c.toString());
c.moveToFirst();
while (c.moveToNext()){
String cod = c.getString(c.getColumnIndex("im_code"));
codes[c.getPosition()] =cod;
String desc1 = c.getString(c.getColumnIndex("im_desc"));
names[c.getPosition()] = desc1;
String price = c.getString(c.getColumnIndex("im_srp"));
prize[c.getPosition()] = price;
}
newDB.close();
holder.textView1.setText(names[position+1]);
holder.textview3.setText(codes[position+1]);
holder.textView2.setText(prize[position+1]);
holder.editText1.setText(arrTemp[holder.ref]);
holder.editText2.setText(arrTemp[holder.ref]);
holder.editText1.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
arrTemp[holder.ref] = arg0.toString();
datas.get(position).put("pref", holder.editText1.toString().trim());
}
});
return convertView;
}
private class ViewHolder {
TextView textView1,textView2,textview3;
EditText editText1,editText2;
public int ref;
}
}
please help me to do this in right way.
Recently I had a similar problem, here is how I solved it using HashMap:
In the adapter constructor, initialize HashMap and make sure to clear it:
public MyAdapter(Context context, String[] texts){
this.context = context;
this.texts = texts;
hashMapTexts = new HashMap<String,String>();
for(String text: texts){
hashMapTexts.put(text, "");
}
Then, correct your private class ViewHolder inside the adapter like that:
private class ViewHolder {
private TextView listItemTextView;
private EditText listItemEditText;
private int position;
public ViewHolder(View view) {
listItemTextView = (TextView)view.findViewById(R.id.list_item_text_view);
listItemEditText = (EditText)view.findViewById(R.id.list_item_edit_text);
}
}
Then, initialize the ViewHolder in the overriden getView() method:
if(convertView == null){
convertView = View.inflate(context, R.layout.list_item, null);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder)convertView.getTag();
}
Get the position of the ViewHolder in the same method:
viewHolder.position = position;
Set the texts for your views:
viewHolder.listItemTextView.setText(texts[position]);
viewHolder.listItemEditText.setText(hashMapTexts.get(texts[position]));
and now use the TextWatcher:
viewHolder.listItemFormEditText.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) {
}
#Override
public void afterTextChanged(Editable s) {
hashMapTexts.put(texts[viewHolder.position], s.toString());
}
});
return convertView;
I suggest you to start with easier case, like mine, then apply it to your specific scenario.
Use different textwatchers for each edit text if you want to perform
different operations on each edit text
EditText1.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
Apply same for other EditText too
et1.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});

How to avoid the refreshing of edittext values in Listview on scroll?

I have a listview with 2 textviews and an Edit text. I have used a custom adapter to populate the listview (as I used the same for avoiding refreshing of checkboxes a month ago), my question is how to avoid the refereshing of the edit texts when the list view is scrolled, as I want to save the data on each edittext. Will the textwatcher help in any way to avoid the refreshing and saving ? If yes please help.I have included my the model and custom adapter class here:
Model class:
public class InternalListModel {
String no;
String name;
String value;
public InternalListModel(String internNo, String internName) {
// TODO Auto-generated constructor stub
this.no=internNo;
this.name=internName;
value="";
}
public String getNo(){
return no;
}
public String getName(){
return name;
}
public String getValue(){
return value;
}
public void setValue(String value){
this.value=value;
}
}
custom adapter class:
public class InternCustomAdapter extends ArrayAdapter<InternalListModel>
{
private final List<InternalListModel> internnoname;
private final Internal6 context;
public InternCustomAdapter(Internal context,
List<InternalListModel> internnoname) {
super(context,R.layout.internlistdecor,internnoname);
// TODO Auto-generated constructor stub
this.context=context;
this.internnoname=internnoname;
}
static class ViewHolder{
protected TextView textview,textview1;
protected EditText edittext;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder viewHolder;
if(convertView ==null){
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.internlistdecor,null);
viewHolder = new ViewHolder();
viewHolder.textview=(TextView)convertView.findViewById(R.id.tvintNO);
viewHolder.textview1=(TextView)convertView.findViewById(R.id.tvintName);
viewHolder.edittext = (EditText)convertView.findViewById(R.id.etintValue);
viewHolder.edittext.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
convertView.setTag(viewHolder);
convertView.setTag(R.id.tvintNO, viewHolder.textview);
convertView.setTag(R.id.tvintName, viewHolder.textview1);
convertView.setTag(R.id.etintValue, viewHolder.edittext);
}
else
{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.textview.setText(internnoname.get(position).getNo());
viewHolder.textview1.setText(internnoname.get(position).getName());
viewHolder.edittext.setText(internnoname.get(position).getValue());
return convertView;
}
}
You can set the entered text, in the value field for that position(index)
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
internnoname.get(position).setValue(s.toString());
}
That way even when the Views are recycled, depending on the position, the value will be set properly to the EditTexView, because of the below code:
viewHolder.edittext.setText(internnoname.get(position).getValue());

how to set edittext text in custome adapter class on text change listner

i want to change editext values (with calculations) on text change listener in custom adapter class
my code:
public class Voucher_Detail_Adapter extends ArrayAdapter<Voucher_List_Details> {
/*Context context;
int layoutResourceId;
ProgressDialog pd1;
ArrayList<Voucher_List_Details> data1=new ArrayList<Voucher_List_Details>();
boolean[] checkBoxState;
static Voucher_List_Details data[] = null;*/
/*static Context context;
int layoutResourceId;
static Voucher_List_Details data[] ;*/
int deducted_balance,assqty;
static Voucher_Detail_Adapter ab;
int d;
//Button b;
String voucher_type, voucher_name;
int voucher_price,voucher_balance, voucher_dist_id,vid1 ;
private LoginDBAdapter mySQLiteAdapter;
SimpleCursorAdapter cursorAdapter;
static Context context;
int layoutResourceId;
static Voucher_List_Details data[] = null;
public BookHolder holder = null;
static Voucher_Detail_Adapter b;
public Voucher_Detail_Adapter(Context context, int textViewResourceId,
Voucher_List_Details[] objects) {
super(context, textViewResourceId, objects);
this.layoutResourceId = textViewResourceId;
Voucher_Detail_Adapter.context = context;
Voucher_Detail_Adapter.data = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
// BookHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new BookHolder();
holder.vname = (TextView) row.findViewById(R.id.txtTitle1);
holder.vtype = (EditText) row.findViewById(R.id.vtype);
holder.vprice = (EditText) row.findViewById(R.id.vprice);
holder.vbalance = (EditText) row.findViewById(R.id.vbal);
holder.vdid=(EditText)row.findViewById(R.id.vdid);
holder.qty=(EditText)row.findViewById(R.id.aqty);
row.setTag(holder);
Voucher_List_Details book_detail = data[position];
holder.vname.setText(book_detail.getV_name());
System.out.println("name" +book_detail.getV_name());
holder.vtype.setText(book_detail.getV_type());
holder.vprice.setText(String.valueOf(book_detail.getV_price()));
holder.vbalance.setText(String.valueOf(book_detail.getV_bal()));
holder.vdid.setText(String.valueOf(book_detail.getV_did()));
System.out.println("did" +book_detail.getV_did());
holder.qty.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
//assqty=10;
deducted_balance=((Integer.parseInt(holder.vbalance.getText().toString()))-assqty);
System.out.println("qty" +assqty +"deducted" +deducted_balance);
Toast.makeText(getContext(),String.valueOf(deducted_balance), Toast.LENGTH_SHORT).show();
holder.vprice.setText(String.valueOf(deducted_balance));
//deducted_balance=holder.vprice.setText(String.valueOf(book_detail.getV_price());
}
});
} else {
holder = (BookHolder) row.getTag();
}
return row;
}
static class BookHolder {
TextView vname;
EditText vtype;
EditText vprice;
EditText vbalance;
EditText vdid;
EditText qty;
}
}
plzz help me my editext values are not changing it shows default values as i set.
i want to do some calculations and shows that values in edittext.i have used settext method but it dosent working
Thank You
Try this..........
holder.qty.addTextChangedListener(new View.TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
//assqty=10;
deducted_balance = ((Integer.parseInt(holder.vbalance.getText().toString())) - assqty);
System.out.println("qty" + assqty + "deducted" + deducted_balance);
Toast.makeText(getContext(), String.valueOf(deducted_balance),
Toast.LENGTH_SHORT).show();
TextView vprice_textview =
(TextView) listview.getChildAt(position).findViewById(R.id.textviewid);
vprice_textview.setText(String.valueOf(deducted_balance));
}
});

How to create search functionality in listview [duplicate]

This question already has answers here:
unable to search using custom Array Adapter in Android?
(2 answers)
Closed 10 years ago.
I want to create search functionality in listview,i have a small doubt that using arraylist can we perform search functionality.Here i ahve attached the code..where the
Totalarraylist is the arraylist and from that arraylist i am passing the data to Question adapter class
code,,
searchbox.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
If so then what to add here for search functionality...
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
adapterfirst = new Questionadapter(MainActivity.this,Totalarraylist);
listviewfirst = (ListView) findViewById(R.id.listquestion);
listviewfirst.setAdapter(adapterfirst);
}
public class Questionadapter extends BaseAdapter {
private ArrayList<HashMap<String, Object>> data;
public LayoutInflater inflater;
Context context;
public Questionadapter(Context context, ArrayList<HashMap<String,Object>> Totalarraylists) {
// TODO Auto-generated constructor stub
data=Totalarraylists;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
System.out.println("data="+data);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
//
}
}
could anybody help me to do this..
add a filter to your ListAdapter, and on onTextChanged apply the filter to listadapter
Try this, Compare your array list with your string you will get answer
public class MainActivity extends Activity {
ArrayList<String> list = new ArrayList<String>();
private EditText searchEdt;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// making it full screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_ugsimply_beta);
list.add("Android");
list.add("Android1");
list.add("blackberry");
list.add("samsung");
list.add("LG");
searchEdt = (EditText) findViewById(R.id.searchEdt);
searchEdt.addTextChangedListener(new TextWatcher() {
private int textlength;
private ArrayList<String> arrayList_sort = new ArrayList<String>();
public void afterTextChanged(Editable s) {
// Abstract Method of TextWatcher Interface.
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// Abstract Method of TextWatcher Interface.
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
textlength = searchEdt.getText().length();
if (arrayList_sort != null)
arrayList_sort.clear();
for (int i = 0; i < list.size(); i++) {
if (textlength <= list.get(i).toString().length()) {
if (searchEdt
.getText()
.toString()
.equalsIgnoreCase(
(String) list.get(i).toString()
.subSequence(0, textlength))) {
arrayList_sort.add(list.get(i));
Log.d("TAG", "log" + arrayList_sort.size());
}
}
}
}
});
}
}

EditText Content Changes on ListView Scroll

I have an Android application with an customized adapter that extends Cursor Adapter.
I overrided bindView and newview methods of CursorAdapter. Each of my ListView row contains an EditText. When I enter some text in EditText and scrolldown the ListView the content of the EditText text is moved to another EditText.
Can anyone help me in sorting out this issue to how to prevent EditText from reused its content when ListView is scrolled down.
Thanks in Advance,
Here is the code:
private class AddScreenArrayAdapter extends ArrayAdapter<AddScreenItems>{
ViewHolder viewHolder = null;
public String[] holder_values;
public AddScreenArrayAdapter(Context context, int textViewResourceId,
List<AddScreenItems> objects) {
super(context, textViewResourceId, objects);
holder_values = new String[objects.size()];
// TODO Auto-generated constructor stub
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.additem_row, null);
viewHolder = new ViewHolder();
viewHolder.label = (TextView) v.findViewById(R.id.label_add_item);
viewHolder.dbName=(TextView)v.findViewById(R.id.dbtable_name);
viewHolder.value = (EditText) v.findViewById(R.id.value_add_item);
viewHolder.value.addTextChangedListener(new TextWatcher()
{
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
holder_values[viewHolder.ref]= arg0.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
}
});
v.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder) v.getTag();
}
AddScreenItems addScreenItems = addScreenItemsList.get(position);
if(addScreenItems!=null)
{
viewHolder.label.setText(addScreenItems.labelname);
viewHolder.dbName.setText(addScreenItems.databaseTableName);
// viewHolder.ref = position;
if (holder_values[position] != null)
{
viewHolder.value.setText(holder_values[position]);
}
else {
viewHolder.value.setText("");
}
}
return v;
}
}
static class ViewHolder {
public TextView label;
public EditText value;
public TextView dbName;
int ref;
}
If you're having problem after scrolling it's the view recycling that's messing you up. You basically need to set up an array to hold the contents of the EditTexts so when you scroll they don't get messed up.
Here's a link to another answer I gave on a similar topic. It ought to point the way for you. SO Answer
EDIT
I use setOnFocusChangeListener to run code when the user finishes an entry in the EditText box.
qty.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
LinearLayout parent = (LinearLayout) v.getParent();
EditText qtyTemp = (EditText) parent.findViewById(R.id.qty);
quantity[pos] = qtyTemp.getText().toString();
}
}
});

Categories

Resources