如何解决如何仅在 ListView 中显示某些项目?
我的代码显示 ListView
中的所有联系人,我想要的是仅显示包含“Ben”和“Dover”的联系人强>”在其中。
示例:
- 本水管工
- 联想本
- 多佛教堂
这是我的完整代码:
public class Contacts extends AppCompatActivity {
ListView listView;
ArrayList<String> StoreContacts;
ArrayAdapter<String> arrayAdapter;
Cursor cursor;
String name,phonenumber;
public static final int RequestPermissionCode = 1;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
listView = (ListView) findViewById(R.id.listview1);
button = (Button) findViewById(R.id.button1);
StoreContacts = new ArrayList<String>();
EnableRuntimePermission();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GetContactsIntoArrayList();
arrayAdapter = new ArrayAdapter<String>(
Contacts.this,R.layout.contact_items_listview,R.id.textView,StoreContacts
);
listView.setAdapter(arrayAdapter);
}
});
}
public void GetContactsIntoArrayList() {
cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null);
while (cursor.moveToNext()) {
name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
phonenumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
StoreContacts.add(name + " " + ":" + " " + phonenumber);
}
cursor.close();
}
public void EnableRuntimePermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(Contacts.this,Manifest.permission.READ_CONTACTS)) {
Toast.makeText(Contacts.this,"CONTACTS permission allows us to Access CONTACTS app",Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(Contacts.this,new String[]{Manifest.permission.READ_CONTACTS},RequestPermissionCode);
}
}
@Override
public void onRequestPermissionsResult(int RC,String per[],int[] PResult) {
switch (RC) {
case RequestPermissionCode:
if (PResult.length > 0 && PResult[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(Contacts.this,"Permission Granted,Now your application can access CONTACTS.",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(Contacts.this,"Permission Canceled,Now your application cannot access CONTACTS.",Toast.LENGTH_LONG).show();
}
break;
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。