【发布时间】:2016-01-31 07:29:10
【问题描述】:
我是 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);
}
【问题讨论】:
标签: android search android-listview