Spandana
Spandana

Reputation: 141

Wicket - getlist() in List printing multiple times

HomePage.java

Table table = new Table("table");
List<String> list = (List<String>) table.getList();
    System.out.println("accessed list:" + list); //returns list from Table.java

    add(new ListView("listview", list) {
        protected void populateItem(ListItem item) {

            item.add(new Label("name", list.get(1)));
            item.add(new Label("type", list.get(3)));
        }
    });

Method in Table.java -

public List<String> getList() {

       return list; 
           }

HomePage.html

<table class="table">
<tr wicket:id="listview">
<td><span wicket:id="name"></span></td>
<td><span wicket:id="type"></span></td>
</tr>
</table>

In HomePage.java ,

  add(new ListView("listview", list) {
  protected void populateItem(ListItem item) {

     item.add(new Label("name", list.get(0))); // printing the value at 1st position multiple times.
     item.add(new Label("type", list.get(1))); // printing the value at 2nd position multiple times.
     }
   });

My list consists of 8 String elements, hence its printing name and type 8 times each.Can Anyone please suggest how to print only once and how to add second row in table(HomePage.html) with same wicket id:listview. I am just a beginner.Any help would be appreciated.Thankyou

Edit -

My list consists of - [name,abc,type,largetext,name,bcd,type,number] Actually this is a JSON.I have parsed the JSON and created a List to pass this to ListView for display in Table. I have parsed and created a List in Table.java . I have ListView in HomePage.java , so used this code in HomePage.java -

Table table = new Table("table");
List<String> list = (List<String>) table.getList();
System.out.println("accessed list:" + list); //returns list from Table.java 

Trying this list value to pass to ListView in HomePage.java for display in Table. But with the code item.add(new Label("name", list.get(1))); ,its printing multiple times i.e as the size of the list.

Upvotes: 0

Views: 685

Answers (3)

m.bouali
m.bouali

Reputation: 484

You have to name your classes and objects in an appropriate way to make your code more readable. You can proceed like this :

List<Person> persons = new ArrayList<>();

    for (int i = 0; i < listOfString.size() - 1; i += 2) {
        Person person = new Person(listOfString.get(i), listOfString.get(i + 1));
        persons.add(person);
    }

Upvotes: 0

m.bouali
m.bouali

Reputation: 484

From what I understood, you want to do something like this:

HomePage.java

Table table = new Table("table");
    List<String> listOfString = (List<String>) table.getList();

    List<MyPojo> listOfMyPojo = new ArrayList<MyPojo>();
    MyPojo myPojo1 = new MyPojo(listOfString.get(1), listOfString.get(3));
    listOfMyPojo.add(myPojo1);
    MyPojo myPojo2 = new MyPojo(listOfString.get(5), listOfString.get(7));
    listOfMyPojo.add(myPojo2);

    add(new ListView<MyPojo>("listview", listOfMyPojo) {
        protected void populateItem(ListItem<MyPojo> item) {
            MyPojo myPojo = item.getModelObject();
            item.add(new Label("name", myPojo.getName()));
            item.add(new Label("type", myPojo.getType()));
        }
    });

Upvotes: 0

m.bouali
m.bouali

Reputation: 484

I did not understand what you want to do exactly but I think that the best way to do that is to create a POJO holding the name and type attributes:

public class MyPojo {
private String name;
private String type;
public MyPojo(String name, String type) {
    super();
    this.name = name;
    this.type = type;
}
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
public String getType() {
    return type;
}
public void setType(String type) {
    this.type = type;
}   

}

And then create a simple Listview using that POJO as a model object:

HTML code:

<table class="table">
<tr wicket:id="listview">
<td><span wicket:id="name"></span></td>
<td><span wicket:id="type"></span></td>
</tr>
</table>

JAVA code:

List<MyPojo> list = new ArrayList<MyPojo>();
//fill your list with the values you want to display
    for (int i = 1; i <= 8; i++) {
        MyPojo myPojo = new MyPojo("name" + i, "type" + i);
        list.add(myPojo);
    }
//and then use a ListView component
    add(new ListView<MyPojo>("listview", list) {
        protected void populateItem(ListItem<MyPojo> item) {
            MyPojo myPojo = item.getModelObject();
            item.add(new Label("name", myPojo.getName()));
            item.add(new Label("type", myPojo.getType()));
        }
    });

Upvotes: 1

Related Questions