Max Shady
Max Shady

Reputation: 78

Separate String inputs android

the problem is I can't remove " , " while there is only 1 item checked and when there are more than one I don't know how to remove the " , " from the end.

public class CheckBox extends AppCompatActivity implements View.OnClickListener {

ImageButton check_button;

CheckBox eminem , dre , tupac , royce , logic , big_sean;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_e10_check_box);
    setTitle("Check Box");
    Inits();
}
private void Inits(){
    check_button = findViewById(R.id.check_button);

    eminem = findViewById(R.id.eminem);
    dre = findViewById(R.id.dre);
    tupac = findViewById(R.id.tupac);
    royce = findViewById(R.id.royca);
    logic = findViewById(R.id.logic);
    big_sean = findViewById(R.id.big_sean);

    check_button.setOnClickListener(this);
}

@Override
public void onClick(View view) {

    String result = "Your Favorite List : x";
    String singers = "";

    if(eminem.isChecked()){
        singers += "Eminem" + " , " ;
    }
    if (dre.isChecked()){
        singers += "Dr.Dre" + " , ";
    }
    if (tupac.isChecked()){
        singers += "Tupac" + " , ";
    }
    if (royce.isChecked()){
        singers += "Royce" + " , ";
    }
    if (logic.isChecked()){
        singers += "Logic" + " , ";
    }
    if (big_sean.isChecked()){
        singers += "Big Sean" + " , ";
    }
    app.t(result.replace("x" , singers));


}

}

Upvotes: 2

Views: 62

Answers (3)

Mr. Brickowski
Mr. Brickowski

Reputation: 1181

You can assign all those Checkbox to a Map and traverse it. Check each of the checkbox and if checked, append a text to a ArrayList. String.join() comes in handy for concatenating string segment.

    Map<String, CheckBox> checkboxMap = new ConcurrentHashMap<String, CheckBox>();

    private void Inits() {
        check_button = findViewById(R.id.check_button);

        eminem = findViewById(R.id.eminem);
        dre = findViewById(R.id.dre);
        tupac = findViewById(R.id.tupac);
        royce = findViewById(R.id.royca);
        logic = findViewById(R.id.logic);
        big_sean = findViewById(R.id.big_sean);

        // organize the checkbox and singer into map
        checkboxMap.put("Eminem", eminem);
        checkboxMap.put("Dr.Dre", dre);
        checkboxMap.put("Tupac", tupac);
        checkboxMap.put("Royce", royce);
        checkboxMap.put("Logic", logic);
        checkboxMap.put("Big Sean", big_sean);

        check_button.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        ArrayList<String> selectedSiners = new ArrayList<String>();

        for (Set<Map.Entry<String, CheckBox>> checkboxEntry : checkboxMap.entrySet()) {
            if (checkboxEntry.getValue().isChecked()) {
                selectedSiners.add(checkboxEntry.getKey());
            }
        }

        String result = "Your Favorite List : ";

        app.t(result + String.join(", ", selectedSiners));

    }

Upvotes: 0

kgori_dev
kgori_dev

Reputation: 166

@MaxShady here is the answer.

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

Button submit;
CheckBox eminem, dre, tupac, royce, logic, big_sean;
TextView info;

//add this
List<String> artists = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Inits();
}

private void Inits() {
    //add this
    artists = new ArrayList<String>();

    submit = findViewById(R.id.submit);

    info = findViewById(R.id.info);
    eminem = findViewById(R.id.eminem);
    dre = findViewById(R.id.dre);
    tupac = findViewById(R.id.tupac);
    royce = findViewById(R.id.royce);
    logic = findViewById(R.id.logic);
    big_sean = findViewById(R.id.big_sean);

    submit.setOnClickListener(this);
}

