Sorting a list with integers android [closed] - android

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Should I use the comparable or comparator when I want to sort my list based on my own personal object?
should I use the collection operation in my recycler view adapter?

Is it a list of integers (as you specify in your title)?
you can use Arrays#sort
Arrays.sort(someArray, new Comparable<Integer>() {
public int compare(Integer a, Integer b) {
return a.compareTo(b);
}
});

If it's just a list of integers, and they are in for instance a List object, you don't have to use a comparator, unless you need to parse the integers as something other than plain numbers.
Collections.sort(myList);
If it's one of your own objects, look at Tyler Sebastians answer. Collections comes with a similar method for lists.
Collections.sort(myList, new MyComparator());

Related

Method of passing object to another activity [closed]

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 4 years ago.
Improve this question
Let's say I have a RecyclerView filled with Car objects. A Car has a lot of variables like ID, size, colour, logo, etc. Every car is saved to the database.
Now, when I want to open the DetailActivity about the specific Car, should I pass the whole object into the new activity OR just an ID and get data from database in DetailActivity again?
Which solution will be more relevant and faster?
If you need to pass one or two properties then its fine. If it is more than two then it is preferred if you use id and query it from DB.
First you should make your object implement Parcelable.
Once you have your object implemented the Parcelable, you attach it to the Intent like this:
Intent intent = new Intent();
intent.putExtra("extra_object_1", your_parcelable_object);
When you are ready to pull the object, use intent.getParcelableExtra():
Intent intent = Context.getIntent();
MyParcelable obj = (MyParcelable) intent.getParcelableExtra("extra_object_1");
If object size is small, you should pass the data through the intent using Parcelable. if data size is large then pass the id to you activity and then get the data from database.
Based on this Answer you can pass 1 MB of data in Intent.
So choose the approach based on the situation.

search text in a website programmatically [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
i want my app to search a specific website(from an url) for a specific text and give me the 3 chars after that text or to search for a pattern with a placeholder and give me the first matching string. Is that possible? There is no need to display the website.
Once you download the page using something like HTTPUrlConnection, you can use a regex with your search term
Pattern p = Pattern.compile("specific text(\w\w\w)");
Matcher m = p.matcher(site_text);
boolean b = m.matches();
The three \w will be captured in a group for you to use if there's a match.

which code set is easier to read/maintain? [closed]

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

how do i user pattern matches like search engine [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I want to generate Search Suggestions that matches the input in the AutoCompleteTextView.
It will check for the matches for the first word I type then all the related words should be populated.
After I type next character then it will match combination of that word and populate the result .
I do not understand how to do pattern matches.
For example: Suppose, there are three country names.
INDIA, IRAQ, IRAN.
When I type I then I want get All Country names Starting with I to be displayed.
When I type IR then result should be IRAN,IRAQ. ETC.
Your requirement will be satisfied in AutoCompleteTextView itself.
Threshold is used to set from the length where auto complete show it's suggestion:
For ex:
String[] languages={"INDIA", "IRAQ","IRAN"};
AutoCompleteTextView text(AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,languages);
text.setAdapter(adapter);
text.setThreshold(1);

Place data from jsonobject into AutoCompleteTextview [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I need to get data from http://schedule.sumdu.edu.ua/index/json?method=getTeachers, parse it and load into AutoCompleteTextview. Any suggestions?
You need to access the service URL using an AsyncTask<>, then later on get its response into a String object.
And, parse it, using JSONObject/Json Array present in android. You will get many examples for this.
Later on you can create a String array, load your data in it, and set it for auto complete for text view.
Here is an example for this.
String[] listTeachers; // initialize this with teachers JSON data
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.layout_teacher_list, listTeachers);
tvTeacher.setAdapter(adapter);

Categories

Resources