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());
}
});
Related
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 2 years ago.
Improve this question
I have a list of courses and I need to filter them while searching
I need to be able to search for every word the user enters
For example: if I have course called nutrition basics, If I enter in search bar basics nothing show up but if i enters nut or even n it works.
Here is my code:
itemFilter: (suggestion, input) => suggestion.name .toLowerCase() .startsWith(input.toLowerCase()),
How can I do the filter as I want? I need to search even if for letter 'b' and still get the result nutrition basics.
I'm using flutter and firebase.
Note: I tried contains insteadof startWith, but if i have two courses one nutrition basics and other math 500 basics and i searched for word basics the result shows after enter is only nutrition, also i need to be able to search for letters too, as b in basics or c..
Thanks.
You have used method startsWith
itemFilter: (suggestion, input) => suggestion.name .toLowerCase() .startsWith(input.toLowerCase()),
use contains instead of startsWith
itemFilter: (suggestion, input) => suggestion.name .toLowerCase() .contains(input.toLowerCase()),
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 details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am a beginner in Android, and understand only very basically that HashMap class enables key/value pairs. But how does this translate into actually using this in an Android app? Could someone provide a simple, plain English example of what case you might want to use HashMap in an app? I cannot imagine a case where I might need it. Make up an Android app idea, if needed. Thanks in advance.
I am looking for a "big picture" analysis that will give some examples where you might use HashMap with certain Android functionalities you are trying to implement.
HashMap or Map interface is not new on android, This is Java Collections framework.
Java collection are meant to be used in several cases to hold data and contain 3 interfaces:
List - Basically simple list,or linked list implementations
Set - The same as list but won't hold 2 equal obejcts(You need to implement you own equals and hashcode)
Map - as you said key value pair.
Uses:
List - For anything, just to hold data
Set - For list of data that we want that all of the items will be unique.
Map - Key value and the most common example is the use for DB items, or something with ids.. for example:
bookId, Book.. I that case you can take the object by id.. This is the most common
I attached link for Java collection tutorial.. It is very important framework that you have to know if you are going to develop java/android
http://tutorials.jenkov.com/java-collections/index.html
Hope that helps
We could use HashMap to keep a list of employess together with their respective salaries.
We can do:
HashMap<String, Float> emplMap = new HashMap<String, Float>();
emplMap.put("fred", 1.000);
for(String name : emplMap.keySet()) {
System.out.print(name + "'s salary is" + emplMap.get(name));
}
Should print
"fred's salary 1.000"
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I just wanna ask for help in getting the index of the given values inside the arraylist. For example I have
arraylist brand contains {"a","b","c","d","e"}
and another
arraylist chosen contains {"a","c","e"}
how will I know the index of the values in arraylist chosen in arraylist brands? Thank you.
I have tried
for (int j=0; j<checkSelected.length; j++)
{
for (int i = 0; i < Brands.size(); i++)
{
checkSelected[i] = true;
}
}
What I am trying to do is to get the index of my values in my chosen arraylist to to the arraylist brand so that will be easy to set the value of that index in true in the other arraylist. But in my code above it only gets the size of the of my arraylist.
Just iterate over chosen and get every index.
for(String chosenElement : chosen) {
int index = brand.indexOf(chosenElement);
// Do something with the index
}
This works not only with lists of String but with every type where equals() is properly overwritten.
Iterate over the 'chosen' list and use indexOf(Object) for each object.
there are methodes indexOf and contains. Check them out
I'm creating a simple quiz game. In this game, the question and the respective answers will be randomly generated. I created a list "q" to the questions.
And for the answers, I created various lists with 4 strings each one. For example, if the question is the 0 in the q list, the answers for this questions will be in the list "a0", right? But I'm having some problem to get the strings in the list of answers. I've tried this:
while(true){
Integer nxt = rng.nextInt(6);
if (!generated.contains(nxt))
{
generated.add(nxt);
textView1.setText(((ArrayList<String>) q).get(nxt));
String x;
x = ("a" +nxt);
Collections.shuffle((x));
btn1.setText(((ArrayList<String>) x).get(0));
btn2.setText(((ArrayList<String>) x).get(1));
btn3.setText(((ArrayList<String>) x).get(2));
btn4.setText(((ArrayList<String>) x).get(3));
break;
}
}
I created a string "x" to get the right list. If the "nxt" is 4, the buttons texts will get the strings in the list a4.
But in my code, the "Collections.shuffle" and the "setText" try to find the list "x". It's not going in the way I imagined.
How can I fix it?
*My idea is check the string of the button clicked and compare with another list of RIGHT answers. In that way, I can attributte the right answer and the other 3 wrong.
I made a similar quiz app (King of Math) a few days ago.
Calculate the correct answers
Add the correct answer to your answers-list
Calculate fake answers, add them to the answers-list
Shuffle the list
Get the id of the correct answer. It is in the range [0, max_answers)
If an answer has been selected, you check if the selected id (0, 1, 2, 3) is the one of the correct answer. If it is, the user picked the right one, otherwise he didn't.
PS: sorry for the self-promotion.
I would be surprised if this code would compile and/or run correctly at all. You are trying to use the content of a String as an variable name, cast that variable to an ArrayList<String> and then to access the elements. This is wrong on so many levels that you should consider doing a few Java tutorials again.
If you do or feel that you can continue anyways, try this approach: You shouldn't store the questions and answers in a separate list, but together in a class.
class Question
{
//...
// maybe id and other stuff belonging to a question
//...
String questionText;
// separate because you need to tell the correct answer apart from the wrong ones later
// you could also just always use the first one in a set of answers.
String correctAnswerText;
ArrayList<String> wrongAnswerTexts;
}
Then you can store your questions in an ArrayList<Question> in your app and set an answer as follows:
//...
// set up ArrayList<Question> questions here
//...
int nxt = rng.nextInt(6);
//...
// make sure your list is actually long enough for the generated index
//...
Question nextQuestion = questions.get(nxt);
//...
// make sure the retrieved object is valid
//...
// set the question text to nextQuestion.questionText;
//...
ArrayList<String> allAnswers = new ArrayList<String>();
allAnswers.add(nextQuestion.correctAnswerText);
allAnswers.addAll(nextQuestion.wrongAnswerTexts);
Collections.shuffle(allAnswers);
btn1.setText(allAnswers.get(0));
btn2.setText(allAnswers.get(1));
btn3.setText(allAnswers.get(2));
btn4.setText(allAnswers.get(3));