ArrayList not deleting properly - android

I have a screen where the user selects items he/she wants deleted, then I store his/her choices into an integer arrayList (based on which positions they clicked on the listView).
I then pass the items that they want deleted to a function where I actually delete them from my hashtable that holds all the information. For some reason though, it's just NOT deleting from the hashtable. I've tested and looked at all the variables I'm using to access the data and they are correct. I'm not sure why it's not removing what I tell it to..
Here is the function:
for (Entry<Integer, ArrayList<Deck>> i : listOfDecks.entrySet()) {
for (int p = 0; p < i.getValue().size(); p++) {
if (i.getValue().get(p).getTitle().equals(deckTitle)) {
for (int z = 0; z < deletedItems.size(); z++) {
listOfDecks.get(i.getKey()).get(p).getDeck().remove(deletedItems.get(z));
}
}
}
}
deletedItems is the arrayList that holds what the user has selected as their items they want deleted.

What I see is that you are calling Deck.remove(int) so you're not removing from a hashtable, but from a deck.
What does this method remove(int) of the class Deck do?.

Related

mongoose nodejs sort by data based on another collection

I am making an app with NodeJS and MongoDB using RESTFUL API. Android on the client side.
I have two collections:
Images which contain
user id
title,description, image string etc
array of tags (tags: [{name: blah}]
And User collection:
user name, email, password etc
array of tags ( tags: [{name:blah, score: 2}]
For every image that a user adds I add all the tags of the image to the user with score 2 if they don't exist or increase the score by 2 if the tag exists for the user.
I want to get all the images sorted by the user's score - for instance if the user has tags like that - {name: baseball, score:2},{name: soccer, score:4}
the image with soccer tag will be first and after that the image with baseball tags.
I tried doing that on the client side(Java) and it requires a lot of loops and inefficient code.
However, using GET I can't send the user's data (the tags with the score).
What is the correct way to do that?
Edit: I managed to do that by populating the user_id field in images schema.
Now I can get all images with all the user's data.
On the client side I am sorting the images by their user's score - looping the user's tags by their score from the highest value to the lowest and adding each image to a new list.
Then I add the rest of images to the temporary list and create the recycler view with it:
List<UserTags> sortedUserTagsListByScore = userTags;
Collections.sort(sortedUserTagsListByScore, new Comparator<UserTags>() {
#Override
public int compare(UserTags lhs, UserTags rhs) {
return rhs.getScore().compareTo(lhs.getScore());
}
});
// reverse user tags for ascending order
// Collections.reverse(userTags);
// loop all user tags
for (int i = 0; i < userTags.size(); i++) {
// loop all images
for (int j = 0; j < imagesArray.size(); j++){
// loop all tags of an image
for (int k = 0; k < imagesArray.get(j).getTags().size(); k++) {
// checks if the tag of the image equals the user tag
if (imagesArray.get(j).getTags().get(k).toString().equals(userTags.get(i).getName())){
// for debugging
// imagesArray.get(j).setUpvotes(userTags.get(i).getScore());
tempImagesArray.add(imagesArray.get(j));
Log.v("sort","tag found :" + imagesArray.get(j).getTags().get(k) + " score: " + userTags.get(i).getScore());
// remove image and insert false image
imagesArray.remove(j);
imagesArray.add(j,new Image("REMOVE"));
break;
}
}
}
}
// delete images that added to tempImagesArray
for (Image image :
new ArrayList<>(imagesArray)) {
if (image.getId().equals("REMOVE"))
imagesArray.remove(image);
}
// sort images without user tag by upvotes
Collections.sort(imagesArray, new Comparator<Image>() {
#Override
public int compare(Image lhs, Image rhs) {
return rhs.getUpvotes().compareTo(lhs.getUpvotes());
}
});
// add images without user tag to sorted images list
tempImagesArray.addAll(imagesArray);
imagesArray = tempImagesArray;
createCards();

Alphabet Indexed ListView Without a cursor

I receive data from a server using JSON and I want to order them alphabetically with alphabet indexed section and store them in a ListView.
Maybe something that will happen in :
for(int i=0;i<jArray.length();i++){
// here
}
I read that you can order elements like that only using a cursor. In my case would be very inefficient to store the elements from the server in the database and read them again. Waste of time and memory.
So, I am asking you if there could be any solution for my problem : order alphabetically with alphabet indexed section string received from JSON .
EDIT: I want my listview to look like this http://eshyu.wordpress.com/2010/08/15/cursoradapter-with-alphabet-indexed-section-headers/ . I mean with those sections . All tutorials I found said that you need to fetch information with a cursor. My question was if I could't do this wihout a cursor, because it would be a waste of memory to store them in the local database too.
You may need to parse the JSON Array :
List<Project> list = new ArrayList<Project>();
for (int i = 0; i < jArray.length(); i++) {
JSONObject obj = (JSONObject) jArray.get(i);
project = new Project();
project.setId( Long.parseLong(obj.get("id").toString()));
project.setKey(obj.get("key").toString());
project.setName(obj.get("name").toString());
list.add(project);
}
You can use the comparator class like this to sort them :
Collections.sort(list), new Comparator<Project>() {
public int compare(Project p1, Project p2) {
return p1.getKey().compareToIgnoreCase(p2.getKey());
}
});
You can also have Project class and implements Comparable:
public class Project implements Comparable<Project> {
private long id;
private String key;
private String name;
public int compareTo(Project p) {
if (this.key > p.key)
return -1;
else if (this.key < p.key)
return 1;
return 0;
}
}
And then sort the list by Collections.sort(list);
My suggestion is try to sort the data in the Server-side, because the memory of the phone is limited and it may make you application time consuming to show the data, but you do not have memory limitation problem in the Server-side.
use a comparator to sort the arraylist as described here . And then use an ArrayAdapter to show the items in Listview

How to sort list view data which is backed with API Response and containing more then one field in each row

I want to sort data in ListView. Which contains title,description ,price at their respective position in each row. I want to implement sort functionality on title in alphabetical order. All field are stored in different array. After sorting each filed should according title. What to do? Please explain with detail example.
Create one Bean Class
Your array
array 1) title[]
array 2) description[]
for example DataBean
public class DataBean
{
String title,desc;
DataBean(String title,String desc)
{
this.title=title;
this.desc=desc;
}
}
Use this code to merge data
Create Vector;
ex: Vector vec = new Vector;
for(int i=0; i< yourArray.length; i++)
{
vec.add(title[i],description[i]);
}
Now you have vec of your date.
Just perform SORT on any field.
either title or desc.
You will get your sorted data.

