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>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…