2015-06-17 93 views
0

事情是,我有一個ExpandableListView有2個組,每個組內部有一些用戶,當我點擊這些用戶時,我得到他們的ID並添加到字符串的HashMap中,現在我試圖刪除這些ID當我第二次點擊它們時,第二組的ID不會被刪除。這裏是我想要做的事:如何從哈希表中刪除值?

aMap = new HashMap < String, GPSEscolas >(); 
TextView idAluno = (TextView) v.findViewById(R.id.idcrianca); 
TextView idEscola = (TextView) v.findViewById(R.id.idescola); 

IdEscola = String.valueOf(idEscola.getText()); 
IdAluno = String.valueOf(idAluno.getText()); 


mGpsEscolas = aMap.get(IdEscola); 
if (mGpsEscolas == null) { 
    mGpsEscolas = new GPSEscolas(); 
    aMap.put(IdEscola, mGpsEscolas); 
    mGpsEscolas.getIds_alunos().add(String.valueOf(IdAluno)); 
    mGpsEscolas.setAlunos(IdAluno); 

} else { 


    Set <String> ia = mGpsEscolas.getIds_alunos(); 


    if (!ia.contains(IdAluno)) { 
     ia.add(String.valueOf(IdAluno)); 

    } else { 
     alunos = aMap.get(IdEscola).getAlunos(); 
     ia.remove(IdAluno); 

     aMap.remove(alunos); 

    } 

    stringArray = new String[ia.size()]; 
    out = ""; 
    if (ia.size() > 0) { 

     for (String str: ia.toArray(stringArray)) { 
      out += ";" + str; 
     } 

     aMap.get(IdEscola).setAlunos(out.substring(1)); 
    } 
} 

型號:

public class GPSEscolas implements Serializable{ 

    private static final long serialVersionUID = 1L; 


    private Integer id_escola; 
    private Set<String> ids_alunos = new TreeSet<String>(); 
    private String Alunos; 
    private double distancia; 
    private Float latitude; 
    private Float longitude; 


    public String getAlunos() { 
     return Alunos; 
    } 

    public void setAlunos(String alunos) { 
     Alunos = alunos; 
    } 

    public Integer getId_escola() { 
     return id_escola; 
    } 

    public void setId_escola(Integer id_escola) { 
     this.id_escola = id_escola; 
    } 

    public Set<String> getIds_alunos() { 
     return ids_alunos; 
    } 

    public void setIds_alunos(Set<String> ids_alunos) { 
     this.ids_alunos = ids_alunos; 
    } 

    public double getDistancia() { 
     return distancia; 
    } 

    public void setDistancia(double distancia) { 
     this.distancia = distancia; 
    } 

    public Float getLatitude() { 
     return latitude; 
    } 

    public void setLatitude(Float latitude) { 
     this.latitude = latitude; 
    } 

    public Float getLongitude() { 
     return longitude; 
    } 

    public void setLongitude(Float longitude) { 
     this.longitude = longitude; 
    } 


} 

回答

1

我想你指的是這一行:

aMap.remove(alunos); 

...這是唯一的地方你在哪裏試圖從HashMap中刪除。

如果是這種情況,問題是您沒有將正確的參數值傳遞給。該方法預計您將通過key值,例如IdEscola,這是您在HashMap中用於執行put的關鍵值。

但是相反,您傳遞的是alunos,我不太確定那是什麼,但它看起來像一個學生列表。

所以我認爲你想要做的只是做aMap.remove(IdEscola)

編輯:我現在意識到你的hashmap是由學校,所以我改變了我的答案。

編輯2:如果上述不是你想要的,那麼你可能不想從該HashMap中刪除任何東西。

+0

是的,你認爲沒事,但如何通過alunos我循環? – AND4011002849

+0

@WARpoluido - alunos是GPEscolas對象,你這裏沒有包括,所以我們怎麼可能能夠幫助? – pnadczuk

+0

@WARpoluido:我調整了我的答案,現在我明白你的意圖了。只要做'aMap.remove(IdEscola)'。我很確定這就是你的意思。 – sstan

0

如果您關鍵對象是不同的,而(aMap.put(IdEscola,mGpsEscolas))和越來越(aMap.get(IdEscola).getAlunos())是兩個不同的對象,然後HashMap的將返回即使它們在邏輯上相同的值也是null。

閱讀關於Java中的對象類,它是equals()和hashcode()函數,hashmap類使用它。

Student A= new Student ("ram"); 
Student B= new Student ("ram"); 
map.put(A,"10"); 

map.get(B) will return null. 

如果你使用一個對象作爲重點把一些元素的地圖,那麼你不能使用對象B進行檢索,即使它們在邏輯上是相同的。