Android listview with search function using aide ide
Tutorials on how to create android listview with search function using aide ide, this is a basic tutorial and easy to use for beginners.
Project name: any
Package: any
Open your created project and goto res/layout/main.xml paste this following codes:
<linearlayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
<edittext android:hint="Search products.." android:id="@+id/inputSearch" android:inputtype="textVisiblePassword" android:layout_height="wrap_content" android:layout_width="fill_parent">
<listview android:id="@+id/list_view" android:layout_height="wrap_content" android:layout_width="fill_parent">
</listview>
</edittext>
</linearlayout>
Create new xml files and name to "list_item.xml"
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >
<textview android:id="@+id/product_name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="10dip" android:textSize="16dip" android:textStyle="bold"/>
</LinearLayout>
Goto your java files and open "MainActivity.jave"
import java.util.ArrayList; import java.util.HashMap; import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.ListView;
public class MainActivity extends Activity {
Adapter ArrayAdapter<string> adapter;
EditText EditText inputSearch;
Listview ArrayList<HashMap<String, String>> productList; @Override public void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
String products[] = {"Aa files", "Bb files", "Cc files", "Dd files", "Ee files", "Ff files", "Gg files", "Hh files", "Ii files", "Jj files", "kk files"};
lv = (ListView) findViewById(R.id.list_view);
inputSearch = (EditText) findViewById(R.id.inputSearch);
adapter = new ArrayAdapter<string>(this, R.layout.list_item, R.id.product_name, products); lv.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 } }); }
}
Done, and run code.
Comments
Post a Comment
Leave a comment hdh