【发布时间】:2017-05-04 20:37:32
【问题描述】:
在我的应用中,我的目标是让用户从他们的画廊上传一张图片,并将其保存在数据库中。每个图像都有一个名称,该名称和图像将显示在列表视图中。列表视图的排序方式类似于电话中的联系人。它们实际上是用于交付目的的物业地图,例如不同的酒店或度假村,甚至学校。我的一切都按照我想要的方式工作,但由于某种原因,我无法按字母顺序排列它们。我尝试了其他示例和 sn-ps,但没有运气。我的代码如下:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.display_hotels);
db = new DatabaseHandler(this);
lv = (ListView) findViewById(R.id.list1);
pic = (ImageView) findViewById(R.id.pic);
fname = (EditText) findViewById(R.id.txt1);
final ArrayList<Hotel> hotels = new ArrayList<>(db.getAllHotels());
data = new dataAdapter(this, hotels);
Collections.sort(hotels, new Comparator<Hotel>()
{
public int compare(Hotel a1, Hotel a2)
{
return (a1.toString()).compareTo(a2.toString());
}
});
lv.setAdapter(data);
}
在我输入代码进行比较之前:
Collections.sort(hotels, new Comparator<Hotel>()
{
public int compare(Hotel a1, Hotel a2)
{
return (a1.toString()).compareTo(a2.toString());
}
});
它只会按照保存的顺序显示图像。但是对于这段代码,似乎有问题。我尝试按顺序保存 3 个不同的属性图像:Westgate Resort、Grand Country Inn 和 College of the Ozarks。出于某种原因,Westgate 排名第一,奥沙克学院第二,Grand Country Inn 第三。如果我添加相同的图像并将其命名为 Grand Country Inn,Westgate Resort 将落在列表的底部。这似乎是正确的,但在添加另一个图像后,Westgate 将返回顶部。任何帮助将不胜感激!感谢您的宝贵时间。
【问题讨论】:
-
您正在尝试比较酒店对象的 .toString()。相反,您应该在酒店对象中有一个属性,让我们说图像字符串并比较它们以使其正确。像这样如果你没有重写一个给出这个字符串的tostring函数,那么当你直接将它转换为字符串时遇到问题是很正常的