@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onClick(View view) {
    String result = "Your Favorite List : x";

    //checks if the checkbox is selected
    if (eminem.isChecked()) {
        //checks if list already contains the item
        if (!artists.contains(eminem.getText().toString())) {
            artists.add(eminem.getText().toString());
        }
    } else {
        //removes item if it was not selected
        artists.remove(eminem.getText().toString());
    }
    //checks if the checkbox is selected
    if (dre.isChecked()) {
        //checks if list already contains the item
        if (!artists.contains(dre.getText().toString())) {
            artists.add(dre.getText().toString());
        }
    } else {
        //removes item if it was not selected
        artists.remove(dre.getText().toString());
    }
    //checks if the checkbox is selected
    if (tupac.isChecked()) {
        //checks if list already contains the item
        if (!artists.contains(tupac.getText().toString())) {
            artists.add(tupac.getText().toString());
        }
    } else {
        //removes item if it was not selected
        artists.remove(tupac.getText().toString());
    }
    //checks if the checkbox is selected
    if (royce.isChecked()) {
        //checks if list already contains the item
        if (!artists.contains(royce.getText().toString())) {
            artists.add(royce.getText().toString());
        }
    } else {
        //removes item if it was not selected
        artists.remove(royce.getText().toString());
    }
    //checks if the checkbox is selected
    if (logic.isChecked()) {
        //checks if list already contains the item
        if (!artists.contains(logic.getText().toString())) {
            artists.add(logic.getText().toString());
        }
    } else {
        //removes item if it was not selected
        artists.remove(logic.getText().toString());
    }
    //checks if the checkbox is selected
    if (big_sean.isChecked()) {
        //checks if list already contains the item
        if (!artists.contains(big_sean.getText().toString())) {
            artists.add(big_sean.getText().toString());
        }
    } else {
        //removes item if it was not selected
        artists.remove(big_sean.getText().toString());
    }

    info.setText(result.replace("x", String.join(",", artists)));
  }
}

A list variable is declared at the top called artists. it is initialized to null and then instantiated at the Inits() method.

On the onlick() method, if a checkbox is selected, the value of that checkbox is first checked if it is not already available in the artists variable to prevent duplicates, if its not available, it is then added. if a checkbox is deselected or unchecked, the value of the checkbox is removed from the artists list. Then the values in the list are joined using a comma(,) using the String.join(",", artists)

Upvotes: 0

Stoyan Milev
Stoyan Milev

Reputation: 735

You could try using StringBuilder for better performance. It doesn't create a new object when appending to it. Here is a possible solution to your problem:

        StringBuilder builder = new StringBuilder();
        if(eminem.isChecked()){
            builder.append("Eminem").append(RAPPER_SEPARATOR);
        }
        if (dre.isChecked()){
            builder.append("Dre").append(RAPPER_SEPARATOR);
        }
        if (tupac.isChecked()){
            builder.append("Tupac").append(RAPPER_SEPARATOR);
        }
        if (royce.isChecked()){
            builder.append("Royce").append(RAPPER_SEPARATOR);
        }
        if (logic.isChecked()){
            builder.append("Logic").append(RAPPER_SEPARATOR);
        }
        if (big_sean.isChecked()){
            builder.append("Big Sean").append(RAPPER_SEPARATOR);
        }
        final int length = builder.length();
        if (length > 0) {
            builder.delete(length - RAPPER_SEPARATOR.length() - 1, length - 1);
            app.t(result.replace("x" , builder.toString()));
        }

Where RAPPER_SEPARATOR = " , ".

Also have in mind that the code has duplicates and if the CheckBox text is the same as the one you are appending you can create a function which does this like this one:

    private void appendRapperIfChecked(CheckBox cb, StringBuilder builder) {
        if (cb.isChecked()) {
            builder.append(cb.getText()).append(RAPPER_SEPARATOR);
        }
    }

and call it multiple times:

        appendRapperIfChecked(builder, eminem);
        appendRapperIfChecked(builder, dre);
        appendRapperIfChecked(builder, tupac);
        appendRapperIfChecked(builder, royce);
        appendRapperIfChecked(builder, logic);
        appendRapperIfChecked(builder, big_sean);

        final int length = builder.length();
        if (length > 0) {
            builder.delete(length - RAPPER_SEPARATOR.length() - 1, length - 1);
            app.t(result.replace("x" , builder.toString()));
        }

Upvotes: 2

Related Questions