我有以下問題:奇怪的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ËN C4 1.6i 16V EP turbo
只是解析使用String s = e.attr("label");
輸出是它應該是一個品牌的時候。 你有什麼想法,問題在哪裏?如果有必要,我會發布代碼的其他部分。 我想感謝您爲我的問題所付出的所有時間和精力
你的代碼與提供的html源碼沒有關係,所以我們如何知道?但是拿出sort()進行一個簡短的測試。而且我們沒有看到圖標,爲什麼使用圖標發佈代碼? – greenapps 2014-09-03 15:16:33
抱歉給您帶來不便。我用HTMLParser類更新了我的問題。還沒有圖標,因爲我正在處理它們。之所以缺少圖標,是因爲我暫時評論了setImageResource()方法 – user2151486 2014-09-03 15:37:46
那麼你是不是在沒有sort()的情況下運行並且在Citroën中使用了? – greenapps 2014-09-03 15:39:24