EditText in ListView changes or duplicates content while scrolling - android

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

Related

edit text in list view empty or not

In my list view ,I have Edittext and some textviews. when I am trying to iterate over listview to get edittext values, I got some problems.I want to perform actions only if my edittext in the listview is not empty. otherwise i want to continue the iteration.I need only non empty values from the edittexts when iterate through listview. my listview name is "list" here.
my code is
if (list != null) {
for (int i = 0; i < list.getChildCount(); i++) {
ed2 = (EditText) list.getChildAt(i).findViewById(R.id.pcs);
if (ed2.getText().toString().trim().length() != 0) {
TextView im_codetext = (TextView) vie.findViewById(R.id.code);
imcode = im_codetext.getText().toString();
try {
DatabaseHelper dbHelper = new DatabaseHelper(this.getApplicationContext());
newDB = dbHelper.getWritableDatabase();
Cursor cr = newDB.rawQuery("select distinct im_srp_pc, im_subgroup_code from itemmaster where im_code =" + imcode, null);
while (cr.moveToNext()) {
srppc = cr.getString(cr.getColumnIndex("im_srp_pc"));
String subcode = cr.getString(cr.getColumnIndex("im_subgroup_code"));
databas.put("subgp", subcode);
}
srps = Float.valueOf(srpval);
qtys = Integer.valueOf(ed2.getText().toString());
} catch (NumberFormatException e) {
srps = 0;
}
float amnts = srps * qtys;
sumnet = sumnet + amnts;
else{
Log.v(" empty", " empty");
}
}
}
}
My adapter class is
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.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);
c.moveToFirst();
while (c.moveToNext()){
Log.v("item detailss", c.toString());
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;
}
holder.textView1.setText(names[position+1]);
holder.textview3.setText(codes[position+1]);
holder.textView2.setText(prize[position+1]);
holder.editText2.setText(arrTemp[position]);
holder.editText2.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
// arrTemp[holder.ref] = arg0.toString();
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
arrTemp[holder.ref] = arg0.toString();
}
});
return convertView;
}
private class ViewHolder {
public Object list;
TextView textView1,textView2,textview3;
EditText editText1,editText2;
int ref;
}
}
please help.thanks in advance.
I have a Solution.
Declare a HashMap in your custom adapter class.
public class CustomAdapter extends BaseAdapter{
public HashMap<Integer,String> textMap = new HashMap<Integer,String>();
....
In getView() Method get text from HasMap and set in EditText.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.list_item, null);
holder.editText = (EditText)rowView.findViewById(R.id.editText);
holder.editText.setText(textMap.get(position));
....
And in TextChangeListner add text in HashMap by position.
holder.editText.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
// arrTemp[holder.ref] = arg0.toString();
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
textMap.put(position,holder.editText.getText().toString());
}
});
Now in your Activity you can get HashMap of adapter and get its value by position. and if there is no text in particuller position ,means edttext is empty.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
listView = (ListView)findViewById(R.id.listView);
final CustomAdapter customAdapter = new CustomAdapter(this,names);
listView.setAdapter(customAdapter);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for(int i=0;i<names.length;i++){
Toast.makeText(MainActivity.this, customAdapter.textMap.get(i), Toast.LENGTH_SHORT).show();
}
}
});

Search in list view where datas are stored in db

