2014-09-03 91 views
1

我有以下问题:奇怪的ListView排序

Wrong sorted ListView

正如你可以在图片上看到的,CITROEN组头是不是上面的所有雪铁龙车型一样,例如DACIA 。 奇怪的是,有大约20个汽车品牌,如宝马,奥迪......每个组头都高于其子项,但不是雪铁龙。

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

我使用自定义适配器:

此列表视图从HTML文件,它具有以下结构填充。下面是比较方法的源代码:

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

EDIT 按照要求,这里是HTMLParser类s源代码。它的构造是由CarsList活动,它扩展了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");输出是它应该是一个品牌的时候。 你有什么想法,问题在哪里?如果有必要,我会发布代码的其他部分。 我想感谢您为我的问题所付出的所有时间和精力

+0

你的代码与提供的html源码没有关系,所以我们如何知道?但是拿出sort()进行一个简短的测试。而且我们没有看到图标,为什么使用图标发布代码? – greenapps 2014-09-03 15:16:33

+0

抱歉给您带来不便。我用HTMLParser类更新了我的问题。还没有图标,因为我正在处理它们。之所以缺少图标,是因为我暂时评论了setImageResource()方法 – user2151486 2014-09-03 15:37:46

+0

那么你是不是在没有sort()的情况下运行并且在Citroën中使用了? – greenapps 2014-09-03 15:39:24

回答

1

我已经做到了,但这是一件很愚蠢的事情。我改变了一些东西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; 
} 

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