listview项重复自身,打开时间列表视图的数量

时间:2013-07-05 04:59:57

标签: android android-listview

listview中的项目重复调用Pizza_Delivery的次数,假设我的listview中有10个项目,最初当我第一次打开Pizza_Delivery时它显示10个项目,但下次我打开Pizza_Delivery时listview中的项目数量为20(原始10)重复自己两次,第三次当我打开Pizza_Delivery时它获得30(原始10)重复自己三次......等等

请检查以下代码

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

   }

 }

2 个答案:

答案 0 :(得分:0)

删除列表的静态属性,它应该可以工作。

答案 1 :(得分: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>>();