2014-08-29 58 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

相關問題