0

列表视图中的项目重复调用 Pizza_Delivery 的次数,假设我的列表视图中有 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);

   }

 }
4

2 回答 2

0

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

于 2013-07-05T05:02:28.903 回答
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>>();

于 2013-07-05T05:19:05.200 回答