I have a listview of different data which are stored in DB and when i click particular data it will show the details of that and also i have used adapter class through position of the listview, it gets the detail of the data to display.
I need to add search for the listview and from the result listview, if i click the data it should show the details. please, can any one provide the code for this.
public class CustomerAdapter extends BaseAdapter{
public CustomerAdapter(Context context) {
this.context = context;
this.layoutInflater = LayoutInflater.from(context);
reload(0, TAKE);
}
private void reload(int skip, int take)
{
totalSize = customers.size();
System.out.println("Total Size: "+totalSize);
loadedCustomer = loadCustomers(skip, take);
}
private ArrayList<HashMap<String, Object>> loadCustomers(int skip, int take)
{
ArrayList<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String,Object>>();
Query query = new Query();
query.select("x.fname, x.lname, x.surrogateKey, x.id");
query.from("Customer", "x");
query.orderBy("id", SortOrder.ASCENDING);
query.setTake(take);
query.setSkip(skip);
lowIndex = skip;
highIndex = lowIndex;
QueryResultSet rs = SUP101.SUP101DB.executeQuery(query);
while(rs.next())
{
String fname = rs.getString(1);
String lname = rs.getString(2);
long sk = rs.getLong(3);
HashMap<String, Object> tempHashMap = new HashMap<String, Object>();
tempHashMap.put(NAME, " " + fname + " " + lname);
tempHashMap.put("sk", sk);
arrayList.add(tempHashMap);
highIndex++;
}
return arrayList;
}
public int getCount() {
return totalSize;
}
public Object getItem(int position) {
reloadIfNeeded(position);
return loadedCustomer.get(position - lowIndex);
}
#SuppressWarnings("unchecked")
public String getSK(int position)
{
HashMap<String, Object> map = (HashMap<String, Object>)getItem(position);
return map.get("sk").toString();
}
private void reloadIfNeeded(int newPosition)
{
if(newPosition < lowIndex || newPosition >= highIndex)
{
int lowIndex = (newPosition);
reload(lowIndex, TAKE);
}
}
public void refreshUI(boolean force)
{
if(force)
{
reload(lowIndex, TAKE);
((Activity)context).runOnUiThread(
new Runnable()
{
public void run()
{
CustomerAdapter.this.notifyDataSetChanged();
}
}
);
}
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv = null;
if(convertView==null){
convertView = layoutInflater.inflate(R.layout.customer, null);
}
tv = (TextView)convertView.findViewById(R.id.textView1);
reloadIfNeeded(position);
tv.setText(loadedCustomer.get(position - lowIndex).get(NAME).toString());
return convertView;
}
// mainmenu class
Mainmenu.this.runOnUiThread(new Runnable()
{
public void run()
{
adapter1 = new CustomerAdapter(Mainmenu.this);
listView.setAdapter(adapter1);
array_sort = new ArrayList<String>();
adapter1 = new CustomerAdapter(Mainmenu.this);
listView.setAdapter(adapter1);
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
Mainmenu.this.adapter1.getFilter().filter(cs);
}
#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
}
});
listView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> a, View v, int position, long id)
{
Intent intent = new Intent(Mainmenu.this, Detailview.class);
intent.putExtra("sk", adapter1.getSK(position));
Mainmenu.this.startActivityForResult(intent, REQUEST_DETAIL);
}
});
}
});
}
}).start();
You can make use of FilterQueryAdapter interface as below:
DataBaseHelper myDbHelper = new DataBaseHelper(this);
FilterQueryProvider fqp = new myFilterQueryProvider(myDbHelper);
<YourCursorAdapter>.setFilterQueryProvider(fqp);
<YourListView>.setTextFilterEnabled(true);
<YourEditTextSearchbox>.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
this.adaptr.getFilter().filter(s);
}
});
public class myFilterQueryProvider implements FilterQueryProvider {
DataBaseHelper myDbHelper;
public myFilterQueryProvider(DataBaseHelper mdb) {
myDbHelper = mdb;
}
public Cursor runQuery(CharSequence constraint)
{
try {
myDbHelper.openDataBase();
}catch(SQLException sqle){
throw sqle;
}
sql =""; //Your DB Query here
Cursor cursor = myDbHelper.getLobbyView(sql);
return cursor;
}
}
If you are working with a DB
simply get one Edittext textwatcher.
Every textchanged call db "LIKE" query to show your results

filtering a list with base adapter

