0

I have arraylist (mNamelist), which has items (NameItem), and those items contain 2 variables (String and int). I also have an input field, where the user inserts new names to the list.

I need to check every item in a list, and if the user gave name is not in any of those items, then it can add an item. If it finds a match, it should give Toast and stop looping immediately.

something I tried:

private void addItem(int position) {
    /** Get user input (name) **/
    textAdd = findViewById(R.id.name_input);

    /** Check if that name already on a list **/
    for (int i = 0; i < mNameList.size(); i++) {

        /** Check namelists item, and in that item String variable named getText1 and check if it match to the name that user gave **/
        if (mNameList.get(i).getText1() == textAdd.toString().trim()) {
            /** If match, give toast and stop looping **/
            Toast toast= Toast.makeText(getApplicationContext(),
                    "This name is already on a list...", Toast.LENGTH_LONG);
            toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 0);
            toast.show();
            break;
        } else {
            /** If no matches, add name to the list **/
            mNameList.add(position, new NameItem(textAdd.getText().toString().trim()));
            sortArrayList();
            saveData();
            mAdapter.notifyItemInserted(position);
            textAdd.getText().clear();
        }
    }
}

Soon I realized that problem here is that every time it won't get a match, it tries to add that item. What it should do is that AFTER ALL items been checked AND none match found, only then it should add that new name ONCE, not in every loop. Also if it finds a match, it should give that Toast and stop looping immediately.

Antti
  • 89
  • 1
  • 5
  • I don't think the `addItem` method is the correct place for checking if an item should be added or not. Do that anywhere else. You could apply streaming if you use Java-8. – deHaar Jul 06 '19 at 11:29
  • Why dont you just loop the array list using foreach loop and check the string variable of each item with the input ? – Syed Ahmed Jamil Jul 06 '19 at 11:58
  • Different ways to iterate a list in java https://stackoverflow.com/questions/18410035/ways-to-iterate-over-a-list-in-java?rq=1 – Syed Ahmed Jamil Jul 06 '19 at 12:04
  • Cause I need to do if... if loop finds, that string from the list, then give Toast and NOT include name to the list, and after ALL items been checked AND none of matches found, only after that mNameList.add() and everything which comes after it – Antti Jul 06 '19 at 12:08
  • Then just show toast when you find it if the if condition gets true while searching in the list. What have you tried so far to loop the array list. Can you share that code ? – Syed Ahmed Jamil Jul 06 '19 at 12:17

1 Answers1

1

You can use the java 8 stream api if your api level is high enough or a simple for-each loop to check if the list already contains an entry with the given input name. You just have to iterate through your list and check if any nameItem contains a name which is equal to your input. If yes you can make your toast. Otherwise you can create a new nameItem with the given input and add it to your list.

private boolean alreadyContainsNameWithForEach(Collection<NameItem> mNames, String input){
    for(NameItem item : mNames){
        if(item.getName().equals(input)){
            return true;
        }
    }
    return false;
}

private boolean alreadyContainsNameWithStreams(Collection<NameItem> mNames, String input){
    return mNames.stream().anyMatch(nameItem -> nameItem.getName().equals(input));
}
Chr3is
  • 387
  • 5
  • 21
  • If you can't update to api lvl 24 you can also use a for each loop. You have to iterate through your list and check if any entry matches the given input. If yes, make your toast. Otherwise you can add the item. This will have a performance of O(n). – Chr3is Jul 06 '19 at 12:03