2014-08-29 60 views
0

我有一个简单的适配器,并为其设置了两个值名称和email.After使用适配器在列表中显示名称和email.My问题出现在我向下滚动列表时,如果我检查第一个值,并向下滚动我检查的值改变他的possition.I认为问题来自适配器,但我不知道如何解决它。ListView在向下滚动或向上时更改他的元素的位置android

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

     } 

回答

0

ListView你不应该设置项目的属性在onClick方法,因为ListView回收体系。相反,您必须将其设置为getView方法Adapter。 所以你必须创建一个自定义适配器。
检查答案:https://stackoverflow.com/a/25403471/3922482

相关问题