0

My MainActivity has a getContactList() method that does what it's suppose to. I also have recyclerViews and adapters and all that work when viewing activity_main.xml in an emulator, so the problem isn't there. The problem I'm having is making the app read the arrayList of contacts or just doing it automatically.

Here's my MainActivity code (what's more important is the onCreate method which loads everything it's suppose to and the getContactList()):

package com.app.wolfix;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.Button;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    RecyclerView recyclerView;
    ArrayList<ContactModel> arrayList = new ArrayList<ContactModel>();
    MainAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = findViewById(R.id.recycler_view);

        adapter = new MainAdapter(this, arrayList);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);

        Button add_btn = findViewById(R.id.add_btn);

        add_btn.setOnClickListener(v -> startActivity(new Intent(MainActivity.this, Add_Contact.class)));

        Button rem_btn = findViewById(R.id.rem_btn);

        rem_btn.setOnClickListener(v -> startActivity(new Intent(MainActivity.this, Main_Remove_Contact.class)));

        checkPermission();
    }

    private void checkPermission() {
        if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_CONTACTS}, 100);
        }else{
            getContactList();
        }
    }

    private void getContactList() {
        Uri uri = ContactsContract.Contacts.CONTENT_URI;

        String sort = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" ASC";

        Cursor cursor = getContentResolver().query(
                uri, null, null, null, sort);

        if (cursor.getCount() > 0){
            while (cursor.moveToNext()){
                String id = cursor.getString(cursor.getColumnIndexOrThrow(
                        ContactsContract.Contacts._ID
                ));
            }
        }
    }
}

checkPermission() is just a verification check that goes for getContactList() once it accepts it, BUT there could be an error there too that I just can't really dissect (it's code from a video tutorial I saw but I have been getting some help and there have been some code changes).

Would it be easier to just make a while() loop somewhere to read all the contacts and add them to arrayList or is there an already added feature that does that already, since I already had that working before. Here's a tutorial I saw a long time ago that used to work on my app too: https://www.youtube.com/watch?v=esDWSfBa-oc

Thanks in advance!

1 Answers1

0

I'll recommend you you to look into the given link. I hope you'll find your solution.

Link: android get all contacts

nilesh14k
  • 58
  • 5
  • Umm, it worked for the most part, but what is this variable `TAG`: `Log.i(TAG, "Name: " + name);` `Log.i(TAG, "Phone Number: " + phoneNo);` – André_Almeida Mar 11 '22 at 08:07
  • It is just for your debugging of code, to get values in your logcat. You can remove it, if you want to. It will not affect the working of code. – nilesh14k Mar 11 '22 at 11:33
  • Oh well, it didn't matter anyways. Turns out I didn't finish watching the tutorial that regarded that. Once I finish it, I'll see if it your answer would have fixed my error. – André_Almeida Mar 11 '22 at 17:23
  • I'm sorry, the reason **was** actually because I forgot to watch the rest of the tutorial I was following. Unfortunately, I won't be able to accept your answer because the code I **could** replace doesn't need to be. – André_Almeida Mar 11 '22 at 18:28