2

How do I get data from a specific ArrayList row using a loop? I've added those value into ArrayList as follow.

myArrayList.add("ID007");
myArrayList.add("PPShein");
myArrayList.add("Male");
myArrayList.add("7-Apr-1983");

I want to do something like this:

for (i=0; i < myarr.size(); i++)
{
    getName = myarr[2].value();
}

It's because of I want to display as follow.

myTextView.setText(getName); //myName : "ppshein"
2
  • 2
    @ppshein Can you explain the question a little more? When you say how to get data from a specific ArrayList row, it sounds like you have an ArrayList fill of arrays, or something representing a row and column structure. Is that correct? And what do you need a loop for? Thanks. Commented Aug 22, 2011 at 1:46
  • @Ray Toal, it's because of I don't want to bind ArrayList with setAdapater. What I want is I want to get the data of only one specific row from arraylist to display TextView. Commented Aug 22, 2011 at 2:26

2 Answers 2

8

ArrayList has a handy method called get, which takes in an index. What you may be used to is using Arrays, such as array[3] to access the 4th element. With an ArrayList, use the get method:

for(int i = 0; i < myArr.size(); i++) {
  System.out.println(myArr.get(i)); //prints element i 
}
Sign up to request clarification or add additional context in comments.

Comments

2

You just call the row...

    String getName;
    int rowValue = 2;
    getName = myarr.get(rowValue);

1 Comment

String should be capitalized there.

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.