我在操作栏中完成了一次搜索查看。
但是当我输入搜索时,理论上listview必须刷新,但事实并非如此。我知道它没问题,因为我“打印”了光标的内容。
可能我错过了一些东西,但我找不到它。
代码:
方法“doFilter()”:
public synchronized void doFilter(String filterString) {
mSearchString = filterString;
if (nSearchChatViewPagerAdapter == null) {
nSearchChatViewPagerAdapter = new ChatViewPagerAdapter(getSupportFragmentManager());
mChatPager.setAdapter(nSearchChatViewPagerAdapter);
mLoaderCallbacks = new MyLoaderCallbacks();
getSupportLoaderManager().initLoader(CHAT_PAGE_LOADER_ID , null, mLoaderCallbacks);
} else {
if (!mAwaitingUpdate)
{
mAwaitingUpdate = true;
mHandler.postDelayed(new Runnable ()
{
public void run ()
{
getSupportLoaderManager().restartLoader(CHAT_PAGE_LOADER_ID , null, mLoaderCallbacks);
mAwaitingUpdate = false;
}
},1000);
}
}
}
LoaderCallback:
class MyLoaderCallbacks implements LoaderCallbacks<Cursor> {
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
StringBuilder buf = new StringBuilder();
if (mSearchString != null) {
buf.append('(');
buf.append("contacts.username");
buf.append(" LIKE ");
android.database.DatabaseUtils.appendValueToSql(buf, "%" + mSearchString + "%");
buf.append(" OR ");
buf.append("contacts.nickname");
buf.append(" LIKE ");
android.database.DatabaseUtils.appendValueToSql(buf, "%" + mSearchString + "%");
buf.append(')');
}
CursorLoader loader = new CursorLoader(NewChatActivity.this, Imps.Contacts.CONTENT_URI_CHAT_CONTACTS, ChatView.CHAT_PROJECTION,
buf == null ? null : buf.toString(), null, Imps.Contacts.DEFAULT_SORT_ORDER);
loader.setUpdateThrottle(50L);
return loader;
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor newCursor) {
nSearchChatViewPagerAdapter.swapCursor(newCursor);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
nSearchChatViewPagerAdapter.swapCursor(null);
}
}
适配器:
public class ChatViewPagerAdapter extends DynamicPagerAdapter {
Cursor mCursor;
boolean mDataValid;
public ChatViewPagerAdapter(FragmentManager fm) {
super(fm);
}
public Cursor getCursor() {
return mCursor;
}
public Cursor swapCursor(Cursor newCursor) {
if (newCursor == mCursor) {
return null;
}
Cursor oldCursor = mCursor;
mCursor = newCursor;
Log.v("MIAAAAAAAAAAAU", android.database.DatabaseUtils.dumpCursorToString(mCursor));
if (newCursor != null) {
mDataValid = true;
// notify the observers about the new cursor
refreshChatViews();
} else {
mDataValid = false;
}
notifyDataSetChanged();
return oldCursor;
}
@Override
public int getCount() {
if (mCursor != null)
return mCursor.getCount() + 1;
else
return 0;
}
@Override
public Fragment getItem(int position) {
if (position == 0)
{
if (mContactList == null)
mContactList = new ContactListFragment();
return mContactList;
}
else
{
int positionMod = position - 1;
mCursor.moveToPosition(positionMod);
long contactChatId = mCursor.getLong(ChatView.CONTACT_ID_COLUMN);
String contactName = mCursor.getString(ChatView.USERNAME_COLUMN);
long providerId = mCursor.getLong(ChatView.PROVIDER_COLUMN);
return ChatViewFragment.newInstance(contactChatId, contactName, providerId);
}
}
@Override
public int getItemPosition(Object object) {
if (object instanceof ChatViewFragment)
{
ChatViewFragment cvFrag = (ChatViewFragment)object;
ChatView view = cvFrag.getChatView();
long viewChatId = view.mLastChatId;
int position = PagerAdapter.POSITION_NONE;
// TODO: cache positions so we don't scan the cursor every time
if (mCursor != null && mCursor.getCount() > 0)
{
mCursor.moveToFirst();
int posIdx = 1;
do {
long chatId = mCursor.getLong(ChatView.CHAT_ID_COLUMN);
if (chatId == viewChatId)
{
position = posIdx;
break;
}
posIdx++;
}
while (mCursor.moveToNext());
}
//` Log.d(TAG, "position of " + cvFrag.getArguments().getString("contactName") + " = " + position);
return position;
}
else if (object instanceof ContactListFragment)
{
return 0;
}
else {
throw new RuntimeException("got asked about an unknown fragment");
}
}
@Override
public CharSequence getPageTitle(int position) {
if (position == 0 || mCursor == null)
{
if (mShowChatsOnly)
return getString(R.string.title_chats);
else
return getString(R.string.contacts);
}
else
{
int positionMod = position - 1;
mCursor.moveToPosition(positionMod);
if (!mCursor.isAfterLast())
{
String nickname = mCursor.getString(ChatView.NICKNAME_COLUMN);
int presence = mCursor.getInt(ChatView.PRESENCE_STATUS_COLUMN);
int type = mCursor.getInt(ChatView.TYPE_COLUMN);
BrandingResources brandingRes = mApp.getBrandingResource(mCursor.getInt(ChatView.PROVIDER_COLUMN));
SpannableString s = null;
Drawable statusIcon = null;
if (Imps.Contacts.TYPE_GROUP == type)
{
s = new SpannableString(nickname);
}
else
{
s = new SpannableString("+ " + nickname);
statusIcon = brandingRes.getDrawable(PresenceUtils.getStatusIconId(presence));
statusIcon.setBounds(0, 0, statusIcon.getIntrinsicWidth(),
statusIcon.getIntrinsicHeight());
s.setSpan(new ImageSpan(statusIcon), 0, 1, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return s;
}
else
return "";//unknown title
}
}
@Override
public Object instantiateItem(ViewGroup container, int pos) {
Object item = super.instantiateItem(container, pos);
if (pos > 0) {
ChatViewFragment frag = (ChatViewFragment)item;
}
return item;
}
@Override
public void destroyItem(ViewGroup container, int pos, Object object) {
super.destroyItem(container, pos, object);
}
public ChatView getChatViewAt(int pos) {
if (pos > 0)
{
ChatViewFragment frag = ((ChatViewFragment)getItemAt(pos));
if (frag != null)
return frag.getChatView();
}
return null; //this can happen if the user is quickly closing chats; just return null and swallow the event
//throw new RuntimeException("could not get chat view at " + pos);
}
}
logcat的:
01-14 12:52:09.025: V/MIAAAAAAAAAAAU(21285): >>>>> Dumping cursor android.content.ContentResolver$CursorWrapperInner@307d2543
01-14 12:52:09.025: V/MIAAAAAAAAAAAU(21285): 0 {
01-14 12:52:09.025: V/MIAAAAAAAAAAAU(21285): _id=5
01-14 12:52:09.025: V/MIAAAAAAAAAAAU(21285): account=1
01-14 12:52:09.025: V/MIAAAAAAAAAAAU(21285): provider=1
01-14 12:52:09.025: V/MIAAAAAAAAAAAU(21285): username=prova@mail.es
01-14 12:52:09.025: V/MIAAAAAAAAAAAU(21285): nickname=Prova Prova
01-14 12:52:09.025: V/MIAAAAAAAAAAAU(21285): type=0
01-14 12:52:09.025: V/MIAAAAAAAAAAAU(21285): mode=0
01-14 12:52:09.025: V/MIAAAAAAAAAAAU(21285): last_unread_message=null
01-14 12:52:09.025: V/MIAAAAAAAAAAAU(21285): _id=5
01-14 12:52:09.025: V/MIAAAAAAAAAAAU(21285): subscriptionType=null
01-14 12:52:09.025: V/MIAAAAAAAAAAAU(21285): subscriptionStatus=null
01-14 12:52:09.025: V/MIAAAAAAAAAAAU(21285): avatars_data=NULL
01-14 12:52:09.025: V/MIAAAAAAAAAAAU(21285): }
01-14 12:52:09.025: V/MIAAAAAAAAAAAU(21285): <<<<<
我在列表中的2项的图像。我认为当我按下“P”或更多时,项目“melissak”必须dessapear。
答案 0 :(得分:1)
你走了:
我还必须告诉你,这种方式效率不高,但目前这种方式对我有用:
你的onCreateOptionsMenu中的:
inflater.inflate(R.menu.main, menu);
searchView = (SearchView)menu.findItem(R.id.search).getActionView();
searchView.setSubmitButtonEnabled(false);
searchView.setOnQueryTextListener(textChangeListener);
在班级:
SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener(){
@Override
public boolean onQueryTextChange(String newText){
if (TextUtils.isEmpty(newText)){
searchView.clearFocus();
CursorAdapter filterAdapter = (CursorAdapter) Cur;
filterAdapter.getFilter().filter(newText.toString());
}
else {
CursorAdapter filterAdapter = (CursorAdapter) Cur;
filterAdapter.getFilter().filter(newText.toString());
}
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
return true;
}
};
OnCreate中的任何地方:
Cur.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
return dbHelper.getDirectoryList(constraint);
}
});
这将进入您的DBAdapter:
public Cursor getDirectoryList(CharSequence constraint) {
open();
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder.setTables(DBAdapter.DATABASE_TABLE1);
String asColumnsToReturn[] = { DBAdapter.DATABASE_TABLE1 + "."
+ DBAdapter.id + "," + DBAdapter.DATABASE_TABLE1 + "."
+ DBAdapter.notificationfor + "," + DBAdapter.DATABASE_TABLE1 + "."
+ DBAdapter.datetonotify + "," + DBAdapter.DATABASE_TABLE1 + "."
+ DBAdapter.isextraordinary + "," + DBAdapter.DATABASE_TABLE1 + "."
+ DBAdapter.notificationdata + "," + DBAdapter.DATABASE_TABLE1 + "."
+ DBAdapter.primaryid + "," + DBAdapter.DATABASE_TABLE1 + "."
+ DBAdapter.type_notifications + "," + DBAdapter.DATABASE_TABLE1 + "."
+ DBAdapter.radio_type_notfi + "," + DBAdapter.DATABASE_TABLE1 + "."
+ DBAdapter.friend_name_not + "," + DBAdapter.DATABASE_TABLE1 + "."
+ DBAdapter.notif_image_Uri + "," + DBAdapter.DATABASE_TABLE1 + "."
+ DBAdapter.notifDob + "," + DBAdapter.DATABASE_TABLE1 + "."
+ DBAdapter.notificationdatefor };
if (constraint == null || constraint.length() == 0) {
String query = "SELECT * FROM Notifications ORDER BY SUBSTR(DATE('NOW'), 0)>SUBSTR(DateToNotify, 0), SUBSTR(DateToNotify, 0)";
return mDb.rawQuery(query, null);
} else {
String value = "%" + constraint.toString() + "%";
return mDb.query(DATABASE_TABLE1,asColumnsToReturn,"NotificationDateFor like ? OR NotificationDateFor like ? ORDER BY DateToNotify ASC ",new String[] { value, value }, null,null, null);
}
}
注意:返回mDb.query在这里非常重要。所以相应地修改你的查询。 Cur是您加载到ListView中的主光标。