为什么会筛选失败呢?
public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor>{ public static final String TAG="123"; public static final int REQUEST_CODE=1444; private LoaderManager loaderManager; private ListView listView; private EditText edt; private String name; private SimpleCursorAdapter mCursorAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); request(); setContentView(R.layout.activity_main); listView=findViewById(R.id.id_lv); edt=findViewById(R.id.id_edt); //设置游标适配器 mCursorAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, null, new String[]{ContactsContract.Contacts.DISPLAY_NAME}, new int[]{android.R.id.text1}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); listView.setAdapter(mCursorAdapter); edt.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { name=s.toString(); //重新创建loader loaderManager.restartLoader(0,null,MainActivity.this); Log.i(TAG, "onTextChanged: 1"); } @Override public void afterTextChanged(Editable s) { } }); loaderManager = getSupportLoaderManager(); loaderManager.initLoader(0,null,this); } //请求权限 private void request() { if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {//没有权限需要动态获取 //动态请求权限 ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_CONTACTS}, REQUEST_CODE); } if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_CONTACTS) != PackageManager.PERMISSION_GRANTED) {//没有权限需要动态获取 //动态请求权限 ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_CONTACTS}, REQUEST_CODE); } } @NonNull @Override public Loader<Cursor> onCreateLoader(int id, @Nullable Bundle args) { Uri uri=ContactsContract.Contacts.CONTENT_URI; if(!TextUtils.isEmpty(name)){ Log.i(TAG, "onCreateLoader: 2 "+name); uri=Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_FILTER_URI, Uri.encode(name)); } Log.i(TAG, "onCreateLoader: 3"+uri.toString()); CursorLoader cursorLoader=new CursorLoader(this, ContactsContract.Contacts.CONTENT_URI, null, null, null, null); return cursorLoader; } @Override public void onLoadFinished(@NonNull Loader<Cursor> loader, Cursor data) { mCursorAdapter.swapCursor(data); } @Override public void onLoaderReset(@NonNull Loader<Cursor> loader) { //释放当前cursor mCursorAdapter.swapCursor(null); } }
10
收起
正在回答 回答被采纳积分+1
2回答
Android数据通信开发与应用2018版
- 参与学习 人
- 提交作业 147 份
- 解答问题 687 个
本专题介绍了Android开发核心组件:广播、服务及全局应用。教会你如何使用AIDL、Thread、Socket、蓝牙解决进程线程间通信问题。利用Glide等实现异步加载及NDK原生代码开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星