向下滚动或向上滚动时,ListView更改其元素的位置

时间:2014-08-29 06:50:29

标签: listview android-adapter simpleadapter

我有简单的适配器,并为他设置两个值名称和电子邮件。之后使用列表中的适配器显示名称和电子邮件。当我向下滚动列表时,我的问题来了,如果我检查第一个值并向下滚动值我检查了改变他的位置。我认为问题来自适配器,但我不知道如何解决它。

// /simple adapter, with his help we can set email and name
        // to list view and visual them
        final SimpleAdapter adapter = new SimpleAdapter(this, list,
                R.layout.custom_row_view, new String[] { "name", "email" },
                new int[] { R.id.listItem, R.id.listSubItem });



Button buttonPickContact = (Button) findViewById(R.id.contactact_us_btn);
    buttonPickContact.setOnClickListener(new Button.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // clear list every time when pick contact button is clicked
            list.clear();
            populateList();
            // set email and name to listview with help of adapter
            listview.setAdapter(adapter);
            // set multiple choise to listview
            listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

            listview.setOnItemClickListener(new OnItemClickListener() {
                @SuppressLint("ResourceAsColor")
                @Override
                public void onItemClick(AdapterView<?> p_arg0, View p_arg1,
                        int p_arg2, long p_arg3) {
                    // if some of the items in list view is clicked
                    // set checked true
                    CheckedTextView checkText = (CheckedTextView) p_arg1
                            .findViewById(R.id.listItem);
                    // if clicked item from list view is not checked
                    // set checked true to item
                    checkText.setChecked(!checkText.isChecked());

                }

            });

        }
    });



// populate(add email and names) from contact list in phone to listview
    private void populateList() {

        ContactsProvider cpro = new ContactsProvider(getApplicationContext());
        List<Contact> contacts = cpro.getContacts();
        // with this loop get emails and names
        // put them in map and after that
        // put map in listview
        for (Contact cnt : contacts) {
            // add all contacts in map
            HashMap<String, String> map = new HashMap<String, String>();
            // then put email and name of contacts in map
            map.put("name", cnt.name);
            map.put("email", cnt.email);

            list.add(map);

        }

1 个答案:

答案 0 :(得分:0)

ListView中,由于onClick的回收系统,您不应在ListView方法中设置项目的属性。相反,您必须使用getView Adapter方法进行设置。 所以你必须创建一个自定义适配器 请检查此答案:https://stackoverflow.com/a/25403471/3922482

相关问题