我尝试为库编写应用程序。我有一项活动,我看到所有书籍都有库存。我使用listView和baseAdapter。所有信息都保存在数据库中。
onCreate in Activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crown_library_details);
// INITIALIZATION VALUE
member = (Member) getIntent().getSerializableExtra(IntentExtra.MEMBER.toString());
book = (Book) getIntent().getSerializableExtra(IntentExtra.BOOK.toString());
title = (TextView) findViewById(R.id.title);
author = (TextView) findViewById(R.id.author);
title.setText(book.getTitle());
author.setText(book.getAuthor());
searchView = (SearchView) findViewById(R.id.searchView);
listView = (ListView) findViewById(R.id.listView);
new AsyncTask<Void, Void, Void>() {
@Override
protected void onPreExecute() {
}
@Override
protected Void doInBackground(Void... voids) {
bookShopArrayList = backend.getAllShopOfBookId(book.getBookID());
cart = backend.getCart(member.getMemberID());
if (member.isShop())
bookShopShopEditionAdapter = new BookShop_ShopEditionAdapter(CrownLibraryDetails.this,
bookShopArrayList, member.getMemberID(), book.getBookID(), AccessType.SHOP);
else
bookShopShopEditionAdapter = new BookShop_ShopEditionAdapter(CrownLibraryDetails.this,
bookShopArrayList, member.getMemberID(), book.getBookID(), AccessType.MEMBER);
bookShopShopEditionAdapter.addListener(CrownLibraryDetails.this);
return null;
}
@Override
protected void onPostExecute(Void list) {
listView.setAdapter(bookShopShopEditionAdapter);
}
}.execute();
// MENU
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setTitle(R.string.title_activity_crown_library);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (member.isShop()) {
Intent intent = new Intent(CrownLibraryDetails.this, BookDetailsForShop.class);
intent.putExtra(IntentExtra.BOOK_SHOP.toString(), bookShopShopEditionAdapter.getItem(i));
intent.putExtra(IntentExtra.PREVIOUS_ACTiVITY.toString(), IntentExtra.CROWN_LIBRARY_DETAILS.toString());
intent.putExtra(IntentExtra.MEMBER.toString(), member);
startActivity(intent);
} else {
Intent intent = new Intent(CrownLibraryDetails.this, BookDetailsForMember.class);
intent.putExtra(IntentExtra.BOOK_SHOP.toString(), bookShopShopEditionAdapter.getItem(i));
intent.putExtra(IntentExtra.MEMBER.toString(), member);
intent.putExtra(IntentExtra.PREVIOUS_ACTiVITY.toString(), IntentExtra.CROWN_LIBRARY_DETAILS.toString());
startActivity(intent);
}
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(final String s) {
if (member.isShop())
bookShopShopEditionAdapter = new BookShop_ShopEditionAdapter(CrownLibraryDetails.this,
bookShopArrayList, member.getMemberID(), book.getBookID(), AccessType.SHOP);
else
bookShopShopEditionAdapter = new BookShop_ShopEditionAdapter(CrownLibraryDetails.this,
bookShopArrayList, member.getMemberID(), book.getBookID(), AccessType.MEMBER);
bookShopShopEditionAdapter.addListener(CrownLibraryDetails.this);
bookShopShopEditionAdapter.getFilter().filter(s);
listView.setAdapter(bookShopShopEditionAdapter);
return false;
}
@Override
public boolean onQueryTextChange(final String s) {
if (member.isShop())
bookShopShopEditionAdapter = new BookShop_ShopEditionAdapter(CrownLibraryDetails.this,
bookShopArrayList, member.getMemberID(), book.getBookID(), AccessType.SHOP);
else
bookShopShopEditionAdapter = new BookShop_ShopEditionAdapter(CrownLibraryDetails.this,
bookShopArrayList, member.getMemberID(), book.getBookID(), AccessType.MEMBER);
bookShopShopEditionAdapter.addListener(CrownLibraryDetails.this);
bookShopShopEditionAdapter.getFilter().filter(s);
listView.setAdapter(bookShopShopEditionAdapter);
return false;
}
});
}
my baseAdpater:
public class BookShop_ShopEditionAdapter extends BaseAdapter implements Filterable {
ArrayList<BookShop> bookShopsList;
ArrayList<BookShop> searchStoreOrEdition;
LayoutInflater inflater;
Context context;
long memberID;
long bookID;
AccessType accessType;
Shop _shop;
private Backend backend = BackendFactory.getInstance(context);
public BookShop_ShopEditionAdapter(Context context, ArrayList<BookShop> list, long memberID, long bookID, AccessType accessType) {
this.bookShopsList = list;
this.context = context;
inflater = LayoutInflater.from(this.context);
this.memberID = memberID;
this.bookID = bookID;
this.accessType = accessType;
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(final CharSequence constraint) {
FilterResults fr = new FilterResults();
new AsyncTask<Void, Void, Void>() {
@Override
protected void onPreExecute() {
}
@Override
protected Void doInBackground(Void... voids) {
if (constraint.equals("")) {
searchStoreOrEdition = bookShopsList;
} else {
for (BookShop book_shopItem : bookShopsList) {
if (backend.getShopToID(book_shopItem.getShopID()).getShopName()
.toLowerCase().contains(constraint))
searchStoreOrEdition.add(book_shopItem);
else if (book_shopItem.getEdition().toLowerCase().contains(constraint))
searchStoreOrEdition.add(book_shopItem);
}
}
return null;
}
@Override
protected void onPostExecute(Void avoid) {
}
}.execute();
fr.count = searchStoreOrEdition.size();
fr.values = searchStoreOrEdition;
return fr;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null) {
bookShopsList = (ArrayList<BookShop>) results.values;
notifyDataSetChanged();
}
}
};
}
public interface StoreEditionAdapterListener {[...]}
private ArrayList<StoreEditionAdapterListener> listListener = new ArrayList<>();
public void addListener(StoreEditionAdapterListener aListener){[...]}
private void sendListenerBuy(BookShop item, int position) {[...]}
private void sendListenerDelete(BookShop item, int position) {[...]}
@Override
public int getCount() {[...]}
@Override
public BookShop getItem(int position){[...]}
@Override
public long getItemId(int position) {[...]}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
RelativeLayout layoutItem;
if (convertView == null) {
layoutItem = (RelativeLayout) inflater.inflate(R.layout.view_book_shop_shop_edition_adapter, parent, false);
} else {
layoutItem = (RelativeLayout) convertView;
}
final TextView shop = (TextView) layoutItem.findViewById(R.id.shop);
final TextView edition = (TextView) layoutItem.findViewById(R.id.edition);
final TextView format = (TextView) layoutItem.findViewById(R.id.format);
final TextView state = (TextView) layoutItem.findViewById(R.id.state);
final TextView price = (TextView) layoutItem.findViewById(R.id.price);
ImageView deleteIcon = (ImageView) layoutItem.findViewById(R.id.delete);
ImageView buyexpressicon = (ImageView) layoutItem.findViewById(R.id.buyExpressIcon);
TextView buyexpress = (TextView) layoutItem.findViewById(R.id.buyExpress);
new AsyncTask<Void, Void, Void>() {
@Override
protected void onPreExecute() {
}
@Override
protected Void doInBackground(Void... voids) {
_shop = new Shop(backend.getShopToID(bookShopsList.get(position).getShopID()));
return null;
}
@Override
protected void onPostExecute(Void avoid) {
shop.setText(_shop.getShopName());
}
}.execute();
edition.setText(bookShopsList.get(position).getEdition());
format.setText(bookShopsList.get(position).getFormat().toString());
state.setText(bookShopsList.get(position).getState().toString());
price.setText(String.format("%s$", bookShopsList.get(position).getPriceBook()));
if (accessType.equals(AccessType.DIRECTOR_DELETE)) {//Director
deleteIcon.setVisibility(View.VISIBLE);
buyexpress.setVisibility(View.GONE);
buyexpressicon.setVisibility(View.GONE);
} else if (accessType.equals(AccessType.SHOP)) {
buyexpress.setVisibility(View.GONE);
buyexpressicon.setVisibility(View.GONE);
deleteIcon.setVisibility(View.GONE);
} else
deleteIcon.setVisibility(View.GONE);
//Click
buyexpressicon.setTag(position);
buyexpressicon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Integer position = (Integer) v.getTag();
sendListenerBuy(bookShopsList.get(position), position);
}
});
deleteIcon.setTag(position);
deleteIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Integer position = (Integer) v.getTag();
sendListenerDelete(bookShopsList.get(position), position);
}
});
return layoutItem;
}
}
当时,我在searchBar中写入,然后调用baseAdapter的函数过滤器。应用程序停止运行,没有任何说明。
01-21 08:50:51.512 19914-19914/? D/dalvikvm: Late-enabling CheckJNI
01-21 08:50:51.556 19914-19920/? D/dalvikvm: Debugger has detached; object registry had 1 entries
01-21 08:50:51.800 19914-19914/? W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
01-21 08:50:51.804 19914-19914/? I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onSearchRequested
01-21 08:50:51.804 19914-19914/? W/dalvikvm: VFY: unable to resolve interface method 18302: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
01-21 08:50:51.804 19914-19914/? D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
01-21 08:50:51.808 19914-19914/? I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onWindowStartingActionMode
01-21 08:50:51.808 19914-19914/? W/dalvikvm: VFY: unable to resolve interface method 18306: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
01-21 08:50:51.808 19914-19914/? D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
01-21 08:50:51.844 19914-19914/? I/dalvikvm: Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawable
01-21 08:50:51.844 19914-19914/? W/dalvikvm: VFY: unable to resolve virtual method 780: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
01-21 08:50:51.844 19914-19914/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
01-21 08:50:51.844 19914-19914/? I/dalvikvm: Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawableForDensity
01-21 08:50:51.844 19914-19914/? W/dalvikvm: VFY: unable to resolve virtual method 782: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
01-21 08:50:51.844 19914-19914/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
01-21 08:50:51.888 19914-19917/? D/dalvikvm: GC_CONCURRENT freed 195K, 3% free 10932K/11207K, paused 11ms+1ms, total 20ms
01-21 08:50:51.944 19914-19914/? D/libEGL: loaded /system/lib/egl/libEGL_genymotion.so
01-21 08:50:51.944 19914-19914/? D/: HostConnection::get() New Host Connection established 0xb80b6c50, tid 19914
01-21 08:50:51.956 19914-19914/? D/libEGL: loaded /system/lib/egl/libGLESv1_CM_genymotion.so
01-21 08:50:51.956 19914-19914/? D/libEGL: loaded /system/lib/egl/libGLESv2_genymotion.so
01-21 08:50:52.024 19914-19914/? W/EGL_genymotion: eglSurfaceAttrib not implemented
01-21 08:50:52.036 19914-19914/? D/OpenGLRenderer: Enabling debug mode 0
01-21 08:50:52.300 19914-19914/? D/OpenGLRenderer: TextureCache::get: create texture(0xb80c93b8): name, size, mSize = 4, 2200, 2200
01-21 08:50:52.356 19914-19914/? D/OpenGLRenderer: TextureCache::get: create texture(0xb80c7fe0): name, size, mSize = 5, 2200, 4400
01-21 08:50:52.540 19914-19914/? D/OpenGLRenderer: TextureCache::get: create texture(0xb80e30f0): name, size, mSize = 11, 4096, 8496
01-21 08:50:52.588 19914-19914/com.example.lilo.ebookstore I/Choreographer: Skipped 32 frames! The application may be doing too much work on its main thread.
答案 0 :(得分:1)
不需要在QueryTextListener中再次设置适配器。如果您的过滤器正在运行,除此之外,您无需执行任何操作:
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(final String s) {
return false;
}
@Override
public boolean onQueryTextChange(final String s) {
bookShopShopEditionAdapter.getFilter().filter(s);
bookShopShopEditionAdapter.notifydatasetchanged();
return false;
}
});
此外,您不需要在过滤器中运行asynctask来进行过滤。
在listview中使用过滤器的示例:
http://www.survivingwithandroid.com/2012/10/android-listview-custom-filter-and.html