【问题标题】:ListView change position of his element when scroll down or up androidListView 在向下或向上滚动时更改其元素的位置 android
【发布时间】:2014-08-29 06:50:29
【问题描述】:

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

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

        }

【问题讨论】:

    标签: listview android-adapter simpleadapter


    【解决方案1】:

    ListView 中,您不应该在onClick 方法中设置项目的属性,因为ListView 的回收系统。相反,您必须在AdaptergetView 方法中设置它。 因此,您必须创建一个自定义适配器。
    检查这个答案:https://stackoverflow.com/a/25403471/3922482

    【讨论】:

      猜你喜欢
      • 2019-06-27
      • 2016-02-01
      • 2017-11-24
      • 2014-12-28
      • 2019-05-17
      • 2014-09-16
      • 2010-12-27
      • 2014-10-06
      • 2017-12-22
      相关资源
      最近更新 更多