0

I have situation where I have Arraylist, which has name items in it. I need to loop through that namelist which check if the new name that user gave, is already on a list or not. If it is, just give toast to notify user that name is already there, or if it isn't, then add name to the list.

This is what I have:

public class ActivityPlayers extends AppCompatActivity {
    public static ArrayList<NameItem> mNameList;

    private Button buttonAdd;
    private EditText textAdd;

    private int checkNumber;


        /** When "add" button been clicked... **/
        textAdd = findViewById(R.id.name_input);
        buttonAdd.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                /** Loop the list **/
                for (int i = 0; i < mNameList.size(); i++) {

                    /** Check if user input match to any name in list 
                    and if it does...**/
                    if (mNameList.get(i).getText1().equals(textAdd.toString().trim())) {
                        checkNumber = 1;
                        break;
                    } else {
                        /** If it doesn't **/
                        checkNumber = 0;
                    }
                }

                /** if checkNumber is still 0 **/
                if (checkNumber == 0) {
                    /** Close soft keyboard **/
                    InputMethodManager input = (InputMethodManager)
                            getSystemService(Context.INPUT_METHOD_SERVICE);
                    input.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);

                    /** ...add a name to the namelist **/
                    addItem(0);
                } else {
                    /** if name in a list == name from input, give toast **/
                    Toast toast = Toast.makeText(getApplicationContext(),
                            "Name is already on a list", Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0);
                    toast.show();
                }
            }
        });

Any ideas why this won't work? It only adds the names to the list, even tho it already exists... but it never detects duplicates...

0

3 Answers 3

2

if (mNameList.get(i).getText1().equals(textAdd.toString().trim()))

In the above line of code you compare the ith list element to the string representation of the EditText itself, not to its Text field. Instead of textAdd.toString(), you should use textAdd.getText().

Unrelated to the problem, but I suggest using boolean variables to represent logical flags in your code instead of integer counting.
In the above example, it would go like this:

private boolean nameInList = false;
//...
if (...) {
    nameInList = true;
    break;
}
//...
if (nameInList) {
    //...
} else {
    //...
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

if (mNameList.get(i).getText1().equalsIgnoreCase(textAdd.getText().toString().trim())) {
    checkNumber = 1;
    break;
} else {
    /** If it doesn't **/
    checkNumber = 0;
}

Comments

0

I would advise you to use a suitable data structure Set which disallows duplicate values. You will also have saved your self from loops.

create a set object for example Set<String> names = new TreeSet(); call the add() which returns true if name doesn't exist in set, else false meaning name exists.

Set<String> names = TreeSet();
boolean isExists = names.add("my name");
if(isExists){ // make you toast here}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.