2015-05-09 76 views
1

在我的應用程序中。我想實現放置在操作欄中的searchview。我正在使用簡單的遊標適配器。簡單遊標適配器中有一個imageview和一個textview。我想在textview內容的基礎上實現搜索。但是我得到了空指針異常。如何在簡單光標適配器上實現搜索

我簡單的遊標適配器:

SimpleCursorAdapter c_adapter = new SimpleCursorAdapter(getActivity(), R.layout.contact_item, mCursor, 
     new String[]{ContactsContract.Contacts.DISPLAY_NAME 
       , ContactsContract.Contacts.PHOTO_THUMBNAIL_URI, ContactsContract.Contacts._ID}, 

     new int[]{R.id.username1, R.id.ivuserpicicon1}); 

而且我得到錯誤:

c_adapter.getFilter().filter(newText); 
+0

你可以添加你在 c_adapter.getFilter()。filter(newText);的錯誤棧跟蹤嗎? –

+0

問題很簡單,我們必須在該簡單遊標適配器或其他東西的textview上設置搜索。但是,如何? –

回答

1

爲此,我們必須使用setfilterqueryprovider(),並使用所需的查詢對特定列的選擇。

public class MainActivity extends ActionBarActivity { 

ListView listview; 
SimpleCursorAdapter c_adapter; 

protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 

Cursor mCursor = getContacts(); 
c_adapter = new SimpleCursorAdapter(MainActivity.this, R.layout.contact_item, mCursor, 
    new String[]{ContactsContract.Contacts.DISPLAY_NAME 
      , ContactsContract.Contacts.PHOTO_THUMBNAIL_URI, ContactsContract.Contacts._ID}, 
    new int[]{R.id.username1, R.id.ivuserpicicon1}); 

listview = (ListView) findViewById(R.id.listview); 
listview.setAdapter(c_adapter); 

c_adapter.setFilterQueryProvider(new FilterQueryProvider() { 
    public Cursor runQuery(CharSequence constraint) { 
     return getCursor(constraint.toString()); 
    } 
}); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu){ 
MenuInflater inflater = getMenuInflater(); 
inflater.inflate(R.menu.menu_main, menu); 
SearchManager SManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
MenuItem searchMenuItem = menu.findItem(R.id.action_search); 
android.support.v7.widget.SearchView searchViewAction = (android.support.v7.widget.SearchView) MenuItemCompat.getActionView(searchMenuItem); 
searchViewAction.setSearchableInfo(SManager.getSearchableInfo(getComponentName())); 
searchViewAction.setIconifiedByDefault(true); 
//////////////////////////////////////////////////////////////////////////////////// 
android.support.v7.widget.SearchView.OnQueryTextListener textChangeListener = new android.support.v7.widget.SearchView.OnQueryTextListener() 
{ 
    @Override 
    public boolean onQueryTextChange(String newText) 
    { 
     c_adapter.getFilter().filter(newText); 
     System.out.println("on text chnge text: " + newText); 
     return true; 
    } 
    @Override 
    public boolean onQueryTextSubmit(String query) 
    { 
     c_adapter.getFilter().filter(query); 
     System.out.println("on query submit: "+query); 
     return true; 
    } 
}; 
searchViewAction.setOnQueryTextListener(textChangeListener); 
/////////////////////////////////////////////////////////////////////////////////////// 
return super.onCreateOptionsMenu(menu); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
switch (item.getItemId()) { 
    case R.id.action_search: 
     //openSearch(); 
     return true; 
    default: 
     return super.onOptionsItemSelected(item); 
} 
} 

private Cursor getContacts() { 
    // Run query 
    Uri uri = ContactsContract.Contacts.CONTENT_URI; 
    String[] projection = new String[]{ContactsContract.Contacts.DISPLAY_NAME, 
      ContactsContract.Contacts.PHOTO_THUMBNAIL_URI, ContactsContract.Contacts._ID}; 
    String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP+" = "+ 
      1 +" AND "+ ContactsContract.Contacts.HAS_PHONE_NUMBER +" = "+ 1; 
    String[] selectionArgs = null; 
    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME 
      + " COLLATE LOCALIZED ASC"; 
    return getContentResolver().query(uri, projection, selection, selectionArgs, 
      sortOrder); 
} 

private Cursor getCursor(String str) { 
Cursor mCursor = null; 
if (str == null || str.length() == 0) { 
    mCursor = getContacts(); 
} 
else { 
    Uri uri = ContactsContract.Contacts.CONTENT_URI; 
    String[] projection = new String[]{ContactsContract.Contacts.DISPLAY_NAME, 
      ContactsContract.Contacts.PHOTO_THUMBNAIL_URI, ContactsContract.Contacts._ID}; 
    String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = " + 
      1 + " AND " + ContactsContract.Contacts.HAS_PHONE_NUMBER + " = " + 1 + " AND " + ContactsContract.Contacts.DISPLAY_NAME + " like '" + str + "%'"; 
    String[] selectionArgs = null; 
    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME 
      + " COLLATE LOCALIZED ASC"; 
    mCursor = getContentResolver().query(uri, projection, selection, selectionArgs, 
      sortOrder); 
} 
if (mCursor != null) { 
    mCursor.moveToFirst(); 
} 
return mCursor; 
} 
}