0

how do i add Search Functionality in my android app? i have a listview in which i want to add search functionality , i have the code but i dont know how do i integrate it properly with my app here is the code -

inputSearch.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
    // When user changed the Text
    MainActivity.this.adapter.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                          
}
});

and here is my java file

package com.Example.app;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

private ListView list1;

private String array[] = { "Iphone", "Tutorials", "Gallery", "Android",

"item 1", "item 2", "item3", "item 4", "item 1", "item 2", "item3", "item 4","item 1",        "item 2", "item3", "item 4"};


   @Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

list1 = (ListView) findViewById(R.id.ListView01);

// By using setAdpater method in listview we an add string array in

// list.

list1.setAdapter(new ArrayAdapter(this,

android.R.layout.simple_list_item_1, array));

list1.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
     if (position == 1)
    {
      Intent myIntent = new Intent(getApplicationContext(),Baba.class);
      startActivity(myIntent);
    }
  }
});
}

}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stackFromBottom="true">
<EditText 
android:id="@+id/inputSearch"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:hint="Search"
android:inputType="textVisiblePassword">
</EditText>
<ListView
    android:id="@+id/ListView01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:scrollbarSize="5dp" 

    >

</ListView>

</LinearLayout>
7
  • androidhive.info/2012/09/…. check this blog Commented Dec 3, 2013 at 15:32
  • so what is the problem then? Commented Dec 3, 2013 at 15:33
  • What do you mean by own adapter? A custom adapter?? Commented Dec 3, 2013 at 15:35
  • i see no custom adapter in your code. Also where is edittext. i gues you have not followed the blog full Commented Dec 3, 2013 at 15:36
  • dude then what is the problem in initializing edittext in onCreate and use the first part of the code in your post in onCreate. Commented Dec 3, 2013 at 15:39

2 Answers 2

1

Try the below

EditText inputSearch; 
ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputSearch = (EditText) findViewById(R.id.inputSearch); // initialize edittext 
list1 = (ListView) findViewById(R.id.ListView01);
adapter =new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, array));
list1.setAdapter(adapter);
inputSearch.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
    // When user changed the Text
    MainActivity.this.adapter.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                          
}
});
list1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
     if (position == 1)
    {
      Intent myIntent = new Intent(getApplicationContext(),Baba.class);
      startActivity(myIntent);
    }
  }
});
}

Initialize editText inputSearch in onCreate.

inputSearch = (EditText) findViewById(R.id.inputSearch); // initialize edittext 

Initialize adapter

adapter =new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, array));

Set the adapter op listview

list1.setAdapter(adapter);

This is where the search happens

 @Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
    // When user changed the Text
    MainActivity.this.adapter.getFilter().filter(cs);   // filter based on input
}
Sign up to request clarification or add additional context in comments.

10 Comments

@96 pls check the edit. pls make an effort to understand then copy paste the code
@96 what switch statement. you are totally confused. i don;t understand where is switch statement?? what is your requirement. You never mentioned anything in your post. But you expect a proper answer?
@96 i really don't understand what you are talking about. Seriously what string?
which code are you talking about. copy paste the whole code of onCreate. can't you even see the changes made? replace yours with mine
@96 becasue your code does not have that function. in onItemClick. you need to have the code that starts a new activity based on list item click. i see only this if (position == 1) { Intent myIntent = new Intent(getApplicationContext(),Baba.class); startActivity(myIntent); }. So how can you say the code is useless without understanding why it won't work
|
0

Since you are using android's array adapter, you can use its getFilter() method too. So you just have to change minor things in your code.

Instead of setting array adapter like this

list1.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1, array));

Create a private variable outside of the methods

private ArrayAdapter adapter;

Then initialize and set it

adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, array);
list1.setAdapter(adapter);

Now text changed method can call MainActivity.this.adapter.getFilter().filter(cs); without any problem

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.