Home > Blockchain >  Why is lint showing 'Value >= 1' on getting contact list names and numbers in Android S
Why is lint showing 'Value >= 1' on getting contact list names and numbers in Android S

Time:12-20

I'm creating a simple text messaging app in Android Studio, but whenever I try to get the contact list from the user's phone by defining a getContactList() function, the lint shows an error that the range value should be greater than one. Why is it coming? I have checked from many sources, and I've written the exact same code they wrote. Here's the exact code:

package com.example.safechat;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.LinearLayout;

import java.util.ArrayList;

public class FindUserActivity extends AppCompatActivity {

    private RecyclerView.Adapter mUserListAdapter;

    ArrayList<UserObject> userList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_user);

        userList = new ArrayList<>();

        initializeRecyclerView();
    }

    private void getContactList() {
        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        while(phones.moveToNext()) {
            String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); //Value should be >= 1
            String phone = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); //Value should be >= 1

            UserObject mContact = new UserObject(name, phone);
            userList.add(mContact);
            mUserListAdapter.notifyDataSetChanged();
        }
    }

    private void initializeRecyclerView() {
        RecyclerView mUserList = findViewById(R.id.userList);
        mUserList.setNestedScrollingEnabled(false);
        mUserList.setHasFixedSize(false);
        RecyclerView.LayoutManager mUserListLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false);
        mUserList.setLayoutManager(mUserListLayoutManager);
        mUserListAdapter = new UserListAdapter(userList);
        mUserList.setAdapter(mUserListAdapter);
    }
}

The lint isn't clear on what should I do, if you can help, it will be your kindness.

CodePudding user response:

Because getColumnIndex() returns -1 if the column does not exist. The return value of phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME) and phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER) are unknown until runtime. If they happen to return -1, phones.getString(-1) will throw. Ideally you should check the return value of getColumnIndex() before passing it to getString()

  • Related