HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Search Android contacts more efficiently

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
efficientlyandroidsearchcontactsmore

Problem

Is there is better way to do that more efficiently?

onCreate():

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    layout = new Layout();
    Event event = new Event(layout);
}


Layout and Event:

class Layout
{
    public Layout()
    {
         txtName = (EditText)findViewById(R.id.txtName);
         btnSerch = (Button)findViewById(R.id.btnSerch);
        group1 = (RadioGroup)findViewById(R.id.group1);
    }
    RadioGroup group1;
    EditText txtName;
    Button btnSerch;
}

class Event
{
    public Event(Layout layout)
    {
        layout.btnSerch.setOnClickListener(new start_Serch());
    }
}

class start_Serch implements OnClickListener
{
    @Override
    public void onClick(View view)
    {
        Serch(view, layout);
    }
    Layout layout;
}


Main goal: (the main commands of the application)

void Serch(View v, Layout layout)
{
    Uri Contacts = android.provider.ContactsContract.Contacts.CONTENT_URI;

    Cursor C = getContentResolver().query(Contacts, null, null, null, null);

    if(C != null)
    {
        if(C.moveToFirst())
        {
            do
            {
                String display_ContactsName = getValue(C, android.provider.ContactsContract.Contacts.DISPLAY_NAME);

                if (Check(display_ContactsName, C) == true)
                {
                    break;
                }

            }while (C.moveToNext());
        }
    }
}


getValue() and Check():

```
private String getValue(Cursor cursor, String name)
{
return cursor.getString(cursor.getColumnIndex(name));
}
private boolean Check(String Name, Cursor C)
{
boolean Found = false;
int id = layout.group1.getCheckedRadioButtonId();
switch (id)
{
case -1:
break;
case R.id.fullName:
if (layout.txtName.getText().toString().equals(Name))
{
AlertDialog.Buil

Solution

Android Strings

Android provides a nice way of externalizing Strings in a strings.xml resource file. Using it right from the start of your project or as soon as possible is a very good idea, you might regret it later if you don't. Using XML-resources in Android is how to provide internationalization of your application. It is also good to keep all strings in one place, so you can get a nice overview of the strings shown in your application.

Speaking of Strings that are showing in your application, the content of your message String for your AlertDialogs is very questionable. If I as an user would encounter the message "f*ck ya" in an application, I would never ever give it a 5 star rating on Google Play. And it honestly does not give me a good impression of you as a person either.

The "ok" message is already defined as a string message in Android, and if your dialog button should only close the dialog then you can pass null as the listener. So therefore you can use alert.setPositiveButton(android.R.string.ok, null); which greatly improves code readability.

Readability

Speaking of code readability... I feel that you have an excessive amount of empty lines in your code, including but not restricted to:

if(C != null)

{

            alert.setPositiveButton("ok",new DialogInterface.OnClickListener() {

                @Override

                public void onClick(DialogInterface dialogInterface, int i)

        do

        {

            String display_ContactsName = getValue(C, android.provider.ContactsContract.Contacts.DISPLAY_NAME);


Instead consider this:

if(C != null)
{
    ...
    alert.setPositiveButton("ok",new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i)
    ...
        do
        {
            String display_ContactsName = getValue(C, android.provider.ContactsContract.Contacts.DISPLAY_NAME);

Code Snippets

if(C != null)

{

            alert.setPositiveButton("ok",new DialogInterface.OnClickListener() {

                @Override

                public void onClick(DialogInterface dialogInterface, int i)


        do

        {

            String display_ContactsName = getValue(C, android.provider.ContactsContract.Contacts.DISPLAY_NAME);
if(C != null)
{
    ...
    alert.setPositiveButton("ok",new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i)
    ...
        do
        {
            String display_ContactsName = getValue(C, android.provider.ContactsContract.Contacts.DISPLAY_NAME);

Context

StackExchange Code Review Q#31603, answer score: 6

Revisions (0)

No revisions yet.