I'm new in Android and i need to make a search function for listview. Please tell me how to write the code and where do i put it. This is my listview java class, layout file and Dbhelper class.
View_Guest.java
public class View_Guest extends AppCompatActivity implements View.OnClickListener {
ListView guestList;
DBhelper helper;
SQLiteDatabase db;
Toolbar toolbar;
String selected_id;
String gt;
SimpleCursorAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_guest);
toolbar = (Toolbar)findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
guestList = (ListView) findViewById(R.id.lblguestlist);
guestList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(final AdapterView<?> arg0, View arg1, final int position, long arg3) {
Cursor row1= (Cursor) arg0.getItemAtPosition(position);
selected_id = row1.getString(0);
gt=row1.getString(0);
Intent myIntent = new Intent(View_Guest.this, Guest.class);
myIntent.putExtra("data key", gt);
startActivity(myIntent);
}
});
helper = new DBhelper(this);
fetchdata();
}
public void fetchdata(){
db = helper.getReadableDatabase();
Cursor c = db.query(DBhelper.TABLE, null, null, null, null, null, null);
adapter = new SimpleCursorAdapter(
this,
R.layout.guest_row,
c,
new String[]{DBhelper.C_NAME, DBhelper.C_COUNT, DBhelper.C_INVITE},
new int[]{R.id.guestrowname, R.id.guestrowcount, R.id.guestrowinvite});
guestList.setAdapter(adapter);
}
activity_view_guest.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ddk.weddingplanner.View_Guest">
<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar"></include>
<TextView
android:layout_width="200dp"
android:layout_height="40dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Guest Name"
android:id="@+id/guestrowname"
android:layout_marginLeft="10dp"
android:layout_below="@+id/tool_bar" />
<!--android:textColor="@drawable/abc_item_background_holo_dark"-->
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Count"
android:id="@+id/guestrowcount"
android:layout_marginLeft="10dp"
android:layout_toEndOf="@+id/guestrowname"
android:layout_toRightOf="@+id/guestrowname"
android:layout_below="@+id/tool_bar"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Invite"
android:id="@+id/guestrowinvite"
android:layout_marginLeft="40dp"
android:layout_toEndOf="@+id/guestrowcount"
android:layout_toRightOf="@+id/guestrowcount"
android:layout_below="@+id/tool_bar"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lblguestlist"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/guestrowname" />
<SearchView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/searchView"
android:layout_alignBottom="@+id/tool_bar"
android:layout_centerHorizontal="true" />
DBhelper.java
public class DBhelper extends SQLiteOpenHelper {
static final String DATABASE = "wedding4.db";
static final int VERSION = 4;
static final String TABLE = "guestlist";
static final String C_ID = "_id";
static final String C_NAME = "name";
static final String C_SIDE = "side";
static final String C_INVITE = "invite";
static final String C_COUNT = "count";
static final String C_ATTEND = "attend";
static final String C_ALCOHOL = "alcohol";
public DBhelper(Context context) {
super(context, DATABASE, null, VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE + "(" + C_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT," + C_NAME + " text,"
+ C_SIDE + " text," + C_INVITE + " text," + C_COUNT + " text,"
+ C_ATTEND + " text," + C_ALCOHOL + " text )");
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table " + TABLE);
onCreate(db);
}
Try this code:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- Editext for Search -->
<EditText android:id="@+id/inputSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Search products.."
android:inputType="textVisiblePassword"/>
<!-- List View -->
<ListView
android:id="@+id/list_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
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" >
<!-- Single ListItem -->
<!-- Product Name -->
<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>
Now open your MainActivity.java and paste the following code to create a simple ListView . In the following code i stored all the list data in an array called products[] and attached to listview using simple ArrayAdapter .
MainActivity.java
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 {
// List view
private ListView lv;
// Listview Adapter
ArrayAdapter<String> adapter;
// Search EditText
EditText inputSearch;
// ArrayList for Listview
ArrayList<HashMap<String, String>> productList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Listview Data
String products[] = {"Item1", "Item2", "Item3", "Item4", "Item5",
"Item6", "Item7",
"Item8", "Item9", "Item10", "Item11"};
lv = (ListView) findViewById(R.id.list_view);
inputSearch = (EditText) findViewById(R.id.inputSearch);
// Adding items to listview
adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);
lv.setAdapter(adapter);
}
}
Enabling Search Functionality
Search functionality can be enabled by writing simple lines of code. All you need to do is adding addTextChangedListener to EditText . Once user enters a new data in EditText we need to get the text from it and passing it to array adapter filter. All the following code in your MainActivity.java
Add this method to the MainActivity.java
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
}
});
Finally add the following property in your AndroidManifest.xml file to hide the keyboard on loading Activity.
android:windowSoftInputMode="stateHidden"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.