Search in ListView with EditText
follow the link
http://marakana.com/forums/android/learning_android_book/617.html
http://androidsearchfilterlistview.blogspot.in/2011/06/android-custom-list-view-filter.html
How can I filter ListView data when typing on EditText in android
How to dynamically update a ListView on Android
Android Listview Searching from edittext
You can do this in following steps.
- Check that that word user entered is available in your listview.
- If yes then get id or position of that word in array list or your listview respectively.
- then set selection to retrieved id.
if you are still not able to get id then pleas post your code you are using string array custom array adapter
Sol:
In your case it could be like
this code should be in your oncreate or wherever you are initlizing your listview
HashMap<String,String> map=new HashMap<String, String>();
for(int i=0; i<arrlist.size();i++)
{
map.put(arrlist.get(i).trim().toLowerCase(), ""+i+1);
}
to get value from hash map
this code will on your search button click
String id= map.get("yourserchitem")
lv.setSelection(Integer.parseInt(id)-1);
on text changer write this code
here e is object of my editext
e.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
if(arr.contains(e.getText().toString().trim().toLowerCase()))
{
String id= map.get(e.getText().toString().trim().toLowerCase());
lv.setSelection(Integer.parseInt(id)-1);
}
}
});
change code inside afterTextChanged with
String middle_string="";
for(int i=0;i<arr.size();i++)
{
if(arr.get(i).startsWith(e.getText().toString().trim().toLowerCase()))
{
middle_string=arr.get(i);
break;
}
}
if(arr.contains(middle_string))
{
String id= map.get(middle_string);
lv.setSelection(Integer.parseInt(id)-1);
}
Searching in a listview using a Custom Adapter with text and images
This is what you have to do inside your adapter, you might need to do some tweaks:-
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
// If the constraint (search string/pattern) is null
// or its length is 0, i.e., its empty then
// we just set the `values` property to the
// original contacts list which contains all of them
if (constraint == null || constraint.length() == 0) {
results.values = videoTitle;;
results.count = videoTitle.size();
}
else {
// Some search copnstraint has been passed
// so let's filter accordingly
ArrayList<String> filteredTitle = new ArrayList<String>();
// We'll go through all the title and see
// if they contain the supplied string
for (String c : string) {
if (string.toUpperCase().contains( constraint.toString().toUpperCase() )) {
// if `contains` == true then add it
// to our filtered list
filteredTitle.add(c);
}
}
// Finally set the filtered values and size/count
results.values = filteredTitle;
results.count = filteredTitle.size();
}
// Return our FilterResults object
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
mList = (ArrayList<String>) results.values;
notifyDataSetChanged();
}
};
}
Hope this will help you.
Using an EditText to search through a ListView
Few Logical Mistakes : I got here this code working at my end
public LabAdapter(Activity activity, List<LabItem> labItem){
this.activity = activity;
this.labItem = labItem;
}
@Override
public int getCount() {
return labItem.size();
}
@Override
public LabItem getItem(int position) { //instead of Object make it Labitem
return labItem.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
if (inflater == null)
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.activity_stack, null);
labName = (TextView) convertView.findViewById(R.id.textView1);
labCase = (TextView) convertView.findViewById(R.id.textView2);
LabItem labs = labItem.get(position);
labName.setText(labs.getName());
labCase.setText(labs.getCase());
return convertView;
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
Log.d("SEARCHTEXT2", "**** PERFORM FILTERING for: " + charSequence);
charSequence = charSequence.toString().toLowerCase();
FilterResults filts = new FilterResults();
if (charSequence == null || charSequence.length() == 0){
filts.values = labItem;
filts.count = labItem.size();
} else {
// We perform filtering operation
List<LabItem> labrs = new ArrayList<LabItem>();
for (LabItem l : labItem){ //you are going to search in labItem that contains the data and not the empty list labrs
if (l.getName().startsWith(charSequence.toString()) || l.getCase().startsWith(charSequence.toString())){
Log.e("(l.getName().startsWith(charSequence.toString())","" + charSequence);
labrs.add(l); // add to the new list**
}
}
filts.values = labrs; //set values to the new list**
filts.count = labrs.size();
//filts.count = labItem.size();
}
return filts;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
// Now we have to inform the adapter about the new list filtered
Log.d("SEARCHTEXT1", "**** PUBLISHING RESULTS for: " + charSequence);
List<LabItem> filtered = (List<LabItem>) filterResults.values;
labItem = filtered; // set the new data as you want with the new set you've received.**
notifyDataSetChanged();
}
};
}
public void notifyDataSetInvalidated()
{
super.notifyDataSetInvalidated();
}
then in your LabDetailActivity.class change it as
searchText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
labListAdapter.getFilter().filter(charSequence.toString());
Log.d("NEW TAGS", "*** Search value changed: " + labListAdapter.getCount());
list.invalidate();
labListAdapter.notifyDataSetChanged();
list.setAdapter(labListAdapter);
}
Edit:
P.S. This code is still not perfect as after clearing the text it
doesn't sets the list back. I leave it to you. Hope this solves your
problem you've been facing.
Let me know if it works.
Related Topics
How to Give Hexagon Shape to Imageview
Google Glass Gdk: How to Communicate with Android Device
Adding Local .Aar Files to My Gradle Build
Phonegap Eclipse Issue - Eglcodeccommon Glutilsparamsize: Unknow Param Errors
How to Hide System Navigation Bar in Tablets
Listview in Arrayadapter Order Get's Mixed Up When Scrolling
Customize Check Box Preference
Drawing Multiple Lines in Edittext E.G. Notepad
Android:How to Capture a Image of Gps Location
Play Sound on Button Click Android
Android Contacts Display Name and Phone Number(S) in Single Database Query
Overlay Images Onto Camera Preview Surfaceview
How to Know That My Webview Is Loaded 100%
Android Vector Drawable App:Srccompat Not Showing Images