【发布时间】: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