I have a ListView with a BaseAdapter , I want to make a filter after the filed tipdoc,and my code doesn't work ,I mean nothing is heapen when start to write in the EditText and I can't find why,This is my code:
listView = (ListView) findViewById(android.R.id.list);
adapter = new ImageAndTextAdapter(Documente.this, R.layout.list_row,nume,tipdoc,formatdoc);
listView.setAdapter(adapter);
edt = (EditText) findViewById(R.id.search_box);
edt.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
ArrayList<String> nume_sort = new ArrayList<String>();
ArrayList<String> tipdoc_sort = new ArrayList<String>();
ArrayList<String> formatdoc_sort = new ArrayList<String>();
int textlength = edt.getText().length();
nume_sort.clear();
tipdoc_sort.clear();
formatdoc_sort.clear();
for (int i = 0; i < tipdoc.size(); i++)
{
if (textlength <= tipdoc.get(i).length())
{
if (edt.getText().toString().equalsIgnoreCase((String) tipdoc.get(i).subSequence(0, textlength)))
{
tipdoc_sort.add(tipdoc.get(i));
}
}
}
listView.setAdapter(new ImageAndTextAdapter
(Documente.this, R.layout.list_row,nume_sort,tipdoc_sort,formatdoc_sort));
}
#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
}
});
}
private class ImageAndTextAdapter extends BaseAdapter{
private Context adContext;
public int getCount() {
return nume.size();
}
public ImageAndTextAdapter(Context context,int layout,ArrayList<String> nume,ArrayList<String> tipdoc,ArrayList<String> formatdoc)
{
super();
this.adContext = context;
}
public View getView(int pos, View inView, ViewGroup parent){
View v = inView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater)adContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_row, null);
}
String num = String.format(nume.get(pos));
((TextView)v.findViewById(R.id.Nume)).setText(num);
String tdoc = String.format(tipdoc.get(pos));
((TextView)v.findViewById(R.id.TDoc)).setText(tdoc);
if (formatdoc.get(pos).equals("doc")){
((ImageView)v.findViewById(R.id.list_image)).setImageResource(R.drawable.doc);
}
else if (formatdoc.get(pos).equals(".xlsx") || formatdoc.get(pos).equals("xls")) ((ImageView)v.findViewById(R.id.list_image)).setImageResource(R.drawable.xls);
else ((ImageView)v.findViewById(R.id.list_image)).setImageResource(R.drawable.pdf);
return v;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}
you have to change the value of nume var,because in your code its the same
try this code...
et.addTextChangedListener(new TextWatcher()
{
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 = et.getText().length();
array_sort.clear();
for (int i = 0; i < listview_array_location.length; i++)
{
if (textlength <= listview_array_location[i].length())
{
if(et.getText().toString().equalsIgnoreCase((String)listview_array_location[i].subSequence(0,textlength)))
{
array_sort.add(listview_array[i]);
}
}
}
AppendList(array_sort);
}
});
}
public void AppendList(ArrayList<String> str)
{
listview_arr = new String[str.size()];
listview_arr = str.toArray(listview_arr);
setListAdapter(new bsAdapter(this));
}
public class bsAdapter extends BaseAdapter
{
Activity cntx;
public bsAdapter(Activity context)
{
// TODO Auto-generated constructor stub
this.cntx=context;
}
public int getCount()
{
// TODO Auto-generated method stub
return listview_arr.length;
}
public Object getItem(int position)
{
// TODO Auto-generated method stub
return listview_arr[position];
}
public long getItemId(int position)
{
// TODO Auto-generated method stub
return listview_array.length;
}
public View getView(final int position, View convertView, ViewGroup parent)
{
View row=null;
LayoutInflater inflater=cntx.getLayoutInflater();
row=inflater.inflate(R.layout.search_list_item, null);
TextView tv=(TextView)row.findViewById(R.id.title);
tv.setText(listview_arr[position]);
return row;
}
}

I have a ListView, where each row contains an EditText field and textview, what ever i typed in the Edittext i want to see in the textview?

