Hello everybody I'm doing an app which has an edittext to search for items on the listview. If the user types a letter. The data come from my json string (database) and then display on my listview. So far this is what I've tried:
ListViewAdapter adapter2;
ArrayList<HashMap<String, String>> arraylist;
ArrayList<String> list = new ArrayList<String>();
wsSearch.addTextChangedListener(new TextWatcher (){
public void afterTextChanged(Editable cs) {
// 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
//BAPTISMAL_SONG.this.adapter2.getFilter().filter(cs);
String searchString = cs.toString();
if(searchString.length() != 2) {
adapter2 = new ListViewAdapterBaptismal(BAPTISMAL_SONG.this, arraylist);
listview.setAdapter(adapter2);
return;
}
ArrayList<HashMap<String, String>> arrayTemplist = new ArrayList<HashMap<String,String>>();
for (int i = 0; i < arraylist.size(); i++)
{
String currentString = arraylist.get(i).get(BAPTISMAL_SONG.TAG_TITLE);
if (searchString.equalsIgnoreCase(currentString))
{
arrayTemplist.add(arraylist.get(i));
}
}
adapter2 = new ListViewAdapterBaptismal(BAPTISMAL_SONG.this, arrayTemplist);
listview.setAdapter(adapter2);
}
});
@Override
protected void onPostExecute(Void args) {
// Locate the listview in listview_main.xml
// Pass the results into ListViewAdapter.java
adapter2 = new ListViewAdapter(Activity2.this, arraylist);
// Binds the Adapter to the ListView
listview.setAdapter(adapter2);
// Close the progressdialog
mProgressDialog.dismiss();
}
}
What I want to achieve, if the user types a letter like B, all the items that start with the said letter should be filtered. But using the code I posted above, it does not do exactly what I want. It just filters whenever I typed the whole item name. Any ideas? Your help will be greatly appreciated. Thanks.
I got complete solution, i put filter method in custom adapter for multiple fields search functionality.
Note: for image loading i took lazy loading so you need ImageLoader.java, FileCache.java, Utils.java and MemoryCache.java.
Check bellow code.....
list_layout.xml ( layout folder )
custom_row.xml ( layout folder )
put sample.json file in assets folder
sample.json
ListActivity.java ( src folder )
SampleData.java ( src folder )
add following permissions in AndroidManifest.xml file
thats it..
enjoy coding
use this `onTextChanged() method, might be it will help you.
//Main Activity
Then ListActivity.class
//Custom Adapter
}
//and Model
}
You could give this a try:
Then Finally Register
Editext
here:Hope this could help you ...
Update your own code only:
The code below maybe help you.
As far as I can see you are only checking if the two Strings match exactly. You might want to use boolean startsWith(String prefix)
Which would make something like