搜索视图在Android Studio中显示Data.entity.Cantact.@85c7ce6

byqmnocz  于 2023-03-13  发布在  Android
关注(0)|答案(1)|浏览(78)

在我做的这个项目中,搜索视图工作正常,但如图所示,输出如下所示:
输出:com.mahdi.roomdatabase.data.entity.Cantact.@85c7ce6
密码有什么问题吗?
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);
}
snvhrwxg

snvhrwxg1#

密码有什么问题吗?
适配器正在使用Cantact类的默认toString方法。
请考虑以下几点。
一个叫做学校的类是:-

@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;
    }
}

下面的代码:

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);

结果:-

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

例如,School类的默认toString方法打印有关School对象示例的详细信息。
但是,如果学校类别更改为包括:

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

那么结果就是

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

你要么
1.以字符串的形式传递您想要显示给适配器的实际值,
1.使用定制的适配器提取您想要显示的值,

  • 或者重写Cantacts toString方法以返回要显示的String。
  • 注意,3可能是最不优选的,尽管是最简单的。

相关问题