Home > Back-end >  SearchView shows Data.entity.Cantact.@85c7ce6 in Android Studio
SearchView shows Data.entity.Cantact.@85c7ce6 in Android Studio

Time:08-24

In this project that I did, the search view works properly, but as you can see in the picture, the output is like this:

output: com.mahdi.roomdatabase.Data.entity.Cantact.@85c7ce6

what's wrong with this codes?

enter image

    Codes:


    Observer<DatabaseNew> observer = new Observer<DatabaseNew>() {
        @Override
        public void onChanged(DatabaseNew databaseNew) {


            searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String query) {
                    getDatabasefromDb(query);
                    return true;
                }

                @Override
                public boolean onQueryTextChange(String newText) {
                    getDatabasefromDb(newText);
                    return true;
                }
            });


        }
    };
    mainModel.getLiveData().observe(this, observer);
}


private void getDatabasefromDb(String searchText) {
    searchText = "%"   searchText   "%";
    contacts=databaseNew.getDatabaseInfo(SearchActivity.this, searchText);

    arrayAdapter = new ArrayAdapter(SearchActivity.this, android.R.layout.
            simple_list_item_1, contacts);
    listView.setAdapter(arrayAdapter);
}

public List<Contact> getDatabaseInfo(Context context, String Query) {
    return getContactDAO(context).getContactList(Query);
}

CodePudding user response:

what's wrong with this codes?

The adapter is using the default toString method of the Cantact class.

Consider the following.

A class called School is :-

@Entity(
        indices = {
                @Index(value = "schoolName", unique = true)
        }
)
class School {
    @PrimaryKey
    Long schoolId=null;
    String schoolName;

    School(){}
    @Ignore
    School(String schoolName) {
        this.schoolName = schoolName;
    }
    School(Long schoolId, String schoolName) {
        this.schoolId = schoolId;
        this.schoolName = schoolName;
    }
}

The the following code:-

School school = new School("The School");
Log.d("EXAMPLE","School if you use the default toString method is "   school   " Whilst getting the value (the name) is "   school.schoolName);

results in :-

D/EXAMPLE: School if you use the default toString method is a.a.so73431456javaroomcatchtryexample.School@f6edce8 Whilst getting the value (the name) is The School

i.e. the School class's default toString method prints details about the instance of the School object.

However, if the School class is changed to include :-

@Override
public String toString() {
    return "ID="   this.schoolId   "; SCHOOLNAME="   this.schoolName;
}

Then the result is

D/EXAMPLE: School if you use the default toString method is ID=null; SCHOOLNAME=The School Whilst getting the value (the name) is The School

You need to either

  1. pass the actual values, as strings, you want displayed to the adpater,
  2. use a customised adapter to extract the values that you want displayed,
  3. or override the Cantacts toString method to return the String that you want to be displayed.
    • noting that 3 is probably the least preferable, albeit the simplest.

CodePudding user response:

I could solve this problem by adding this toString() method to the contact class:

@Entity(tableName = "Contact")
public class Contact {

public Contact() {
}

public Contact(String name, int age, int image) {
    this.name = name;
    this.age = age;
    this.image = image;
}

public int getID() {
    return ID;
}

public void setID(int ID) {
    this.ID = ID;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}


@Override
public String toString() {
    return  "outcome: "   name;
}
  • Related