6
\$\begingroup\$

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.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;
}
\$\endgroup\$
6
\$\begingroup\$

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);
\$\endgroup\$
  • 2
    \$\begingroup\$ +1 for being an example to follow \$\endgroup\$ – Mathieu Guindon Nov 18 '13 at 18:50
  • \$\begingroup\$ I Edit white space in questions, because most of the time people copy and paste their code, and some IDE's put tabs in and some put in spaces, and the formatting messes things up, it's not always visible when they are preparing the question. but yeah sometimes people just have a lot of white space. \$\endgroup\$ – Malachi Nov 18 '13 at 20:34
  • \$\begingroup\$ The questionable content you refer to in the second paragraph has been removed from the question. On SO I would roll that edit back for invalidating an important part of an answer, but I'm not sure how you would want to handle it here. (Especially given that both the users involved are mods!) \$\endgroup\$ – Josh Caswell May 9 '16 at 21:16
2
\$\begingroup\$

Naming conventions

Several of your classes and variables defies Java naming conventions. A class should start with an uppercase letter and a variable with a lowercase letter (with a possible exception for static final variables, which you do not use).

Here are some changes to conform with the naming conventions

class start_Serch --> class StartSearch
Uri Contacts --> Uri contacts
Cursor C --> Cursor cursor

Your classes

Your Event class only job is to set an onClickListener, which it sets in the constructor. A constructor should normally not be used to actually perform anything, it should merely store the information it needs to do stuff later. However, I don't think you need your Event class at all. And you especially don't need to store it in a variable (which is never used later).

You also don't need to create the StartSearch class or the Layout class. Instead, store the values in the Layout class in your Activity (the class where the onCreate method is) and let your Activity implement OnClickListener.

So I would change this code in your onCreate method

layout = new Layout();
Event event = new Event(layout);

To this:

txtName = (EditText)findViewById(R.id.txtName);
btnSerch = (Button)findViewById(R.id.btnSerch);
group1 = (RadioGroup)findViewById(R.id.group1);
btnSerch.setOnClickListener(this);
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.