SearchView在Android Studio中显示Data.entity.Cantact.@85c7ce6



在我做的这个项目中,搜索视图工作正常,但正如你在图中看到的,输出如下:

输出:com.mahdi.roomdatabase.Data.intity.Cantact.@85c7ce6

这些代码出了什么问题?

输入图像

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

这些代码有什么问题?

适配器使用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. 将实际值作为字符串传递给adpader
  2. 使用自定义适配器提取您想要显示的值
  3. 或者覆盖铁路超高toString方法以返回要显示的字符串。
    • 注意,3可能是最不可取的,尽管是最简单的

相关内容

最新更新