我是Android編程的初學者,我遇到了一個問題,我似乎無法找到解決方案。我搜索了幾個小時的stackoverflow,但我仍然不太清楚它應該怎麼做:如何使用searchview在自定義ArrayAdapter上實現搜索功能?
我創建了一個用php與本地xampp mysql服務器通信的slim api,並且我可以對它進行基本的CRUD操作。我從服務器獲取api的數據,它將數據庫中的所有「項目」返回給List,並且我可以在listView中使用它。每個列表項都有6個TextView,所以我創建了一個自定義的ArrayAdapter,但我不知道要搜索特定的列表項。
我的項目類:
public class Item {
private String mName;
private String mQuantity;
private String mDate_added;
private String mBarcode;
private String mPrice;
private String mWarranty;
private int mId;
public Item(String mName, String mQuantity, String mDate_added, String mBarcode, String mPrice, String mWarranty,int mId) {
this.mName = mName;
this.mQuantity = mQuantity;
this.mDate_added = mDate_added;
this.mBarcode = mBarcode;
this.mPrice = mPrice;
this.mWarranty = mWarranty;
this.mId = mId;
}
public int getmId(){return mId; }
public String getmName() {
return mName;
}
public String getmQuantity() {
return mQuantity;
}
public String getmDate_added() {
return mDate_added;
}
public String getmBarcode() {
return mBarcode;
}
public String getmPrice() {
return mPrice;
}
public String getmWarranty() {
return mWarranty;
}
}
我的適配器
public class ItemAdapter extends ArrayAdapter<Item> {
public ItemAdapter(Context context, ArrayList<Item> items) {
super(context,0, items);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View ListItemView= convertView;
if (ListItemView == null) {
ListItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item,parent,false);
Item currentItem = getItem(position);
TextView nameText = (TextView) ListItemView.findViewById(R.id.name);
TextView quantityText = (TextView) ListItemView.findViewById(R.id.quantity);
TextView date_addedText = (TextView) ListItemView.findViewById(R.id.date_added);
TextView barcodeText = (TextView) ListItemView.findViewById(R.id.barcode);
TextView priceText = (TextView) ListItemView.findViewById(R.id.price);
TextView warrantyText = (TextView) ListItemView.findViewById(R.id.warranty);
nameText.setText(currentItem.getmName());
quantityText.setText(currentItem.getmQuantity() +" e");
date_addedText.setText(currentItem.getmDate_added());
barcodeText.setText(currentItem.getmBarcode());
priceText.setText(currentItem.getmPrice()+ " pr/e");
warrantyText.setText(currentItem.getmWarranty());
}
return ListItemView;
}
}
我得到HttpURLConnection的get方法的數據,我用一個AsyncTaskLoader這一點。這會將數據返回給定URL中的List。
public class ItemLoader extends AsyncTaskLoader<List<Item>> {
private String mUrl;
public ItemLoader(Context context, String url) {
super(context);
mUrl = url;
}
@Override
protected void onStartLoading() {
forceLoad();
}
@Override
public List<Item> loadInBackground() {
if (mUrl == null) {
return null;
}
// Perform the network request, parse the response, and extract a list of items.
List<Item> items = QueryUtils.fetchItemData(mUrl);
return items;
}
}
接下來,在我的主要活動中,我實現了loader回調。
private ItemAdapter mAdapter;
@Override
public android.content.Loader<List<Item>> onCreateLoader(int i, Bundle bundle) {
return new ItemLoader(this,API_REQUEST_URL);
}
@Override
public void onLoadFinished(android.content.Loader<List<Item>> loader, List<Item> items) {
mAdapter.clear();
if (items != null && !items.isEmpty()) {
mAdapter.addAll(items);
}
}
@Override
public void onLoaderReset(android.content.Loader<List<Item>> loader) {
Log.e("init loader","Log");
mAdapter.clear();
}
在的onCreate
android.app.LoaderManager loaderManager = getLoaderManager();
loaderManager.initLoader(0, null, MainActivity.this);
listView.setAdapter(mAdapter);
我要搜索項目的名稱,並返回相應的在ListView。我創建了其與setOnQueryTextListener搜索視圖:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main,menu);
SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView search = (SearchView) menu.findItem(R.id.search).getActionView();
search.setSearchableInfo(manager.getSearchableInfo(getComponentName()));
search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
return false;
}
});
return true;
}
AAAND ......現在我有點卡住了。我知道我應該在適配器類上實現'Filterable',重寫一些方法,創建一個過濾器等,如果我有一個硬編碼List,我只需要更改它就不那麼困難,但是這些網絡請求,後臺線程,從服務器獲取數據讓我感到困惑,所以我不知道該怎麼做。
Halp! :c
接受答案,如果它幫助 – elmorabea