【发布时间】:2015-10-29 19:29:43
【问题描述】:
我想在 MainActivity.java 中将搜索视图添加到我的列表视图中,如何?
activity_main.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"
android:orientation="horizontal"
tools:context=".MainActivity">
<SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/searchView1"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:queryHint="search"/>
<ListView
android:id="@+id/listview"
android:layout_below="@+id/searchView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
MainActivity.java
public class MainActivity extends ListActivity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// 1. pass context and data to the custom adapter
MyAdapter adapter = new MyAdapter(this, generateData());
//2. setListAdapter
setListAdapter(adapter);
}
private ArrayList < Item > generateData() {
ArrayList < Item > items = new ArrayList < Item > ();
items.add(new Item("item1", "description1"));
items.add(new Item("item2", "description2"));
items.add(new Item("item3", "description3"));
return items;
}@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu); //Menu Resource, Menu
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.about:
Intent intent = new Intent(this, about.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
}
【问题讨论】:
标签: java android listview search