I'm displaying a List of Objects in a MultipleChoiceDialog. Another List contains all Objects who are already checked.
My Lists:
List<Participant> participants = datasourceParticipant.getAllParticipants();
List<Participant> participantsConference = datasourceParticipant.getAllParticipants(conference.getId());
In order to display them in the MultipleChoiceDialog, I build my List like this:
participantsNames = new ArrayList<String>();
for(int i = 0; i < this.participants.size(); i++) {
participantsNames.add(i, participants.get(i).getFirstname() + " " + participants.get(i).getLastname());
}
participantConferenceNames = new ArrayList<String>();
for(int i = 0; i < this.participantsConference.size(); i++) {
participantConferenceNames.add(i, participantsConference.get(i).getFirstname() + " " + participantsConference.get(i).getLastname());
}
Afterwards, I create the necessary String array ...
final CharSequence[] items = participantsNames.toArray(new CharSequence[participantsNames.size()]);
to display it in the MultipleChoiceDialog
builder.setMultiChoiceItems(items, null, null);
How do I add the checkedItems to the MultipleChoiceDialog. Or is there a much easier way to do it?
You have to pass in a boolean[] instead of null with the values that you want checked. The most straightforward way to accomplish this is to use a set:
Set<Participant> set = new HashSet();
set.addAll(datasourceParticipant.getAllParticipants(conference.getId()));
boolean[] checked = new boolean[participants.size()];
for (int i =0; i < participants.size(); i ++) {
checked[i] = set.contains(participants.get(i));
}
....
builder.setMultiChoiceItems(items, checked, null);
For that to work your Participant class must implement hashCode();
Related
I have two different lists, one is List<MainCategoriesAPI.Datum> datumList = new ArrayList<>(); and second is List<SubCategoryEnt> subCategoryEnts1 = new ArrayList<>();.
What I want is to compare these two lists and get those ids which are not present in datumList. And then, I want to delete the data regarding these ids from SubCategoryEnt.
If you are targeting above Android N
List<String> a1 = Arrays.asList("2009-05-18", "2009-05-19", "2009-05-21");
List<String> a2 = Arrays.asList("2009-05-18", "2009-05-18", "2009-05-19", "2009-05-19", "2009-05-20", "2009-05-21","2009-05-21", "2009-05-22");
List<String> result = a2.stream().filter(elem -> !a1.contains(elem)).collect(Collectors.toList());
or you can use the Collection interface's removeAll method.
// Create a couple ArrayList objects and populate them
// with some delicious fruits.
Collection firstList = new ArrayList() {{
add("apple");
add("orange");
}};
Collection secondList = new ArrayList() {{
add("apple");
add("orange");
add("banana");
add("strawberry");
}};
// Show the "before" lists
System.out.println("First List: " + firstList);
System.out.println("Second List: " + secondList);
// Remove all elements in firstList from secondList
secondList.removeAll(firstList);
// Show the "after" list
System.out.println("Result: " + secondList);
You have to use for loop to find the similar ids and the again use for loop to remove ids from datumList;
List<MainCategoriesAPI.Datum> datumList = new ArrayList<>();
List<SubCategoryEnt> subCategoryEnts1 = new ArrayList<>();
List<Integer> results = new ArrayList<Integer>();// this is for storing same ids
To get different ids
// to get same ids
if (datumList.size() == subCategoryEnts1.size()) {
for (int i=0; i<datumList.size();i++){
int datIds = datumList.get(i);
for (int j=0; j<subCategoryEnts1.size();j++){
int subId = subCategoryEnts1.get(j);
if (datIds!=subId){
results.add(subId);
break;
}
}
}
}
to remove ids
// to remove same id
for (int i=0; i<results.size();i++){
int datIds = results.get(i);
for (int j=0; j<datumList.size();j++){
int subId = datumList.get(j);
if (datIds==subId){
datumList.remove(j);
break;
}
}
}
Hope this will help you.
Check below to find missing items of subCategoryEnts1
List missingIds = new ArrayList<SubCategoryEnt>();
for (SubCategoryEnt subCategory : subCategoryEnts1) {
for (MainCategoriesAPI.Datum datam : datumList) {
if (datam.id == subCategory.id){
missingIds.add(subCategory);
break;
}
}
}
Now remove those from subCategoryEnts1
subCategoryEnts1.removeAll(missingIds);
I want to search app content from Google. I m adding ArrayList in FirebaseAppIndex but not able to search all list items from Google Search, I can able to search only the last item of arraylist.
ArrayList<String> titleList = new ArrayList<>();
titleList.add("ABC");
titleList.add("DEF");
titleList.add("GHI");
titleList.add("KLM");
ArrayList<Indexable> indexableNotes = new ArrayList<>();
for (int i = 0; i < titleList.size(); i++) {
Indexable noteToIndex = Indexables.noteDigitalDocumentBuilder()
.setName(titleList.get(i) + " Note")
.setText("Pierogi")
.setUrl("http://recipe-app.com/recipe/pierogi-poutine")
.build();
Log.d("onHandleIntent", "update");
indexableNotes.add(noteToIndex);
}
for (int i = 0; i < indexableNotes.size(); i++) {
Indexable[] notesArr = new Indexable[indexableNotes.size()];
notesArr = indexableNotes.toArray(notesArr);
Log.d("update", notesArr.toString());
FirebaseAppIndex.getInstance().update(notesArr);
}
}
it is always the same URL, so they overwrite each other and only the last one wins.
(you can also use the firebase appindexing debugging UI to look at the indexed results)
I am trying to read json array without object name, and pass it to a list.
My json looks like :
"facilites": [
"Pool",
" Air Conditioning",
" Pets Allowed",
" Fitness center",
" Kitchen",
" Internet",
" Sona"
]
I am trying to retrieve it using the following code -
for (int l = 0; l < chaletFacilities.length(); l++){
String facilities = chaletFacilities.getString(l);
list = new ArrayList<String>();
list.add(facilities);
}
Inside the main loop I put to my pojo class chalets.setList(list);
The issue is in this line list.add(facilities); it only add the last element. After looping through all, list carry sona only.
Your list should be instantiated outside the loop.
list = new ArrayList<String>();
for (int l = 0; l < chaletFacilities.length(); l++){
String facilities = chaletFacilities.getString(l);
list.add(facilities);
}
An improvement would be directly add string to list instead of capturing it into a string variable like list.add(chaletFacilities.getString(l))
Move initialization of your ArrayList outside of your loop.
Do like this
list = new ArrayList<String>();
for (int l = 0; l < chaletFacilities.length(); l++){
list.add(chaletFacilities.getString(l))
}
What you doing is initializing yourlist again and again and adding the element. So while last iteration the list is getting initialized again and only single element is being added to it.
ArrayList list = new ArrayList<String>();
for (int l = 0; l < chaletFacilities.length(); l++){
String facilities = chaletFacilities.getString(l);
list.add(facilities);
}
You are creating the new list always. So your list size will be 1 with the last value in chaletFacilities array.
Solution: Keep your list initialization outside the for loop as below, and add all the values under the array into single list you created in the top.
list = new ArrayList<String>();
for (int l = 0; l < chaletFacilities.length(); l++)
{
list.add(chaletFacilities.getString(l));
}
ok so i create an array that has integers. The array displays five number from the min and max. How can i display all five numbers in a textview or edittext ? I tried:
nameofile.setText(al.get(x).toString());
but it only displays one?
ArrayList<Integer> al = new ArrayList<Integer>();
for (int i = minint; i <= maxint; i++)
al.add(i);
Random ran = new Random();
for (int i = 0; i < 5; i++) {
int x = al.remove(ran.nextInt(al.size()));
String myString = TextUtils.join(", ", al);
lottonumbers.setText(myString);
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(0);
al.add(1);
al.add(5);
al.add(4);
al.add(3);
java.util.Collections.sort(al);//for sorting Integer values
String listString = "";
for (int s : al)
{
listString += s + " ";
}
nameofile.setText(listString);
You're currently only printing out one element (the one at index x). If you want to print them all in order, you can just join them using TextUtils.join().
Update: After seeing your edit, I think there's a better way to go about what you're trying to do. Instead of trying to pull the values one at a time, and update the list, why not just shuffle them, then use the above method?
Update 2: Okay, I think I finally understand your problem. Just a simple change, then.
ArrayList<Integer> al = new ArrayList<Integer>();
for (int i = minint; i <= maxint; i++)
al.add(i);
Random ran = new Random();
StringBuilder text = new StringBuilder(); // Create a builder
for (int i = 0; i < 5; i++) {
int x = al.remove(ran.nextInt(al.size()));
if (i > 0)
text.append(", "); // Add a comma if we're not at the start
text.append(x);
}
lottonumbers.setText(text);
al.get(x).toString() will only get the value at index "x". If you want to display all values, you need to combine all of the values from the array into a single string then use setText on that string.
You are only showing one number of your array in the TextView, you must to concat the numbers to see the others results like:
for(Integer integer : al) {
nameofile.setText(nameofile.getText() + " " + al.get(x).toString());
}
Then i think you can show all number in one String.
I want merge two arrayList list into one
list1 and list 2 contains
list1 : ahm,sam,ram
list2 :1 1 0
i want to put the first position of list1 with first position to list 1
ex: want to call (ram,0) (ahm,1) and so on....?
ArrayList<String> list1=new ArrayList<String>();
ArrayList<String> list2=new ArrayList<String>();
So you want keys and values into one list? Then you will need a HashMap in order to get this:
HashMap<String, Integer> myMap = new HashMap<String, Integer>();
for(int i = 0; i < list1.size(); i++){
myMap.put(list1.get(i), list2.get(i));
}
Edit: so you want to put two elements of each list into a new list, with String format as (x,y) then you need to do the following:
ArrayList<String> list3 = new ArrayList<String>();
for(int i = 0; i < list1.size(); i++){
// if list2 contains string represented with numbers
String str = list1.get(i) + "," + list2.get(i);
// if list2 contains integers
String str = list1.get(i) + "," + String.valueOf(list2.get(i));
list3.add(str);
}
You may write a method to success what you want to do like:
String getPair( int position )
{
return list1.get( position ) +","+ list2.get( position );
}
I dont exacly know what you mean, but probably something like this: (assuming you have two equal sized lists)
for(int i=0; i < list1.size(); i++) {
list1.get(i) = list1.get(i) + "," + list2(i);
}
However, if I understand correctly, you probably need a HashMap:
HashMap<String, Integer> map = new HashMap<String, Integer>();
for(int i = 0; i < list1.size(); i++){
map.put(list1.get(i), list2.get(i));
}
For real merging, use list1.addAll(list2);