How do you make these two array lists into one array list so that it still works in my arrayAdapter. I cant remember how to go about doing this.
public class MyArrayAdapter extends ArrayAdapter<String>{
Context context;
ArrayList<String>mtitle;
ArrayList<String>mdesc;
public MyArrayAdapter(Context c, ArrayList<String>title, ArrayList<String>desc) {
super(c, R.layout.single_row, R.id.viewtitle, title);
...
use java been type array list.
Like:-
public class JBContacts {
public String strID = "";
public String strTitle = "";
public String getStrID() {
return strID;
}
public void setStrID(String strID) {
this.strID = strID;
}
public String getStrTitle() {
return strTitle;
}
public void setStrTitle(String strTitle) {
this.strTitle = strTitle;
}
}
ArrayList<JBContacts> mArrayListContacts;
JBContacts mJbContacts;
mArrayListContacts = new ArrayList<JBContacts>();
mJbContacts = new JBContacts();
strId = mJsonObjectContacts.getString(Constants.TAG_ID);
strTitle = mJsonObjectContacts
.getString(Constants.TAG_TITLE);
mJbContacts.setStrTitle(strTitle);
Related
How can I use Binary Search with an ArrayList?
Here are elements of the ArrayList:
public class DictionaryElements implements
Comparable<DictionaryElements>, Comparator<DictionaryElements>{
private String word;
private String translation;
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
public String getTranslation() {
return translation;
}
public void setTranslation(String translation) {
this.translation = translation;
}
public DictionaryElements() {
}
public DictionaryElements(String word, String translation) {
this.word = word;
this.translation = translation;
}
#Override
public String toString() {
return word + " - " + translation;
}
#Override
public int compareTo(DictionaryElements dictionary) {
return this.word.compareTo(dictionary.word);
}
#Override
public int compare(DictionaryElements wordOne, DictionaryElements wordTwo) {
return wordOne.getWord().compareTo(wordTwo.getWord());
}
}
Than I sorted a list here:
public class DictionarySorter {
ArrayList<DictionaryElements> dictionaryList = new ArrayList<>();
public DictionarySorter(ArrayList<DictionaryElements> dictionaryList) {
this.dictionaryList = dictionaryList;
}
public ArrayList<DictionaryElements> getSortedByWord() {
Collections.sort(dictionaryList);
return dictionaryList;
}
}
And here I tried to imply Binary Search:
public class Main {
public static void main(String[] args) {
DictionaryElements dictionaryElements = new DictionaryElements();
ArrayList<DictionaryElements> dictionaryList = new ArrayList<>();
DictionarySorter dictionarySorter = new
DictionarySorter(dictionaryList);
boolean found = true;
dictionaryList();
Scanner scanner = new Scanner(System.in);
System.out.println("Write one word in English:");
String wordInEnglish = scanner.nextLine();
int index = Collections.binarySearch(dictionaryList, wordInEnglish);
if (found) {
//code here.
}
else {
System.out.println("Sorry, i didn't find " + wordInEnglish + " ;(");
}
scanner.close();
}
public static void dictionaryList() {
ArrayList<DictionaryElements> dictionaryList = new ArrayList<>();
dictionaryList.add(new DictionaryElements("Apple", "Apfel"));
dictionaryList.add(new DictionaryElements("Pear", "Birne"));
dictionaryList.add(new DictionaryElements("Orange", "Orange"));
DictionarySorter dictionarySorter = new DictionarySorter(dictionaryList);
ArrayList<DictionaryElements> sortedDictionaryList = dictionarySorter.getSortedByWord();
for (DictionaryElements dictionary : sortedDictionaryList) {
System.out.println(dictionary);
}
}
}
Error says:
The method binarySearch(List<? extends Comparable<? super T>>, T) in the type Collections is not applicable for the arguments (ArrayList, String)
What did I missed and how can I fix this?
If you have a look at the method declaration for Collections.binarySearch the comparator implemented must be of the same type as the key being passed in.
Your DictionaryElements extends Comparable of type DictionaryElements type but the key you are passing in is of type String.
You'll need pass a DictionaryElement as the key instead:
DictionaryElements key = new DictionaryElements(wordInEnglish, translation);
int index = Collections.binarySearch(dictionaryList, key);
https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#binarySearch(java.util.List,%20T)
You have a list of DictionaryElements objects, and are looking for a String.
That's like me handing you a bag of apples and asking: Hey, find me this pear.
The compiler is helping you out and preventing you from writing this code, as it would make no sense.
I want to order nodes' chosen string variables. this is an homework due to tomorrow.
public void sortSongName() {
DefaultSongs sortedList = new DefaultSongs();
int temp= Integer.MAX_VALUE;
Song curr=root;
Song hold = null;
while(root.nextSong != null) {
if(curr.getSongName().charAt(0)<hold.getSongName().charAt(0)) {
hold=curr;
curr=root.nextSong;
}else {
curr=root.nextSong;
}
sortedList.createSong(root.nextSong.getSongName(),root.nextSong.getBandName() , root.nextSong.getDuration());
deleteSong(root.nextSong.getSongName());
sortSongName();
}
}
Assuming your song class look something like this
public class Song {
private String name;
public String name() {
return name;
}
public void setName(final String name) {
this.name = name;
}
}
And the DefaultSongs class is just a repo with a list of Songs
public class DefaultSongs {
private final List<Song> songList;
public DefaultSongs() {
this.songList = new ArrayList<>();
}
public List<Song> songList() {
return songList;
}
}
Simplest way would be to use java stream
public void sortSongsByName(){
songList.stream().sorted(Comparator.comparing(Song::name));
}
The simplest way is to use Collections.sort();
For example:
List<String> songs = Arrays.asList("Bam","Cam", "Am","Dam");
Collections.sort(songs);
System.out.println(songs);
This will give you the list in alphabetical order.
I already filtered a group of object against a specific string entered in the EditText and now I need to sort that list with the position of the specified string, how can I do that?
I am already done this
Filter Function
public void setFilter(String query) {
visibleList = new ArrayList<>();
query = query.toLowerCase(Locale.getDefault());
for (AccountProfile accountProfile : accountProfileList) {
if (accountProfile.getName().toLowerCase(Locale.getDefault())
.contains(query))
visibleList.add(accountProfile);
}
Collections.sort(visibleList, new AccountNameComparator());
}
AccountNameComparator
public class AccountNameComparator implements Comparator<AccountProfile> {
#Override
public int compare(AccountProfile first, AccountProfile second) {
return first.getName().compareTo(second.getName());
}
}
the list is sorted but it is based on the getname() I need to sort the list with specific substring of the getname()
To sort that list with the position of the specified string, you could try something like this:
public class AccountNameComparator implements Comparator<AccountProfile> {
private final String query;
public AccountNameComparator(String query) {
this.query = query;
}
#Override
public int compare(AccountProfile first, AccountProfile second) {
Integer f = first.getName().indexOf(this.query);
Integer s = second.getName().indexOf(this.query);
return f.compareTo(s);
}
}
There is slight change in above answer: like below
public class AccountNameComparator implements Comparator<AccountProfile> {
private final String query;
public AccoluntNameSortComparator(String query) {
this.query = query;
}
#Override
public int compare(AccountProfile first, AccountProfile second) {
String firstName = first.getName().toLowerCase();
String secoundName = second.getName().toLowerCase();
query = query.toLowerCase();
Integer f = firstName.indexOf(query);
Integer s = secoundName.indexOf(query);
return f.compareTo(s);
}
}
I've got a problem with my programm. When i try to compile following i just receive the message:
Tutorium.java:15: error: <identifier> expected
public void settName(vorlesung.lectureName) {
^
So my Code:
Tutorium.java
public class Tutorium {
private Vorlesung vorlesung;
public String tName;
private int tNumber;
public int gettNumber() {
return this.tNumber;
}
public String gettName() {
return this.tName;
}
public void settName(vorlesung.lectureName) {
this.tName = vorlesung.lectureName;
}
public String toString() {
return (this.tName + ", " + this.tNumber);
}
public Tutorium(int tNumber){
this.tNumber = tNumber; } }
Vorlesung.java
public class Vorlesung {
public String lectureName;
private int lectureNumber;
private int lecture;
private Dozent dozent;
private String lecturerlName;
public String getlectureName(){
return this.lectureName;
}
public int lectureNumber(){
return this.lectureNumber;
}
public int lecture(){
return this.lecture;
}
public String getlecturer(){
this.lecturerlName = dozent.lecturerlName;
return this.lecturerlName;
}
public String toString() {
return (this.lectureName + ", " + this.lectureNumber);
}
public Vorlesung(String lectureName, int lecture) {
this.lectureName = lectureName;
this.lecture = lecture +1;
this.lectureNumber = this.lecture -1;
this.lecturerlName = lecturerlName;
}}
My Main-Method:
public class MainVorlesung {
public static void main(String[] args) {
Student student = new Student("STUDENTNAME", "STUDENTLASTNAME", 178, 1);
Vorlesung vorlesung = new Vorlesung("Programmieren", 13341);
Tutorium tutorium = new Tutorium(3);
Dozent dozent = new Dozent("LECTURERFIRSTNAME", "LECTURERLASTNAME", 815);
System.out.println(student.toString());
System.out.println(vorlesung.toString());
System.out.println(tutorium.toString());
System.out.println(dozent.toString());
}}
My goal is to set the value of tName equal the value of vorlesung.lectureName.
Why can't i do this that way?
I appreciate every help. :)
Thanks
For methods, the arguments that you pass in must have a declared value.
In this case, a String. So you need to change your method to this:
public void settName(String newLectureName) {
this.tName = newLectureName;
}
Read more about what a java method is and how to create one here: http://www.tutorialspoint.com/java/java_methods.htm
Change settName to
public void settName(String name) {
this.tName = name;
}
Since your goal is:
My goal is to set the value of tName equal the value of vorlesung.lectureName.
You should get rid of the setName method entirely since it will depend entirely on the vorlesung field and so should not be changeable. You should also get rid of the tName field, and instead change getName() to:
public class Tutorium {
private Vorlesung vorlesung;
// public String tName; // get rid of
private int tNumber;
public String gettName() {
if (vorlesung != null) {
return vorlesung.getlecturer();
}
return null; // or throw exception
}
// *** get rid of this since you won't be setting names
// public void settName(Vorlesung vorlesung) {
// this.tName = vorlesung.lectureName;
// }
I have just now noticed that your Tutorium class does not have and absolutely needs a setVorlesung(...) method.
public void setVorlesung(Vorlesung vorlesung) {
this.vorlesung = vorlesung;
}
Needing to create an unspecified number of objects, I tried to create a builder that do that. All was well until I realized that my builder creates all objects with their properties having the same values.
So when I call the builder:
ValidationHelper v = new ValidationHelper.HelperBuilder()
.addHelper("ICAO Identifier", icaoIdentifier, rulesICAO)
.addHelper("Long Name", longName, rulesLongName)
.build();
... I'll have 2 objects and their properties will have values of the last object the builder was asked to create.
To start with, is factory builder the prudent approach to this? Secondly, is my builder salvageable?
Builder:
public class ValidationHelper {
private static ArrayList<HelperBuilder> validatorHelpers = new ArrayList();
public static class HelperBuilder {
private String txtFieldName;
private String txtFieldValue;
private List<Integer> valCodes = new ArrayList<Integer>();
private ArrayList<HelperBuilder> innerValidatorHelpers = new ArrayList<HelperBuilder>();
public HelperBuilder() {}
public final HelperBuilder addHelper(String txtFieldName, String txtFieldValue, int[] validationCodes) {
this.txtFieldName = txtFieldName;
this.txtFieldValue = txtFieldValue;
for( int i = 0; i < validationCodes.length; i++ ){
getValCodes().add((Integer) validationCodes[i]);
}
innerValidatorHelpers.add(this);
return this;
}
public final ValidationHelper build() {
return new ValidationHelper(this);
}
public String getTxtFieldName() {
return txtFieldName;
}
public String getTxtFieldValue() {
return txtFieldValue;
}
public List<Integer> getValCodes() {
return valCodes;
}
}//end HelperBuilder
private ValidationHelper(HelperBuilder helperBuilder) {
validatorHelpers = helperBuilder.innerValidatorHelpers;
}
public void setHelpers(ArrayList validatorHelpers) {
validatorHelpers = validatorHelpers;
}
public ArrayList getHelpers() {
return validatorHelpers;
}
}
EDIT/FIXED:
So for what it's worth, here's the revised builder. It needed another constructor that could properly initialize an instance of what it's supposed to build.
public class ValidationHelper {
private static ArrayList<HelperBuilder> validatorHelpers = new ArrayList();
public static class HelperBuilder {
private String txtFieldName;
private String txtFieldValue;
private List<Integer> valCodes = new ArrayList<Integer>();
private ArrayList<HelperBuilder> innerValidatorHelpers = new ArrayList<HelperBuilder>();
public HelperBuilder() {}
public HelperBuilder(String txtFieldName, String txtFieldValue, int[] validationCodes) {
this.txtFieldName = txtFieldName;
this.txtFieldValue = txtFieldValue;
for (int i = 0; i < validationCodes.length; i++) {
valCodes.add((Integer) validationCodes[i]);
}
}
public final HelperBuilder addHelper(String txtFieldName, String txtFieldValue, int[] validationCodes) {
innerValidatorHelpers.add( new HelperBuilder(txtFieldName, txtFieldValue, validationCodes) );
return this;
}
public final ValidationHelper build() {
return new ValidationHelper(this);
}
public String getTxtFieldName() {
return txtFieldName;
}
public String getTxtFieldValue() {
return txtFieldValue;
}
public List getValCodes() {
return valCodes;
}
}//end HelperBuilder
private ValidationHelper(HelperBuilder helperBuilder) {
validatorHelpers = helperBuilder.innerValidatorHelpers;
}
public ArrayList getHelpers() {
return validatorHelpers;
}
}
Each time you just overwrite the values in
private String txtFieldName;
private String txtFieldValue;
and the last one winns. So you create only 1 HelperInstance here
ValidationHelper v = new ValidationHelper.HelperBuilder()
and the fields name and value are overwritten each time you call addHelper(). But you need to create an instance for each "configuration". So addHelper should create a new Instance and add it into
private ArrayList<HelperBuilder> innerValidatorHelpers = ...;
If you want to build objects with different values you have to either
alter the builder between creating the objects so it will build something different.
instruct the builder to change the values automatically e.g. use a counter, or filename based on the date, or provide a list of values.