2017-01-02 49 views
-2

我寫代碼:.replace(列表(x), 「」)不工作的Java

List<String> Names = new ArrayList<String>(); 
List<Double> Prices = new ArrayList<Double>(); 
List<String> Dates = new ArrayList<String>(); 
List<String> Hours = new ArrayList<String>(); 
List<String> iDs = new ArrayList<String>(); 
SimpleAdapter adapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    ListView resultsListView = (ListView) findViewById(R.id.listMyReceipts); 
    SearchView m = (SearchView) findViewById(R.id.action_search); 

    HashMap<String, String> NamePrice = new HashMap<>(); 

    TextView test = (TextView) findViewById(R.id.test); 

    Names.add("Starbucks"); Prices.add(11.99); Dates.add("01/01/2017"); Hours.add("9:33"); 
    Names.add("Apple Store"); Prices.add(500.00); Dates.add("01/01/2017"); Hours.add("9:30"); 
    Names.add("Esselunga"); Prices.add(135.67); Dates.add("01/01/2017"); Hours.add("11:51"); 
    Names.add("Mediaworld"); Prices.add(19.98); Dates.add("01/01/2017"); Hours.add("12:03"); 
    Names.add("Starbucks"); Prices.add(11.99); Dates.add("01/01/2017"); Hours.add("12:47"); 

    for (int i = 0; i < Names.size(); i++) { 
     iDs.add(";[email protected]:" + i + ":@+;"); 
     NamePrice.put(iDs.get(i) + Names.get(i), " " + Prices.get(i).toString() + "€" + " - " + Dates.get(i) + " " + Hours.get(i)); 
    } 



    List<HashMap<String, String>> listItems = new ArrayList<>(); 
    adapter = new SimpleAdapter(this, listItems, R.layout.list_item, 
      new String[] {"First", "Second"}, 
      new int[] {R.id.listitemTitle, R.id.listitemSubItem}); 

    //Integer x = 0; 
    //int x = -1; 
    Iterator it = NamePrice.entrySet().iterator(); 
    for (int i = 0; i < iDs.size(); i++) { 
     HashMap<String, String> resultMap = new HashMap<>(); 
     Map.Entry pair = (Map.Entry) it.next(); 
     resultMap.put("First", pair.getKey().toString().replace(iDs.get(i), "")); 
     resultMap.put("Second", pair.getValue().toString()); 
     listItems.add(resultMap); 
    } 
    //test.setText(IDs.get(x - 1)); 

    resultsListView.setAdapter(adapter); 

} 

這應全部更換ID添加到 「」。它沒有。所以輸出可以是:+ @ 0:@ +; NAME1 12.99 + @:1:@ +; NAME2 0.99 但在第二循環中,如果我說

resultMap.put("First", pair.getKey().toString().replace(IDs.get(0), "")); 

它正確替換元素0. 輸出將是:NAME1 12.99; + @:1:@ +; NAME2 0.99

+0

因爲'replace'給你新的字符串對象,舊的沒有修改 –

+0

那麼,我該怎麼辦? @PavneetSingh – Johnson

+0

等待,你的意思是,內循環,使用索引,你的替換不工作,好像我以其他方式解釋你的問題 –

回答

0

您沒有按預期的順序得到NamePrice的條目。我在你的第二個for環之間插入一個System.out.println()並得到順序輸出:

;[email protected]:3:@+;Mediaworld 
;[email protected]:0:@+;Starbucks 
;[email protected]:4:@+;Starbucks 
;[email protected]:1:@+;Apple Store 
;[email protected]:2:@+;Esselunga 

所以你試圖取代;[email protected]:0:@+;;[email protected]:3:@+;Mediaworld等等。這並不能代替任何東西。原因是NamePriceHashMap。 A HashMap的迭代器不會以任何特定順序返回條目,特別是不按照它們插入的順序。

您可以使用LinkedHashMap代替:

LinkedHashMap<String, String> NamePrice = new LinkedHashMap<>(); 

迭代器中創建它們的順序返回條目。所以現在替換工作。

+0

只有一個詞:真棒 – Johnson