简体   繁体   English

添加新项目时重复执行ListView项目

[英]ListView item repeating when adding new item

Whenever i add a new item in my ListView shows only the last item repeatedly 每当我在ListView中添加新项目时,仅重复显示最后一个项目

 final ArrayList<Map<String, String>> persontList = new ArrayList <Map<String, String>>();

    ajout.setOnClickListener(new View.OnClickListener(){

         Map<String, String> data;

        public void onClick(View v) {
            String nom = etname.getText().toString();
            String number = etnumber.getText().toString();
            Person p =new Person(nom, number);
            String strNumber = String.valueOf(p.getPhoneNumber());
            data =  new HashMap<String, String>(2);
            data.put("FullName",p.getFullName());
            data.put("PhoneNumber",strNumber);

            persontList.add(data);
            SimpleAdapter arrayAdapter = new SimpleAdapter(MainActivity.this, persontList, android.R.layout.simple_list_item_2, new String[]{"FullName", "PhoneNumber"}, new int[]{android.R.id.text1, android.R.id.text2});
            lvStudent.setAdapter(arrayAdapter);

        }
    });

i saw this android ListView Simple Adapter, item repeating , but it dosen't seem to work for me 我看到了这个android ListView Simple Adapter,重复了项目 ,但似乎对我不起作用

I'm not sure if it's directly related to your problem, but you should not be doing: 我不确定这是否与您的问题直接相关,但您不应该这样做:

SimpleAdapter arrayAdapter = new SimpleAdapter(MainActivity.this, persontList, android.R.layout.simple_list_item_2, new String[]{"FullName", "PhoneNumber"}, new int[]{android.R.id.text1, android.R.id.text2});
lvStudent.setAdapter(arrayAdapter);

It's not mandatory, but probably you don't want to create a simple adapter every time you click your View. 这不是强制性的,但是您可能不想每次单击View时都创建一个简单的适配器。

Try creating your adapter out of this callback. 尝试通过此回调创建适配器。

I think it's because you create a SimpleAdapter every time you click on "ajout". 我认为这是因为您每次单击“ ajout”都会创建一个SimpleAdapter。

You have just to add the item to the list to the adapter that you had created before the click event. 您只需要将项目添加到单击事件之前创建的适配器的列表即可。

For example: 例如:

final ArrayList<Map<String, String>> persontList = new ArrayList <Map<String, String>>();
SimpleAdapter arrayAdapter = new SimpleAdapter(MainActivity.this, persontList, android.R.layout.simple_list_item_2, new String[]{"FullName", "PhoneNumber"}, new int[]{android.R.id.text1, android.R.id.text2});

    ajout.setOnClickListener(new View.OnClickListener(){

         Map<String, String> data;

        public void onClick(View v) {
            String nom = etname.getText().toString();
            String number = etnumber.getText().toString();
            Person p =new Person(nom, number);
            String strNumber = String.valueOf(p.getPhoneNumber());
            data =  new HashMap<String, String>(2);
            data.put("FullName",p.getFullName());
            data.put("PhoneNumber",strNumber);

            persontList.add(data);
            arrayAdapter = new SimpleAdapter(MainActivity.this, persontList, android.R.layout.simple_list_item_2, new String[]{nom, number}, new int[]{android.R.id.text1, android.R.id.text2});

            lvStudent.setAdapter(arrayAdapter);
            arrayAdapter.notifyDataSetChanged();
        }


        lvStudent.setAdapter(arrayAdapter);
    });

Add personlist.clear(); 添加personlist.clear(); before String nom = etname.getText().toString(); String nom = etname.getText().toString();

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

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