0

I am trying to populate a list view from an ArrayList but for some reason it only grabs the last record. The array holds the data correctly and if I change the I to a number it will print the selected value.

My code is below any help would be appreciated.

  ArrayList<String> tests = new ArrayList();
    for(HashMap<String, String> test : outageData){
        String outagenumber = test.get("outagenum");
        Log.v("Outage",outagenumber);

     tests.add(outagenumber);
    }
    SectionListItem[] exampleArray = null;
    for(int i = 0; i < tests.size(); i++) {
        exampleArray = new SectionListItem[]{

                new SectionListItem("test", tests.get(i)),


        };
    }

    CustomOutageDetailListAdapter adapter = new CustomOutageDetailListAdapter(this, exampleArray);
    sectionAdapter = new SectionListAdapter(getLayoutInflater(),
                adapter);

This is the adapter for fill List.

public class SectionOutageListItem {
public Object item;
public String section;

    public SectionOutageListItem(final Object item, final String section) {
         super();
         this.item = item;
         this.section = section;
    }

    @Override
    public String toString() {
        return item.toString();
    }

}

Updated Code:

 SectionOutageListItem[] exampleArray = new SectionOutageListItem[outnums.size()];

    for(int i = 0; i < outnums.size(); i++) {
        exampleArray[i] = 
      new SectionOutageListItem("Impact", impacted.get(i), "Outage No. " + outnums.get(i)),
      new SectionOutageListItem("status", status.get(i), "Outage No. " + outnums.get(i));


    }
        CustomOutageDetailListAdapter adapter = new CustomOutageDetailListAdapter(this, exampleArray);
        sectionAdapter = new SectionOutageListAdapter(getLayoutInflater(),
                adapter);
1
  • In the for block your settings exampleArray = new SectionListItem[]{ new SectionListItem("test", tests.get(i)), Each time it is recreating a new SectionListItem Commented Jul 15, 2015 at 15:18

2 Answers 2

1

The issue is here:

for(int i = 0; i < tests.size(); i++) {
        exampleArray = new SectionListItem[]{
                new SectionListItem("test", tests.get(i)),
        };
    }

For each element in tests, you are creating a new exampleArray with a single item.

You should use:

SectionListItem[] exampleArray = new SectionListItem[tests.size()];
for(int i = 0; i < tests.size(); i++) {
        exampleArray[i] = new SectionListItem("test", tests.get(i)),
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I actually tried that at first the comma behind the get(i)) was throwing me off.
how can I make that an array of arrays please look at updated code.
Instead of using array of arrays, I suggest you setting all info about the single item inside the object SectionListItem.
0

Update this code:

SectionListItem[] exampleArray = null;
    for(int i = 0; i < tests.size(); i++) {
        exampleArray = new SectionListItem[]{

                new SectionListItem("test", tests.get(i)),


        };
    }

with:

SectionListItem[] exampleArray = new SectionListItem[tests.size()];
    for(int i = 0; i < tests.size(); i++) {
        exampleArray[i]= new SectionListItem("test", tests.get(i));
    }

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.