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"