简体   繁体   English

listview项目重复自身,打开listview的次数

[英]listview item repeating itself, number of time listview is opened

The items in the listview are repeating itself the number of times Pizza_Delivery is called, suppose i have 10 item in my listview, initially when i open Pizza_Delivery for the first time it shows 10 items but the next time when i open Pizza_Delivery the number of item in listview gets 20 (Original 10) repeating itself two times, third time when i open Pizza_Delivery it gets 30 (Original 10) repeating itself three times... and so on 列表视图中的项目正在重复自身调用Pizza_Delivery的次数,假设我的列表视图中有10个项目,最初当我第一次打开Pizza_Delivery时它第一次显示10个项目,但是下次我打开Pizza_Delivery时,项目数量在listview中,得到20(原始10)重复两次,第三次,当我打开Pizza_Delivery时,得到30(原始10)重复三遍...等等

please check the below code 请检查以下代码

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

   }

 }

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

Change 更改

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

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

And Instantiate your list in your onListItemClick 并在 onListItemClick 实例化您的列表

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM