Skip to main content
3 of 4
Removed White Space and removed curse words
Malachi
  • 29.1k
  • 11
  • 87
  • 188

Search Android contacts more efficiently?

there is better way to do that? like more efficient? ineed to do this code, but my moderator tell me to do this with more efficient way thx for help:

onCreate:(starters...)

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

layout+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());
        }
    }
}

get value+Chack: (assist commands..)

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.Builder alert = new AlertDialog.Builder(MainActivity.this);

                alert.setTitle("Hey This Contact Is in the LIST!!! yay you are the best lasy man EVER SO G***");

                alert.setMessage("The contact "+Name+" was found in this phone... oh ya");

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

                    @Override
                    public void onClick(DialogInterface dialogInterface, int i)
                    {
                    }
                });
                alert.show();
                Found = true;
            }
            break;
        case R.id.Contane:
            if ( Name.toLowerCase().contains ( layout.txtName.getText ().toString ().toLowerCase()))
            {
                AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);

                alert.setTitle("this is the first result for the name that you typed");

                alert.setMessage("The contact "+Name+" was found in this phone... oh ya");

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

                    @Override
                    public void onClick(DialogInterface dialogInterface, int i)
                    {
                    }
                });
                alert.show();
                Found = true;
                break;
            }
    }

     if (C.isLast() == true&&layout.txtName.getText().toString().equals(Name) == false&& Name.toLowerCase().contains ( layout.txtName.getText ().toString ().toLowerCase()) == false)
    {
        AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);

        alert.setTitle("Hey This Contact Is NOT in the LIST!!!");

        alert.setMessage("The contact/part of "+layout.txtName.getText().toString()+" was not found in this phone...");

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

            @Override
            public void onClick(DialogInterface dialogInterface, int i)
            {
            }
        });
        alert.show();
    }
    return Found;
}