gpt4 book ai didi

android - 奇怪的ListView排序

转载 作者:行者123 更新时间:2023-11-30 02:41:48 26 4
gpt4 key购买 nike

我有以下问题:

Wrong sorted ListView

如图所示,CITROEN 组标题并未位于所有雪铁龙车型之上,例如 DACIA。奇怪的是,大约有 20 个汽车品牌,例如 BMW、AUDI...并且每个组标题都在其子项之上,但 CITROEN 却没有。

此 ListView 是从 html 文件填充的,该文件具有以下结构:

<optgroup label="BMW">    
<option value="225" >BMW X3 3.0si</option>
<option value="226" >BMW X5 3.0d A/T</option>
<option value="227" >BMW X5 4.8i A/T</option>
</optgroup>
<optgroup label="CITROËN">
<option value="67" >CITROËN C1 1.0i</option>
<option value="68" >CITROËN C1 1.4 HDi</option>
<option value="69" >CITROËN C2 1.1i</option>

我正在使用自定义适配器。下面是比较方法的源代码:

 @Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
progressDialog.dismiss();

adapter = new ModelsAdaper(CarsList.this, generateData());

/*Should sort the ListView alphabetically*/
adapter.sort(new Comparator<Model>() {
@Override
public int compare(Model lhs, Model rhs) {
return lhs.getTitle().compareTo(rhs.getTitle());
}
});
setListAdapter(adapter);

generateData() 方法:

private ArrayList<Model> generateData() {
models = new ArrayList<Model>();

/*This loop adds car brands to the listview*/
for(String s: brands){
models.add(new Model(R.drawable.alfa_romeo_icon_52,s));
}

/*This loop inserts car models into the listview*/
int key;
for(int i = 0; i < hashMap.size(); i++) {
key = hashMap.keyAt(i);
models.add(new Model(hashMap.get(key)));
}
return models;
}

最后是模型类

public class Model {
private String title;
private boolean isGroupHeader = false;
private int icon;

/**
* This constructor will be used for creating instance od target_item
* @param title is content of the item
*/
public Model(String title){
this.title = title;
}

/**
* This constructor will be used for group headers
* @param icon is icon of the group
* @param title is name of the group
*/
public Model(int icon, String title){
this.icon = icon;
this.title = title;
isGroupHeader = true;
}

编辑根据要求,这里是 HTMLParser 类源代码。它的构造函数是从 CarsList Activity 调用的,它扩展了 ListActivity

public class HTMLParser {
private String value;
private InputStream is = null;
private Context context=null;
private org.jsoup.nodes.Document document = null;
SparseArray<String> hashMap = new SparseArray<String>();
private ArrayList<String> modelsList = new ArrayList<String>();
private ArrayList<String> brandsList = new ArrayList<String>();

/**
* Constructor is used to pass instance of CarsList Context to get html asset
* @param context instance of the CarsList activity context
*/
public HTMLParser(Context context) throws IOException {
this.context = context;
is = context.getAssets().open("modely aut.html");
document = Jsoup.parse(is,"UTF-8","http://example.com");
}

/**
* The purpose of this method is to parse car brands from html asset
* @return ArrayList of car brands
*/
public ArrayList<String> parseCarBrands(){
Elements models = document.select("optgroup");
for (Element e: models){
brandsList.add(e.attr("label"));
}
return brandsList;
}

/**
* Method parses all car models from html asset. For IO safety operations, it is recommended to call this method
* after parseCarBrands() method, because parseCarModels() method closes inputStream.
* @return SparseArray consisting of key: carID and value: car model
*/
public SparseArray<String> parseCarModels(){
try {
Elements models = document.select("option");
for (Element e: models){
int res = new Scanner(e.toString()).useDelimiter("\\D+").nextInt();
value = e.html();
modelsList.add(value);
hashMap.put(res,value);
}
} finally {
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return hashMap;
}

编辑 2 问题的可能来源我用相同的代码做了一些测试,但只在简单的 java 项目中。它看起来像一些编码问题。使用时

Elements models2 = doc.select("option");

for (Element e: models2){
int key = Integer.parseInt(e.attr("value"));
String modelName = e.html();
modelsList.add(value);
}

System.out.println(modelName) 的输出如下所示:

CITRO&Euml;N C4 1.6i 16V EP turbo

但是当使用 String s = e.attr("label"); 只解析品牌名称时,输出是应该的。您知道问题出在哪里吗?如果有必要,我将发布代码的其他部分。感谢您为我的问题付出的所有时间和精力

最佳答案

我做到了,但这是一件非常愚蠢的事情。我在 parseCarModels() 中做了一些改动首先,我将返回类型更改为 LinkedHashMap<Integer,String> .现在解析 html 似乎更快了。然后我改变了 value从 String 到 CharSequence 的变量。这让我可以使用 Html.fromHtml(e.html) ,所以最终代码如下所示:

public LinkedHashMap<Integer,String> parseCarModels(){
Elements models = document.select("option");
int key;
CharSequence value;
for(Element e: models){
key = Integer.parseInt(e.attr("value"));
value = Html.fromHtml(e.html());
hashMap.put(key,value.toString());
}
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return hashMap;
}

谢谢大家的帮助。我真的很感激。我希望这段代码不是非常无效

关于android - 奇怪的ListView排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25646767/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com