user3711421 user3711421 - 3 months ago 18
Android Question

Remove string from list using ((Button)v).getText().toString()

What am i missing, i have some code similar to:

ArrayList<String> myList = new ArrayList<>();
myList.add("hello")
btn.setText("hello");
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String removeThisString = ((Button)v).getText().toString();
myList.remove(removeThisString);
}
});


But it doesn't work so i have to be missing something. Is my string removeThisStrings equals method obscured because of the string extraction from the button? Strings should already have an equals method on them that should let me compare them, which the remove method uses.

I added a log, and removeThisString is indeed = to "hello" and myList = ["hello"]. And myList.contains(removeThisString) is false so even though they look the same they arent somehow.

If i would do
myList.remove("hello");
than it would work. Is there a way i can check the strings equal implementation.

Answer

ArrayList works like an array in the sense that it can get an item by it's index which an int. In this case, adding the first item to myList "hello" is at index 0. So in your code it would be:

  1. Simple Explicit Indexing

    ArrayList<String> myList = new ArrayList<>();
    myList.add("hello")
    btn.setText("hello");
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           String removeThisString = btn.getText().toString();
           myList.remove(0);
        }
    });
    
  2. OR a conditional solution:

    ArrayList<String> myList = new ArrayList<>();
    myList.add("hello")
    btn.setText("hello");
    btn.setOnClickListener(new View.OnClickListener() {
    
        @Override
        public void onClick(View v) {
           String removeThisString = btn.getText().toString();
            if (removeThisString.equals(myList.get(0))) {
                myList.remove(0);
            }
        }
    });
    
  3. OR a conditional and a more dynamic solution:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_activity);
        btn = (Button) findViewById(R.id.activity_button);
        final ArrayList<String> myList = new ArrayList<>();
        String[] list = new String[]{"welcome", "goodbye", "hello"};
    
        for (String s: list) {
            myList.add(s);
        }
    
        btn.setText(myList.get(myList.size() - 1)); //Whatever item you want
    
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String removeThisString = btn.getText().toString();
                int index = (myList.size() - 1);
    
                if (removeThisString.equals(myList.get(index)) && index > 0) {
                    myList.remove(index);
                    btn.setText(myList.get(index - 1));
                } else {
                    btn.setText("No more in the Array List");
                }
            }
        });
    }