-1

Summary of Problem:

In a nutshell, a user selects a name, and then they try to connect to a server. That server checks if the name is taken. If the name is taken, they are sent to the Name Selection screen, and the list should update by removing all the other names that are currently taken. I am getting a String with names inside that one String from a server. I am trying to use that one String to remove names from an ArrayList. For example, my list has Name 01, Name 02, Name 03, Name 04. They are individual items in my list. When I get a String Message from the server it is for example in one String like this: "Name 01, Name 02, Name 03" and I want to be able to remove those names from the ArrayList.

What I have tried:

I have created an Iterator to go through the list of names a user can select from. I have tried using a single name "Name 01" for example to remove it from the list and that works. I have also tried using "Name 01" + "Name 02" but it doesn't remove anything from the list. I have also tried "Name 01, Name 02, Name 03" but that doesn't remove anything either. I have also tried both if (s.contains(value)) and if (s.startsWith(value)) with the long String of Names in one String and it still doesn't remove anything. I have also looked at: Remove multiple elements with multiple indexes from arraylist in java - Java Arraylist remove multiple element by index - Remove multiple items from ArrayList - Remove items from ArrayList with certain value - Remove all elements from an ArrayList that contains string

Name Selection Activity: within onCreate

// Create the list to populate the spinner

List<String> deviceNameList = new ArrayList<>();
nameList.add("Name 01");
nameList.add("Name 02");
nameList.add("Name 03");
nameList.add("Name 04");

// Array Adapter for creating list

adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, nameList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
nameSpinner.setAdapter(adapter);
nameSpinner.setOnItemSelectedListener(this);

// An Iterator to go through the list and remove any names already taken
// that we get back as one String from server
// This is the Extra Intent from the Main Activity if user has chosen a name already taken

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String value = extras.getString("namesTaken");

    Iterator<String> it = nameList.iterator();
    while (it.hasNext() ) {
        String s = it.next();
        if (s.startsWith(value)) {
            it.remove();
    }
}

Main Activity: within onCreate

// Create test extraIntent with names to see if the list will be updated
// Test button simulates if name is already taken, server will send message to phone
// and send user back to the device name selection screen and only show the devices that are free to select

bTest.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        String namesTaken = "Horizon 01, Horizon 02, Horizon 03";
        myIntent.putExtra("namesTaken", namesTaken);
        startActivity(myIntent);
        //Clear shared preferences
        SharedPreferences settings = context.getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE);
        //Removes Device Name to be empty ""
        settings.edit().remove(NAME).commit();
        finish();
    }
});

What I expect and actual results: I expect a user to pick a name from the ArrayList, then get sent to the MainActivity. When they connect to the server, I expect to be able to get a String of names taken from the server, and send the user back to the Name Selection Screen while using the String to remove the names taken and update the list to only show available names in the ArrayList. Right now, I can only remove one name in a String "Name 01" for example but cannot with a long String like "Name 01, Name 02, Name 03"

Salim Djerbouh
  • 10,719
  • 6
  • 29
  • 61
Treewallie
  • 346
  • 2
  • 13

1 Answers1

2

If I understand correctly, you have a String which contains multiple names separated with a delimiter. You want to remove all those names from a List Whig stores each name as a different element.

So you have a list: ["name1","name2",...]. You have a String: String names = "name1, name2".

You try to remove the names from the list using list.remove(names);. This doesn’t work because it tries to remove an element which equals to the parameter.

Instead, you must first split the names into different elements and remove each:

String[] toRemove = names.split(", ");
list.removeAll(Arrays.asList(toRemove));
tomtzook
  • 539
  • 2
  • 6
  • This looks promising. I put it like this ``String namesTaken = "Horizon 01, Horizon 02, Horizon 03"; String[] toRemove = namesTaken.split(“, “); myIntent.putExtra("deviceNamesTaken", toRemove);`` and then send the user back to the Device Selection screen and the iterator looks like this: ``String value = extras.getString("deviceNamesTaken"); Iterator it = deviceNameList.iterator(); while (it.hasNext() ) { String s = it.next(); if (s.startsWith(value)) { it.remove(); ...`` – Treewallie Sep 05 '19 at 19:11
  • I am getting an error "java.lang.NullPointerException: Attempt to read from field 'int java.lang.String.count' on a null object reference" – Treewallie Sep 05 '19 at 19:15
  • Where am I placing these two lines you posted? In the iterator? – Treewallie Sep 05 '19 at 19:19
  • 1
    Got it to work! Removed the Iterator completely! You pointed me in the right direction with your two lines of code. ``Bundle extras = getIntent().getExtras(); if (extras != null) { String value = extras.getString("namesTaken"); String[] toRemove = value.split(", "); nameList.removeAll(Arrays.asList(toRemove)); }`` – Treewallie Sep 05 '19 at 19:28
  • 1
    Sorry I wasn’t available to answer your comments... but hey, you figured it out – tomtzook Sep 05 '19 at 20:05
  • No problem. If you can upvote my OP that would be helpful. – Treewallie Sep 09 '19 at 14:24