2013-07-05 72 views
0

listview中的項目重複自己調用Pizza_Delivery的次數,假設我在我的listview中有10個項目,最初當我第一次打開Pizza_Delivery時顯示10個項目,但下次我打開Pizza_Delivery時,listview中的項目數量得到20(原來的10)重複自己兩次,第三次當我打開Pizza_Delivery它得到30(原來10)重複自己三次...等等重複自己的listview項目,打開時間listview的次數

請檢查下面的代碼

public class Pizza_Delivery extends ListActivity { 

static final ArrayList<HashMap<String,String>> list = 
    new ArrayList<HashMap<String,String>>(); 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.listview); 
    SimpleAdapter adapter = new SimpleAdapter(
     this, 
     list, 
     R.layout.rowview, 
     new String[] {"title","address","phone"}, 
     new int[] {R.id.text1,R.id.text2, R.id.text3} 
    );  
    populateList(); 
    setListAdapter(adapter); 

} 

protected void onListItemClick (ListView l, View v, int position, long id){ 
    super.onListItemClick(l,v,position,id); 
    if(position>=0 && position<list.size()) { 
     HashMap<String, String> tmp = list.get(position); 
     if(tmp.containsKey("phone")) { 
      String tel = tmp.get("phone"); 
      Intent callIntent = new Intent(Intent.ACTION_CALL); 
      callIntent.setData(Uri.parse("tel:"+tel)); 
      startActivity(callIntent); 
     } 
    } 
} 

private void populateList() { 
    HashMap<String,String> 
    map = new HashMap<String,String>(); 
    map.put("title","Dominos Pizza"); 
    map.put("address", "G-5, Ground Floor, The Mark, 20/ A, Saket Nagar, Navneet Tower Road, Old Palasia"); 
    map.put("phone", "0731 4016464"); 
    list.add(map); 
    map = new HashMap<String,String>(); 
    map.put("title","Dominos Pizza"); 
    map.put("address", "Shop No S-1, Block A, Ground Floor, Sita Shree Residency, Revenue Survey No 179/1/2, Sirpur, Airport Road"); 
    map.put("phone", "0731 2611260"); 
    list.add(map); 
    map = new HashMap<String,String>(); 
    map.put("title","Dominos Pizza"); 
    map.put("address", "C/O Sapna Sangeeta Mall, Ground Floor, Hall No 1 & 3, Scheme No 47 , Sneh Nagar, Sapna Sangeeta Road"); 
    map.put("phone", "6888688"); 
    list.add(map); 
    map = new HashMap<String,String>(); 
    map.put("title","Dominos Pizza"); 
    map.put("address", "Ground Floor 32, Prime House, Scheme No 54, Opposite Sayaji Hotel , Block A, Meghdoot Park Road, Vijay Nagar"); 
    map.put("phone", "0731 4234555"); 
    list.add(map); 
    map = new HashMap<String,String>(); 
    map.put("title","Dominos Pizza"); 
    map.put("address", "Shop No 27, Ground Floor, C 21 Mall, Near Orbit Mall, Opposite Pakeeza Showroom, Vijay Nagar, A B Road"); 
    map.put("phone", "6888688"); 
    list.add(map); 
    map = new HashMap<String,String>(); 
    map.put("title","Dominos Pizza"); 
    map.put("address", "4th Floor, Food Court, Treasure Island Mall, Tukoganj Main Road, South Tukoganj"); 
    map.put("phone", "0731 4286222"); 
    list.add(map); 
    map = new HashMap<String,String>(); 
    map.put("title","Dominos Pizza"); 
    map.put("address", "Ground Floor, Patani Tower, Survey No- 521 & 527 , Plot No- 1000, Footi Kothi Main Square, Sudama Nagar Main Road"); 
    map.put("phone", "0731 4757700"); 
    list.add(map); 
    map = new HashMap<String,String>(); 
    map.put("title","Pizza Hut"); 
    map.put("address", "Plot No 11, Treasure Island Mall, M G Road, South Tukoganj"); 
    map.put("phone", "0731 39883988"); 
    list.add(map); 
    map = new HashMap<String,String>(); 
    map.put("title","Pizza World"); 
    map.put("address", "LG-8, Arcade Silver 56, Near To Titan Showroom, New Palasia"); 
    map.put("phone", "0731 2430888"); 
    list.add(map); 

    } 

} 
+0

不要製作靜態數組列表。它的參考可用於記憶中的課程。 –

+0

刪除靜態最終修飾符,它應該工作 – Raghunandan

回答

0

刪除列表中的靜態屬性,它應該工作。

0

變化

static final ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();

private ArrayList<HashMap<String,String>> list;

而在你onListItemClick

list = new ArrayList<HashMap<String,String>>();

實例化列表0