For-loop not working the way i want it to

I have a extremely minor issue that I can't seem to figure out. I'm trying to extract data based on a type of value from an ArrayList> and place it into another ArrayList. The issue is that the for-loop only runs once, which in this case i need it to traverse the entire array and then place the data into the unSuppressedData arraylist.
Below is the for-loop:
for (int x = 0; x < suppressedStatus.length; x++) {
for (int i = 0; i < availData.size(); i++) {
Hashtable<String,String> checkAvail = availData.get(i);
String itemStatus = checkAvail.get("loanStatus");
if (unSuppressedData.contains(checkAvail) == false) {
if (!(itemStatus.equals(suppressedStatus[x]))) {
Log.d("Item Status", itemStatus);
Log.d("Suppressed Status", suppressedStatus[x]);
unSuppressedData.add(checkAvail);
//break;
}
}
}
}
suppressedStatus is a String array
availData is the arraylist i want to extract data from
unSuppressedData is the arraylist i want to place the data in
I believe that it only runs once is due to this line of code:
if (unSuppressedData.contains(checkAvail) == false) {
But i need to this line to check whether my unSuppressdData has the data, if no then will add the data from availData arraylist into unSuppressedData arraylist.
Could it be that i'm writing this piece of code wrongly? Appreciate any insights shed on this.
A good collection type for this sort of thing is the LinkedHashSet. Because it's a set, each element can only be added once. Being a hash, the contains test is quick. Being 'linked' the resulting set is iterated in insertion order.

can't get the text from an autocomplete

I have to create an app in android with a database.In that database I have a predefined list of products.
Now,the thing is that my ap has to offer to the user the posibility to introduce in that list some other products which are not in the list.
To this end, I've created an autocomplete text view in which I introduce a new product and I take the text fro autocomplete and I have to write it in the database
Now,my problem is that when I display the products that I've introduced in the database,the toast text that I use to display what I have in the database it shows me nothing next to "product......".
Now,that may be because when I try to get the text from the autocomplete I get nothing in return?
This is how I read from autocomplete:
mItem = (AutoCompleteTextView) findViewById(R.id.todo_edit_item);
String nou=mItem.getText().toString();
And then I compare nou(which is what I wrote in the autocomplete) with what I have predefnied in the list,so if it is a new product(which was not in the list already) the I add it in the database:
for(int i = 0; i < l; i++)
{
if (nou!=fruits[i])
t=true;
else t=false;
}
if (t==true)
{
db.insertTitle(nou);
fruits=db.getAllfromDB("Fruits","fruit");
l=l+1;
}
Anyone any ideas of what I'm doing wrong in here cause I can't figure out.I'lll be here for further details.Thank u in advance:)
You compare strings using != instead of using !nou.equals(fruits[i]). also you compare to all elements in array each time, since you so t is always the value of the comparison to the last element in the array whether a match was found or not.
It should be written like that:
t = true;
for(int i = 0; i < l; i++)
{
if (nou.equals(fruits[i]))
{
t=false;
break;
}
}
if (t==true)
{
db.insertTitle(nou);
fruits=db.getAllfromDB("Fruits","fruit");
l=l+1;
}

Categories

Resources