I am using ListView to show TextViews in some rows and not others. I don't know how many items there will be, it runs fine but when I scroll down the display changed.
I'm working on an app where users fill reports and polls. In an activity I have a ListView where children contain EditTexts, Checkbox and TextView.
The problem with EditTexts and Checkbox was that when scrolling, the content that they have is lost. I was able to fix the Checkbox problem by saving the check status in an array, but I can't fix the problem with EditTexts. I know that there are some questions about this particular issue but the solutions that they provided doesn't seem to work.
Im using this BaseAdapter.
public class ProductAdapter extends BaseAdapter {
private Context mContext;
private List<Model> models = new ArrayList<>();
private ModelTable avModelTable = new ModelTable();
private BrandTable brandTable = new BrandTable();
public ProductAdapter(Context mContext, List<Model> models) {
this.mContext = mContext;
this.models = models;
}
#Override
public int getCount() {
return models.size();
}
#Override
public Model getItem(int i) {
return models.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
ProductHolder item = new ProductHolder();
View row = view;
if (row == null) {
LayoutInflater mInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = mInflater.inflate(R.layout.product_item, viewGroup, false);
item.nameProduct = (TextView) row.findViewById(R.id.nameProduct);
item.subCategoryProduct = (TextView) row.findViewById(R.id.subCategoryProduct);
item.productGroup = (RadioGroup) row.findViewById(R.id.groupRadioProduct);
item.oos_product = (RadioButton) row.findViewById(R.id.oos_product);
item.av_product = (RadioButton) row.findViewById(R.id.av_product);
item.shelfProduct = (EditText) row.findViewById(R.id.shelfProduct);
item.quantityProduct = (EditText) row.findViewById(R.id.quantityProduct);
item.priceProduct = (EditText) row.findViewById(R.id.priceProduct);
item.layoutBrand = (LinearLayout) row.findViewById(R.id.layoutBrand);
item.avBrand = (EditText) row.findViewById(R.id.avBrand);
row.setTag(item);
} else {
item = (ProductHolder) row.getTag();
}
final Model model = models.get(i);
item.nameProduct.setText(model.getProduct_name());
item.subCategoryProduct.setText(brandTable.getBrandName(model.getBrand_id()));
if (model.getQuantity() != 0) {
item.quantityProduct.setText(String.valueOf(model.getQuantity()));
}
if (model.getShelf() != 0) {
item.shelfProduct.setText(String.valueOf(model.getShelf()));
}
if (model.getPrice() != null) {
item.priceProduct.setText(model.getPrice());
}
if (model.getBrand_id() == 1) {
item.layoutBrand.setVisibility(View.GONE);
item.avBrand.setVisibility(View.GONE);
item.productGroup.setVisibility(View.VISIBLE);
item.oos_product.setVisibility(View.VISIBLE);
item.av_product.setVisibility(View.VISIBLE);
switch (model.getAv()) {
case 0:
item.oos_product.setChecked(true);
break;
case 1:
item.av_product.setChecked(true);
break;
default:
break;
}
item.productGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i) {
case R.id.oos_product:
avModelTable.updateAv(model.getId(), 0);
break;
case R.id.av_product:
avModelTable.updateAv(model.getId(), 1);
break;
default:
break;
}
}
});
} else {
item.layoutBrand.setVisibility(View.VISIBLE);
item.avBrand.setVisibility(View.VISIBLE);
item.productGroup.setVisibility(View.GONE);
item.oos_product.setVisibility(View.GONE);
item.av_product.setVisibility(View.GONE);
item.avBrand.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
if (arg0.toString().equals("")) {
avModelTable.updateAv(model.getId(), 0);
} else avModelTable.updateAv(model.getId(), Integer.parseInt(arg0.toString()));
}
});
}
item.shelfProduct.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
if (arg0.toString().equals("")) {
avModelTable.updateShelf(model.getId(), 0);
} else avModelTable.updateShelf(model.getId(), Integer.parseInt(arg0.toString()));
}
});
item.quantityProduct.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
if (arg0.toString().equals("")) {
avModelTable.updateQuantity(model.getId(), 0);
} else
avModelTable.updateQuantity(model.getId(), Integer.parseInt(arg0.toString()));
}
});
item.priceProduct.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
if (arg0.toString().equals("")) {
avModelTable.updatePrice(model.getId(), null);
} else
avModelTable.updatePrice(model.getId(), arg0.toString());
}
});
return row;
}
private class ProductHolder {
private TextView nameProduct, subCategoryProduct;
private RadioButton oos_product, av_product;
private RadioGroup productGroup;
private EditText shelfProduct, quantityProduct, priceProduct, avBrand;
private LinearLayout layoutBrand;
}
}
I made a test code using your adapter but with different views/models and tried it but it didn't work. It only worked after I replaced View row = view; with View row = null; at the start of your getView method, forcing the code to inflate the views every time. That's why I believe the problem is at the item = (ProductHolder) row.getTag(); part. Perhaps the ProductHolder model is not correct.
Related
Trying to filter my listview with a edittext, everytime i enter some text the listview becomes blank, can anyone tell me what I'm doing wrong and how i can solve this
search.addTextChangedListener(new TextWatcher (){
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
String searchString = cs.toString();//wsSearch.getText().toString();
/* if(searchString.length() != 2) {
adapter2 = new Adapter(MainActivity.this, array);
listView.setAdapter(adapter2);
return;
}*/
for (int i = 0; i < array.size(); i++)
{
String currentString = array.get(i).getTitulo();
if (searchString.equalsIgnoreCase(currentString))
{
mTemp.add(array.get(i));
}
}
adapter2 = new Adapter(MainActivity.this, mTemp);
listView.setAdapter(adapter2);
}
});
}
Try this :
search.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
String searchString = arg0.toString();//wsSearch.getText().toString();
/* if(searchString.length() != 2) {
adapter2 = new Adapter(MainActivity.this, array);
listView.setAdapter(adapter2);
return;
}*/
mTemp.clear()
for (int i = 0; i < array.size(); i++)
{
String currentString = array.get(i).getTitulo();
if (currentString.contains(searchString))
{
mTemp.add(array.get(i));
}
}
adapter2 = new Adapter(MainActivity.this, mTemp);
listView.setAdapter(adapter2);
}
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
//BAPTISMAL_SONG.this.adapter2.getFilter().filter(cs);
}
});
Hope this helps.
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
}
});
Here is my Custom list adapter how can i implement search facility In custom list adapter using edit text. I tried many ways to implement search facilty
public class ListAdapter extends BaseAdapter {
public ArrayList<Users> listDataContainers = new ArrayList<>();
ListAdapter(ArrayList<Users> listDataContainers) {
this.listDataContainers = listDataContainers;
}
#Override
public int getCount() {
return listDataContainers.size();
}
#Override
public User getItem(int arg0) {
return listDataContainers.get(arg0);
}
#Override
public long getItemId(int arg0) {
return arg0;
}
#Override
public View getView(int pos, View arg1, ViewGroup arg2) {
if (arg1 == null) {
LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
arg1 = inflater.inflate(R.layout.listitem_layout, arg2, false);
}
TextView pa_name = (TextView) arg1.findViewById(R.id.textName);
TextView pa_date = (TextView) arg1.findViewById(R.id.textRoom);
Patient item = listDataContainers.get(pos);
pa_name.setText(item.name);
pa_date.setText(item.adm_date);
if (pos % 2 == 0)
arg1.setBackgroundResource(R.drawable.listview_selector_white);
else
arg1.setBackgroundResource(R.drawable.listview_selector_grey);
return arg1;
}
}
Or you can use a TextWatcher:
When an object of a type is attached to an Editable, its methods will be called when the text is changed.
searchField = (EditText) findViewById(R.id.search);
searchField.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
String text = searchField.getText().toString().toLowerCase(Locale.getDefault());
adapter.filter(text);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
});
Now, the EditText will capture user input and pass it to the filter function of your custom adapter class.
In your custom adapter's constructor, you need to initialize a second list, to keep track of the currently desired / searched items:
public class ListAdapter extends BaseAdapter {
public ArrayList<Users> listDataContainers = new ArrayList<>();
private ArrayList<Users> usersFilteredList;
ListAdapter(ArrayList<Users> listDataContainers) {
this.listDataContainers = listDataContainers;
this.usersFilteredList = new ArrayList<Users>();
this.usersFilteredList.addAll(listDataContainers);
}
Add the filter function to your Adapter class and don't forget to call notifyDataSetChanged to update the UI.
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
listDataContainers.clear();
if (charText.length() == 0) {
listDataContainers.addAll(usersFilteredList);
} else {
for (Users user : usersFilteredList) {
if (user.getName().toLowerCase(Locale.getDefault())
.contains(charText)) {
listDataContainers.add(user);
}
}
}
notifyDataSetChanged();
}
Also, take a look at some other Q&A's on the same topic:
How to use the TextWatcher class in Android?
You need to use OnQueryTextListener, consider searchView to be the edit text:
searchView.setOnQueryTextListener(textChangeListener);
SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText){
if (TextUtils.isEmpty(newText)){
searchView.clearFocus();
//Search logic when edit text is empty
}
else {
//Search logic when edit text is not empty
}
return true;
}
#Override
public boolean onQueryTextSubmit(String query) {
return true;
}
};
try this it helps you.
use textwatcher for your edittext as below:
// In your main activity
et_Search = (EditText)findViewById(R.id.et_Search);
et_SearchBar.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count)
{
try
{
try{
(listDatacontainers).getFilter().filter(s.toString());
}
catch(Exception e)
{
Log.i("error at ListAdapter", e.toString());
}
}
catch (Exception e)
{
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s)
{
}
});
create method getfilter() in your adapter source file
I have a custom listview that contains an edittext,checkbox and two text views.Now i am hving a problem with the edittext values in my code.I am not able to get the correct value from the edittext.And also if i set the value of an edit text at one position in the list,that value is also set to edittexts at random positions in the listview.And i have the common problem of the values changing on scroll.Following is the code:
Activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_assess_list);
prev = getIntent();
final ListView listview = (ListView) findViewById(R.id.studentsListView);
listview.setAdapter(new AssessmentAdapter(this, R.layout.assessment,
StudentNames.student_name, StudentNames.studentRollNo));
}
Adapter:
public class AssessmentAdapter extends ArrayAdapter<String> {
Context context;
ViewHolder holder;
CheckBox present;
EditText marks;
int pos;
public static ArrayList<String> studentNames,studentRollNo,studentsPresent,marksObtainedList;
public AssessmentAdapter(Context context, int textViewResourceId,ArrayList<String> studentNames,ArrayList<String> studentRollNo) {
super(context, textViewResourceId,studentNames);
// TODO Auto-generated constructor stub
this.context=context;
AssessmentAdapter.studentNames=studentNames;
AssessmentAdapter.studentRollNo=studentRollNo;
studentsPresent=new ArrayList<String>();
marksObtainedList=new ArrayList<String>();
Log.d("No. of students",""+studentRollNo.size());
for(int i=0;i<studentNames.size();i++)
{
studentsPresent.add("1");
marksObtainedList.add("0");
}
}
static class ViewHolder {
CheckBox presentCB;
TextView name,Rno;
EditText marksObtained;
Button save;
}
public View getView(final int pos,View convertview,ViewGroup parent)
{
try
{
holder=null;
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertview==null)
{
//rowView=inflater.inflate(org.example.attendance.R.layout.list_radio,parent,false);
convertview=inflater.inflate(R.layout.assessment,null);
holder=new ViewHolder();
holder.presentCB=(CheckBox)convertview.findViewById(org.example.attendance2.R.id.presentCB);
holder.name=(TextView)convertview.findViewById(org.example.attendance2.R.id.studNameTV);
holder.Rno=(TextView)convertview.findViewById(org.example.attendance2.R.id.studRollNoTV);
holder.marksObtained=(EditText)convertview.findViewById(org.example.attendance2.R.id.marksET);
holder.save=(Button)convertview.findViewById(org.example.attendance2.R.id.saveButton);
convertview.setTag(holder);
}
else
{
holder=(ViewHolder) convertview.getTag();
}
holder.name.setText(studentNames.get(pos));
holder.Rno.setText(studentRollNo.get(pos));
holder.save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
holder.marksObtained.addTextChangedListener(new TextWatcher(){
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
marksObtainedList.set(pos, arg0.toString());
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
});
holder.presentCB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(studentsPresent.get(pos).equals("1"))
{
studentsPresent.set(pos, "0");
}
else
{
studentsPresent.set(pos, "1");
}
}
});
//if(RadioChecked[pos])
if(studentsPresent.get(pos).equals("1"))
{
holder.presentCB.setChecked(true);
}
// else if(!RadioChecked[pos])
else if(studentsPresent.get(pos).equals("0"))
{
holder.presentCB.setChecked(false);
}
if(holder.marksObtained.getText().toString().equals(""))
{
marksObtainedList.set(pos,"0");
}
else
{
String marks=holder.marksObtained.getText().toString();
marksObtainedList.set(pos,marks);
Log.d(""+pos,marksObtainedList.get(pos));
}
holder.marksObtained.setText(marksObtainedList.get(pos));
}catch(Exception e)
{
}
return convertview;
}
public boolean areAllItemsEnabled()
{
return true;
}
#Override
public boolean isEnabled(int arg0)
{
return true;
}
}
the problem is all about relate to focus . see here , it may help you.
Focusable EditText inside ListView
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;
}
}