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!