开发者

How to get datas from List<Object> (Java)?

I`m new in Java and have a problem with showing data from a list of objects. I have a simple method, which should collect data across multiple tables and return it to my controller:

public List<Object> getHouseInfo(){
Query q = em.createNativeQuery("SELECT houses.id, addresses.country, addresses.region, house_details.rooms, house_details.square FROM houses, addresses, house_details");
List<Object> myList = q.getResultList(); 
return myList;}

Now I want to get this data in controller, but I don`t know how to get single results from the list. I tried to do something like this:

List<Object> list = getHouseInfo();
for (int i=0; i<list.size; i++){
System.out.println("Element "+i+list.get(0));}

but I`m getting only references to this objects (for example [Ljava.lang.Object;@167a47b). I also tried to use Iterator, but the result is the same. I tried to use a code like this:

List<Object> list = getHouseInfo();
for (int i=0; i<list.size; i++){
System.out.println("Element "+i+list.get(0)[0]);}

but it doesn`t help me too - this ends with a compile error.

Can someone tell me how to get an 'id'(integer value) from this list? I`m using MyFaces in my 'View' where I have a code like this (houseControll is the name of my JSF Managed Bean - the controller):

<t:dataList id="myDataList" value="#开发者_StackOverflow中文版{houseControll.fullOffer}" var="element" rows="3" >
...
<t:outputText id="houseId" value="#{element[0]}"/>
...
</t:dataList>

this code shows an 'id' value properly - I have 1,2,3,... values. How can I get the same result in my controller? How to print the data in controller?


For starters you aren't iterating over the result list properly, you are not using the index i at all. Try something like this:

List<Object> list = getHouseInfo();
for (int i=0; i<list.size; i++){
   System.out.println("Element "+i+list.get(i));
}

It looks like the query reutrns a List of Arrays of Objects, because Arrays are not proper objects that override toString you need to do a cast first and then use Arrays.toString().

 List<Object> list = getHouseInfo();
for (int i=0; i<list.size; i++){
   Object[] row = (Object[]) list.get(i);
   System.out.println("Element "+i+Arrays.toString(row));
}


Do like this

List<Object[]> list = HQL.list(); // get your lsit here but in Object array

your query is : "SELECT houses.id, addresses.country, addresses.region,..."

for(Object[] obj : list){
String houseId = String.valueOf(obj[0]); // houseId is at first place in your query
String country = String.valueof(obj[1]); // country is at second and so on....
.......
}

this way you can get the mixed objects with ease, but you should know in advance at which place what value you are getting or you can just check by printing the values to know. sorry for the bad english I hope this help


Thanks All for your responses. Good solution was to use 'brain`s' method:

List<Object> list = getHouseInfo();
for (int i=0; i<list.size; i++){
Object[] row = (Object[]) list.get(i);
System.out.println("Element "+i+Arrays.toString(row));
}

Problem solved. Thanks again.


System.out.println("Element "+i+list.get(0));}

Should be

System.out.println("Element "+i+list.get(i));}

To use the JSF tags, you give the dataList value attribute a reference to your list of elements, and the var attribute is a local name for each element of that list in turn. Inside the dataList, you use properties of the object (getters) to output the information about that individual object:

<t:dataList id="myDataList" value="#{houseControlList}" var="element" rows="3" >
...
<t:outputText id="houseId" value="#{element.houseId}"/>
...
</t:dataList>


You should try something like this

List xx= (List) list.get(0)
String id = (String) xx.get(0)

or if you have a House value object the result of the query is of the same type, then

House myhouse = (House) list.get(0);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