我是Android的新手,我需要为listview创建搜索功能。请告诉我如何编写代码以及我在哪里编写代码。这是我的listview java类,布局文件和Dbhelper类。
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);
}
答案 0 :(得分:0)
试试这段代码:
<强> 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>
现在打开 MainActivity.java 并粘贴以下代码以创建简单的 ListView 。在以下代码中,我将所有列表数据存储在名为 products [] 的数组中,并使用简单的 ArrayAdapter 附加到 listview 。
<强> 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);
}
}
启用搜索功能
可以通过编写简单的代码行来启用搜索功能。您需要做的就是将 addTextChangedListener 添加到 EditText 。一旦用户在EditText中输入新数据,我们需要从中获取文本并将其传递给数组适配器过滤器。 MainActivity.java
中的以下所有代码将此方法添加到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
}
});
最后在 AndroidManifest.xml 文件中添加以下属性,以便在加载活动时隐藏键盘。
android:windowSoftInputMode="stateHidden"