gpt4 book ai didi

android - 如何在简单游标适配器上实现搜索?

转载 作者:行者123 更新时间:2023-11-30 02:10:44 26 4
gpt4 key购买 nike

在我的申请中。我想实现放置在操作栏中的搜索 View 。我正在使用简单的游标适配器。简单光标适配器中有一个 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);

最佳答案

为此,我们必须使用 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;
}
}

关于android - 如何在简单游标适配器上实现搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30141999/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com