Это класс фрагмента, в котором я должен отображать список контактов. Я уже проверил разрешения в манифесте
public class ContactsFragment extends Fragment {
View v;
private RecyclerView recyclerView;
private ContactAdapter contactAdapter;
public ContactsFragment() {
// Required empty public constructor
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
v= inflater.inflate(R.layout.fragment_contacts,container,false);
return v;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
recyclerView= (RecyclerView) v.findViewById(R.id.recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
if (ActivityCompat.checkSelfPermission(getContext(),Manifest.permission.READ_CONTACTS)== PackageManager.PERMISSION_DENIED)
{
requestPermissions(new String[]{Manifest.permission.READ_CONTACTS},1);
return;
}
else {
contactAdapter=new ContactAdapter(getActivity());
recyclerView.setAdapter(contactAdapter);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults[0]==PackageManager.PERMISSION_GRANTED)
{
contactAdapter=new ContactAdapter(getActivity());
recyclerView.setAdapter(contactAdapter);
}
}
}
, и это Activity, которое содержит 2 фрагмента, я не думаю, что я должен отображать здесь список контактов
public class ProfileActivity extends AppCompatActivity {
private Toolbar toolbar;
private FirebaseUser currentUser;
private FirebaseAuth mAuth;
//fragements
private TabsAccessorAdapter myTabsAccessorAdapter;
private ViewPager myViewPager;
private TabLayout myTabLayout;
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_profile);
//toolbar
toolbar = (Toolbar) findViewById(R.id.main_page_toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("MyBackup");
//fragements
myViewPager = (ViewPager)findViewById(R.id.main_tabs_pager);
myTabsAccessorAdapter = new TabsAccessorAdapter(getSupportFragmentManager());
myViewPager.setAdapter(myTabsAccessorAdapter);
myTabLayout = findViewById(R.id.main_tabs);
myTabLayout.setupWithViewPager(myViewPager);
myTabLayout.getTabAt(0).setIcon(R.drawable.ic_contacts);
myTabLayout.getTabAt(1).setIcon(R.drawable.ic_sms);
mAuth = FirebaseAuth.getInstance();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_main,menu);
return true;
}
private void sendUserToLoginActivity(){
Intent intent = new Intent(ProfileActivity.this,MainActivity.class);
startActivity(intent);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
super.onOptionsItemSelected(item);
//hna diri backup w restore
//logout
if (item.getItemId() == R.id.logout_option){
mAuth.signOut();
sendUserToLoginActivity();
}
return true;
}
@Override
protected void onStart() {
super.onStart();
if (currentUser !=null){
sendUserToLoginActivity();
}
}
}
И это класс модели, который содержит только имя, номер телефона и адрес электронной почты
public class Contact {
private String name;
private String phone;
private String email;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
, а это contactAdapter
public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.Viewholder>{
private List<Contact> data=new ArrayList<>();
private ContactDao contactDao;
public ContactAdapter(Context context){
contactDao=new ContactDao(context);
data=contactDao.getData();
}
public static class Viewholder extends RecyclerView.ViewHolder {
TextView txtname;
TextView txtphone;
TextView txtemail;
View v;
public Viewholder(View v){
super(v);
txtname=v.findViewById(R.id.name);
txtphone=v.findViewById(R.id.phone);
txtemail=v.findViewById(R.id.email);
}
}
@NonNull
@Override
public Viewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.contact_items, parent, false);
Viewholder viewholder =new Viewholder(v);
return viewholder;
}
@Override
public void onBindViewHolder(@NonNull Viewholder viewholder, int position) {
viewholder.txtname.setText(data.get(position).getName());
viewholder.txtphone.setText(data.get(position).getPhone());
viewholder.txtemail.setText(data.get(position).getEmail());
}
@Override
public int getItemCount() {
return data.size();
}
}
, а это ContactDAO
public class ContactDao {
private List<Contact> data =new ArrayList<>();
public List<Contact> getData() {
return data;
}
public void setData(List<Contact> data) {
this.data = data;
}
public ContactDao(Context ctx)
{
Cursor Cursorcontacts=new CursorLoader(ctx, Contacts.CONTENT_URI,null,null,null,null).loadInBackground();
Contact contact;
while (Cursorcontacts.moveToNext())
{
contact = new Contact();
contact.setName(Cursorcontacts.getString(Cursorcontacts.getColumnIndex(Contacts.DISPLAY_NAME)));
String Id=Cursorcontacts.getString(Cursorcontacts.getColumnIndex(Contacts._ID));
//Récup les Teles
String [] PhoneProjection=new String[]{CommonDataKinds.Phone.NUMBER};
Cursor cursorPhone=ctx.getContentResolver().query(CommonDataKinds.Phone.CONTENT_URI,PhoneProjection,Id+"="+CommonDataKinds.Phone.CONTACT_ID,null,null);
if (cursorPhone.getCount()>0)
{
cursorPhone.moveToFirst();
contact.setPhone(cursorPhone.getString(0));
}
//Recup. Email
String [] EmailProjection=new String[]{CommonDataKinds.Email.ADDRESS};
Cursor cursorEmail=ctx.getContentResolver().query(CommonDataKinds.Email.CONTENT_URI,EmailProjection,Id+"="+CommonDataKinds.Email.CONTACT_ID,null,null);
if (cursorEmail.getCount()>0)
{
cursorEmail.moveToFirst();
contact.setEmail(cursorEmail.getString(0));
}
data.add(contact);
}
}
}