Here i have the following code below :-
public class OrderActivity extends ListActivity implements OnClickListener{
ArrayList<String> data,rate, num;
OrderAdapter orderAdapter;
int i = 0;
int s;
TextView tvNumber,tvName,tvRate;
EditText etQty,etRemarks;
Button sendToConfirm,edit,saveAndAdd;
// EditText qty,remarks;
//OrderAdapter orderAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.order);
Init();
Here i get arraylists from previous activity
Bundle extras = getIntent().getExtras();
data = extras.getStringArrayList("list");
rate = extras.getStringArrayList("rate");
num = extras.getStringArrayList("number");
for(String str : data) {
Log.d("data", str);
}
for(String str1 : rate) {
Log.d("data", str1);
//s = Integer.parseInt(st)
}
//Log.d("total", s);
for(String strnum : num) {
Log.d("data", strnum);
}
String[] captionArray = (String[]) data.toArray(
new String[data.size()]);
orderAdapter = new OrderAdapter(
OrderActivity.this, R.layout.order_list,
captionArray);
setListAdapter(orderAdapter);
return ;
}
Initialize the buttons
public void Init() {
sendToConfirm = (Button) findViewById(R.id.bsendtoconfirm);
edit = (Button) findViewById(R.id.bEdit);
saveAndAdd = (Button) findViewById(R.id.bsave);
sendToConfirm.setOnClickListener(this);
edit.setOnClickListener(this);
saveAndAdd.setOnClickListener(this);
//etQty.addTextChangedListener(this);
}
Here is the baseAdapter class.
private class OrderAdapter extends BaseAdapter {
String[] items;
public OrderAdapter(Context context, int textViewResourceId,
String[] items) {
this.items = items;
}
public View getView(final int POSITION, View convertView,
ViewGroup parent) {
// TextView tvNumber,tvName,tvRate;
// EditText etQty,etRemarks;
View view = convertView;
if (view == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.order_list, null);
}
tvNumber = (TextView) view.findViewById(R.id.itemnumber);
tvName = (TextView) view.findViewById(R.id.itemname);
tvRate = (TextView) view.findViewById(R.id.itemrate);
etQty = (EditText) view.findViewById(R.id.editqty);
etRemarks = (EditText) view.findViewById(R.id.editremarks);
etQty = (EditText) view.findViewById(R.id.editqty);
//tvRate = (TextView) v.findViewById(R.id.itemrate);
Ontextchange listener . here whatever i type in the edittext i need to get in the textview ?please help me out with this.
etQty.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
String text;
text=etQty.getText().toString();
if(text!=null)
{
tvRate.setText(text);
}
else {
tvRate.setText("You Have Not type anything");
}
}
});
tvNumber.setText(num.get(POSITION));
tvName.setText(data.get(POSITION));
tvRate.setText(rate.get(POSITION));
return view;
}
public int getCount() {
// TODO Auto-generated method stub
return items.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bsendtoconfirm:
Log.d("click", "send");
break;
case R.id.bEdit:
Log.d("click", "edit");
break;
case R.id.bsave:
Log.d("click", "saved");
finish();
break;
}
}
// public void afterTextChanged(Editable s) {
// // TODO Auto-generated method stub
//// String text;
//// text=etQty.getText().toString();
//// if(text!=null)
//// {
//// tvRate.setText(text);
//// }
//// else {
//// tvRate.setText("You Have Not type anything");
//// }
// }
// public void beforeTextChanged(CharSequence s, int start, int count,
// int after) {
// // TODO Auto-generated method stub
//
// }
// public void onTextChanged(CharSequence s, int start, int before, int count) {
// // TODO Auto-generated method stub
//
// }
}
hi you can use the code below to do this,
for source:
https://dl.dropbox.com/u/68130108/ListViewEditItem.rar
public class OrderActivity extends ListActivity implements OnClickListener{
// create static object to hold your widget in listview
static TextView sTxt;
static EditText sEdTxt;
static int p;
.
.
.
.
etQty.setId(position);
etQty.setTag(tvRate);
etQty.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
sPosition = v.getId();
sTxt = (TextView) v.getTag();
EditText e = ((EditText) v);
if (!e.getText().toString().equals("")) {
if (hasFocus) {
// change your num arraylist values at the position
// because you can’t save anything on ListView Item.
num.set(sPosition,e.getText().toString());
}
}
}
});
editText.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) {
// every text change setText of textview
sTxt.setText(s);
}
});
tvRate.setText(num.get(position));
.
.
.
.

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