Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Search ArrayList of objects for only one paramater, return index of object

I am having a big problem with this, now I have a class called FileRelationship, and I have two constructors with it. Class is shown below. I also overrode the equals method in this class.

public class FileRelationship {
String fileName;
String firstLink;
String secondLink;
String thirdLink;

public  FileRelationship(String fileName) {
    this.fileName = fileName;
}

public  FileRelationship(String fileName, String firstLink, String secondLink, String thirdLink) {
    this.fileName = fileName;
    this.firstLink = firstLink;
    this.secondLink = secondLink;
    this.thirdLink = thirdLink;
}

public boolean equals(Object o) {
    if(o == null) {
        return false;
    }

    if(this == o) {
        return true;
    }

    if(o instanceof FileRelationship) {
        return this.fileName.equals(((FileRelationship)o).fileName);
    }

    return false;
}

}

Now I have an arrayList of FileRelationships and I want to be able to search through this list (These all use the constructor with 4 Strings in it (fileName, firstLink, secondLink, thirdLink)) Now I have a method in another class that is supposed to search this arrayList and this find the index of one that has a matching fileName. When I call this method I am only passing into the constructor with the fileName and that is all. (This is the only I can do it, I will not know the other strings).

This is how I set up the ArrayList of FileRelationships.

        fileRelationships = new ArrayList<FileRelationship>();

    String MasterLine;

    InputStream inputStream = this.getResources().openRawResource(R.raw.masterfile);

    InputStreamReader inputreader = new InputStreamReader(inputStream);
    BufferedReader buffreader = new BufferedReader(inputreader);

    // Creates the arrayList of all the relationships for later use of retreival.
    try {
        while (( MasterLine = buffreader.readLine()) != null) {
            MasterLineArray = MasterLine.split(",");
            String filename = MasterLineArray[0];
            String choice1 = MasterLineArray[1];
            String choice2 = MasterLineArray[2];
            String choice3 = MasterLineArray[3];
            FileRelationship fr = new FileRelationship(filename, choice1, choice2, choice3);

            fileRelationships.add(fr);
        }
    } catch (IOException e) {

    } finally {
        closeQuietly(buffreader);
}

This is my method for finding the relationship and then setting it to the variable I need. I want to be able to find the index of the list and apply it to the variable.

        public void findRelationship(String nextFileName) {
        int pageIndex = fileRelationships.indexOf((new FileRelationship(nextFileName)));
        selectedRelationship = fileRelationships.get(pageIndex);
        currentPage = pageIndex;
    }

EDIT : I'm sorry forgot to say what was wrong. No it doesn't work. pageIndex is returning a -1 (So in other words it isn't finding anything). I don't know what else to do, maybe use HashCode but honestly I've even looked up stuff about it and I can't really figure out how to use that either.

I really really need help on this, as this is for a school project due soon. I have most of the project done as once I get this part finished I should be almost done. Thanks.

like image 699
Mike Shapiro Avatar asked Dec 05 '25 20:12

Mike Shapiro


1 Answers

Search like this

public void findRelationship(String nextFileName) {

for(FileRelationship file:fileRelationships)
{
    if(file.firsFileName.equals(nextFileName))
  {
     currentPage=fileRelationships.indexOf(file);
   }
}

}

list returns -1 when list doesn't contain that object. In your case you where using indexOf() obj by creating new object

 int pageIndex = fileRelationships.indexOf((new FileRelationship(nextFileName)));

this is not the right way. because you are creating new object and list does not contains new object

And also make getter setter method for each fields in FileRelationship class directly accessing each field is not the good practice

like image 63
FaisalAhmed Avatar answered Dec 08 '25 10:12

FaisalAhmed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!