This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
ListView issue when displaying strings
I have the following code
List<Result> results = response.results;
i need the the values in "results" to be displayed in a listview. what should i write within for (Result result : results){}
You've asked a general question so the best I can do is give you a general answer. You don't use for(Results result: results){} You need to use an ArrayAdapter. For a tutorial on how to do so, please see this documentation.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Both of the codes below give me the same exact answers. I was just wondering which would be better programming practice for readability and maintainability. Would less lines of code be best? Would one affect the functionality of the program more than the other? any suggestions would be very much appreciated as I just want to learn the best practices for programming.
Here are the codes:
for (int i = 0; i < db.getAllDecks().size(); i++)
{
String CardCount = String.format("(%s)",db.getCardsForDeck(i+1).size());
adapter2.add(db.getAllDecks().get(i).getDeck_name());
adapter3.add(CardCount);
}
or
for (Deck deck: deckList) {
String deckName = deck.getDeck_name();
adapter2.add(deckName);
int count = db.getCardIds(deck).length;
String strCount = Integer.toString(count);
adapter3.add(strCount);
}
Overall, I think the second code is clearer, and more readable.
It contains moe variable names that is able to tell what exactly it is used for, such as deckName, count and strCount. I can clearly see that you are getting every deck's name and card count and put them in different (list?) adapters.
For the first one, I apparently needed more time to comprehend what it is doing. So IMO, the second one!
Also if you could just rename getDeck_name to getDeckName that would be better for people to read. getDeckName follows the naming convention for naming Java methods i.e. camelCase.
if you want to get data from simple list thnn foreach loop is good to use but,,, if you want to data from exact position or to store from id than for-loop is better ..
and there is NO difference by performance wise both are same as well, as i know.
as my suggestion use for loop :)
As per this book Code Complete - Steve McConnell's
for loop is good choice when you need a loop that executes a specified number of times.
foreach loop is useful for performing an operation on each member of an array or the container.
for more visit : Google books - Code Complete
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
As you can see down here I have a ListView populated by a JSON but I want to sort the list by date and to have the same items down the corresponding title.
In the image down the the row third and fourth have the same date but in different row.
I do not know what code should I show if the adapter or where I populate the list.
I think U need to sort the populated data date wise in hashmap
hashmap<date,list<other Object>>like this and then use expandable listview to show the populated data..
In the spirit of having less computation processes into the Android client, I suggest you create a new method called findServicesByDate from the server, which should give you the right order based on dates.
However, if this is not possible, the only thing you have to do is order the array based on date, you should check this answer.
Is a basic. Have you ever try to Google it ?
Use a comparator.
Collections.sort(yourList, new Comparator<YourObjectInYourList>() {
public int compare(YourObjectInYourList o1, YourObjectInYourList o2) {
if (o1.getDate() == null || o2.getDate() == null)
return 0;
return o1.getDate().compareTo(o2.getDate());
}
});
This question already has answers here:
WITH statement in Java
(8 answers)
Closed 8 years ago.
I am new to android and I've been trying to create a ListView example application, in which I've added items to Arraylist....
list.add("Item1");
list.add("Item2");
list.add("Item3");
.......
Is there any way in android to avoid typing "List" every Time....?
I know there is something ,But I couldn't find out what the keyword is..
in VB I could write
with list
.add("Item1");
.add("Item2");
Thanks in adavance
Actually, there isn't.. What you can do is use Arrays.asList(...) which you can pass any number of arguments to and you'll get a list with these items back
No, there's no Java equivalent of the Visual Basic with statement.
Check this question.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does suffix âfâ mean in Android programming?
Excuse me if it was a stupid question, but I always see people writing something like 350f and 15f when initializing animations (passing as paramter) . What does f mean?
Thank you
I believe it's the format of the argument, FLOAT in this case.
This question already has answers here:
Difference between getString() and getResources.getString()
(3 answers)
Closed 9 years ago.
I read about String Resources and I understood that you simply use the getString(...) method in order to read the value of a string from res/values/string.xml. Then I read that you can also use getResources().getString(...).
What is the difference between these two ways of obtaining the value of a string?
No any difference. They're both equal.