有人可以帮我这么做吗?我想使用listview
从SearchView
搜索项目。这是我的代码
这是ListObat.java
package co.id.zsoft.apotikstore;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class ListObat extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_obat);
ArrayList<ItemDetails> image_details = GetSearchResults();
final ListView lv1 = (ListView) findViewById(R.id.listView);
lv1.setAdapter(new ItemListBaseAdapter(this, image_details));
lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Object o = lv1.getItemAtPosition(position);
ItemDetails obj_itemDetails = (ItemDetails) o;
Toast.makeText(ListObat.this, "You have chosen : " + " " + obj_itemDetails.getName(), Toast.LENGTH_LONG).show();
}
});
}
private ArrayList<ItemDetails> GetSearchResults(){
ArrayList<ItemDetails> results = new ArrayList<ItemDetails>();
ItemDetails item_details = new ItemDetails();
item_details.setName("Pizza");
item_details.setItemDescription("Spicy Chiken Pizza");
item_details.setPrice("RS 310.00");
item_details.setImageNumber(1);
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("Burger");
item_details.setItemDescription("Beef Burger");
item_details.setPrice("RS 350.00");
item_details.setImageNumber(2);
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("Pizza");
item_details.setItemDescription("Chiken Pizza");
item_details.setPrice("RS 250.00");
item_details.setImageNumber(3);
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("Burger");
item_details.setItemDescription("Chicken Burger");
item_details.setPrice("RS 350.00");
item_details.setImageNumber(4);
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("Burger");
item_details.setItemDescription("Fish Burger");
item_details.setPrice("RS 310.00");
item_details.setImageNumber(5);
results.add(item_details);
item_details = new ItemDetails();
item_details.setName("Mango");
item_details.setItemDescription("Mango Juice");
item_details.setPrice("RS 250.00");
item_details.setImageNumber(6);
results.add(item_details);
return results;
}
@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_list_obat, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是ItemDetails.java
package co.id.zsoft.apotikstore;
/**
* Created by rsr_unitedstate on 12/18/2015.
*/
public class ItemDetails {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getItemDescription() {
return itemDescription;
}
public void setItemDescription(String itemDescription) {
this.itemDescription = itemDescription;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public int getImageNumber() {
return imageNumber;
}
public void setImageNumber(int imageNumber) {
this.imageNumber = imageNumber;
}
private String name ;
private String itemDescription;
private String price;
private int imageNumber;
}
这是ItemListBaseAdapter.java
package co.id.zsoft.apotikstore;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.util.ArrayList;
/**
* Created by rsr_unitedstate on 12/18/2015.
*/
public class ItemListBaseAdapter extends BaseAdapter {
private static ArrayList<ItemDetails> itemDetailsrrayList;
private Integer[] imgid = {
R.drawable.testimage,
R.drawable.testimage,
R.drawable.testimage,
R.drawable.testimage,
R.drawable.testimage,
R.drawable.testimage
};
private LayoutInflater l_Inflater;
public ItemListBaseAdapter(Context context, ArrayList<ItemDetails> results) {
itemDetailsrrayList = results;
l_Inflater = LayoutInflater.from(context);
}
public int getCount() {
return itemDetailsrrayList.size();
}
public Object getItem(int position) {
return itemDetailsrrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = l_Inflater.inflate(R.layout.item_details_view, null);
holder = new ViewHolder();
holder.txt_itemName = (TextView) convertView.findViewById(R.id.name);
holder.txt_itemDescription = (TextView) convertView.findViewById(R.id.price);
holder.txt_itemPrice = (TextView) convertView.findViewById(R.id.price);
holder.itemImage = (ImageView) convertView.findViewById(R.id.photo);
holder.txt_qty = (TextView) convertView.findViewById(R.id.Qty);
holder.btnkurang = (ImageButton) convertView.findViewById(R.id.btnkurang);
holder.btntambah = (ImageButton) convertView.findViewById(R.id.btntambah);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txt_itemName.setText(itemDetailsrrayList.get(position).getName());
holder.txt_itemDescription.setText(itemDetailsrrayList.get(position).getItemDescription());
holder.txt_itemPrice.setText(itemDetailsrrayList.get(position).getPrice());
holder.itemImage.setImageResource(imgid[itemDetailsrrayList.get(position).getImageNumber() - 1]);
// imageLoader.DisplayImage("http://192.168.1.28:8082/ANDROID/images/BEVE.jpeg", holder.itemImage);
return convertView;
}
static class ViewHolder {
TextView txt_itemName;
TextView txt_itemDescription;
TextView txt_itemPrice;
TextView txt_qty;
ImageView itemImage;
ImageButton btntambah;
ImageButton btnkurang;
}
}
这是activity_list_obat.xml作为listview布局
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="co.id.zsoft.apotikstore.ListObat">
<SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ImageButton
android:layout_width="70dp"
android:layout_height="70dp"
android:id="@+id/imageButton"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
<ListView
android:layout_width="wrap_content"
android:layout_height="400dp"
android:id="@+id/listView"
android:layout_below="@+id/editText"
android:layout_alignParentEnd="true" />
</RelativeLayout>
这是item_details_view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:id="@+id/photo"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nama Obat"
android:id="@+id/name"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/photo" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Harga Obat"
android:id="@+id/price"
android:layout_below="@+id/name"
android:layout_toEndOf="@+id/photo" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nama Apotik"
android:id="@+id/itemDescription"
android:layout_below="@+id/price"
android:layout_toEndOf="@+id/photo" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:id="@+id/Qty"
android:layout_below="@+id/itemDescription"
android:layout_toEndOf="@+id/photo" />
<ImageButton
android:layout_width="20dp"
android:layout_height="20dp"
android:id="@+id/btnkurang"
android:layout_below="@+id/itemDescription"
android:layout_toEndOf="@+id/Qty"
android:src="@drawable/minus"/>
<ImageButton
android:layout_width="20dp"
android:layout_height="20dp"
android:id="@+id/btntambah"
android:layout_below="@+id/itemDescription"
android:layout_toEndOf="@+id/btnkurang"
android:src="@drawable/plus" />
</RelativeLayout>
答案 0 :(得分:2)
搜索视图位于操作栏中。因此需要设置菜单项。
/*******************
* Menu Items
******************/
/*
Menu added to perform search on the action bar. Two menu items:
2. Open the search area on the action bar so that the user can search.
*/
@Override
public void onCreateOptionsMenu(final Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.search, menu);
final MenuItem item = menu.findItem(R.id.action_search);
final SearchView sv = new SearchView(((MainActivity) getActivity()).getSupportActionBar().getThemedContext());
sv.setIconifiedByDefault(true);
sv.setFocusable(true);
sv.setIconified(false);
sv.clearFocus();
sv.requestFocusFromTouch();
MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
MenuItemCompat.setActionView(item, sv);
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
sv.setIconifiedByDefault(true);
sv.setFocusable(true);
sv.setIconified(false);
sv.requestFocusFromTouch();
return false;
}
});
sv.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
item.collapseActionView();
hideKeyboard();
return true;
}
});
MenuItemCompat.setOnActionExpandListener(menu.findItem(R.id.action_search), new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
//DO SOMETHING WHEN THE SEARCHVIEW IS CLOSING
simpleSearch = null;
sv.setQuery(null, false);
hideKeyboard();
return true;
}
});
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
System.out.println("search query submit");
return false;
}
@Override
public boolean onQueryTextChange(String searchString) {
System.out.println(searchString);
classSearchArray.clear();
if (searchString.length() > 0) {
simpleSearch = searchString.toLowerCase();
for (ClassModel classObj : classArray) {
if (classObj.getName().toLowerCase().contains(simpleSearch) ||
classObj.getCity().toLowerCase().contains(simpleSearch) ||
classObj.getStaffName().toLowerCase().contains(simpleSearch) ||
classObj.getLocationName().toLowerCase().contains(simpleSearch)) {
classSearchArray.add(classObj);
}
}
ClassAdapter adapter = new ClassAdapter(classSearchArray, getActivity(), FindAClassFragment.this);
listViewSearch.setAdapter(adapter);
listView.setVisibility(View.GONE); //original list view
listViewSearch.setVisibility(View.VISIBLE); //search list view
} else {
simpleSearch = null;
listView.setVisibility(View.VISIBLE);
listViewSearch.setVisibility(View.GONE);
}
return false;
}
});
}
菜单XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_search"
android:title="Search Items"
android:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
答案 1 :(得分:0)
试试这种方式
std::less
listitem.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>
MainActivity.java
<?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>