-1

i have to show the data on the textview and i am fetching it from arraylist and add coma as seperator between them and if their is nothing in the list it must show not specified and what code i wrote for this is

   if (skill.size() != 0) {
                for (int i = 0; i < skill.size(); i++) {
                    if (!skill.get(i).getSkillName().equalsIgnoreCase("")) {
                        tvKeySkills.append(skill.get(i).getSkillName());
                        if (i != skill.size() - 1 && !skill.get(skill.size()-1).getSkillName().equalsIgnoreCase("")) {
                            tvKeySkills.append(" , ");
                        }
                    }
                }
            } else {
                tvKeySkills.append("Not Specified ");
                tvKeySkills.setTextColor(getResources().getColor(R.color.color_three));
            }

now i am facing one problem is that if someone is entering only empty strings it is not showing anything empty textview as it must show not specified and if some data is their in the listview and last 2 index value is empty string it is showing coma at last which it must not show.

9
  • @Nilesh Rathod please check the question before marking it as duplicate. The problem i am facing is totally different from what link you have send. Commented Nov 19, 2018 at 6:43
  • can't you validate before allowing empty values, like if user input is empty you can prompt to enter some data. Commented Nov 19, 2018 at 6:48
  • also inplace of checking equals with "" you can use TextUtils.isEmpty() Commented Nov 19, 2018 at 6:49
  • TextView is empty because you are appending "Not Specified " only if the list is empty. Inside 'for' loop where you are checking if not empty, it's else condition (if empty) is not handled. Commented Nov 19, 2018 at 7:03
  • @KaranMer no, data is coming from json so i cant prompt to enter data. i just have to basically check if all the elements in the arraylist contains " " than it must show "not specified". Commented Nov 19, 2018 at 7:04

1 Answer 1

1

First handle list is empty or null.

Then for each place in list check for not null and not empty skill name, keep adding it to a local string.

This local string will be starting with ,. Remove that before setting in text view.

String keySkills = "";

if(skill != null && !skill.isEmpty()){
    for (int i = 0; i < skill.size(); i++) {
        if (skill.get(i).getSkillName() != null && !skill.get(i).getSkillName().isEmpty()) {
            keySkills = keySkills+", "+ skill.get(i).getSkillName();
        }
    }

    if(keySkills.isEmpty())
        keySkills = "Not Specified";
}else {
    keySkills = "Not Specified";
}

if(keySkills.equals("Not Specified")
    tvKeySkills.setTextColor(getResources().getColor(R.color.color_three));
else
    keySkills = keySkills.subString(2); // handle initial ',' with a space

tvKeySkills.setText(keySkills);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.