0

I'm new to Java and Android development and i'm stucked while trying to populate an ArrayAdapter to creating a ListView. The code below works perfectly:

        String name[] = {"a","b","c",
            "d","e"};
        this.setListAdapter(new ArrayAdapter<String>(this,
                R.layout.row, R.id.label,
                name));

But I simply can't figure out how I should populate name with a for loop. All suggestions are welcome.

3
  • Why do you want to populate it with a loop? Which values should it have? Commented Feb 28, 2011 at 10:10
  • Joachim, I have an SAX XML handler result, and I want to draw it line by line. for (int i = 0; i < xmlhandler.getName().size(); i++) { } works fine, but I can't assign the values to name[]. Commented Feb 28, 2011 at 10:14
  • You need to assign to name[i] in that loop. Commented Feb 28, 2011 at 10:15

1 Answer 1

3

ArrayAdapter also has a constructor that takes a List<String>, so you can just do

List<String> name = new ArrayList<String>();
for (... ) {
   name.add(.... );
}
this.setListAdapter(new ArrayAdapter<String>(this,
                R.layout.row, R.id.label,
                name));

Otherwise it just boils down to populating an array with data.

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.