【发布时间】:2015-04-23 07:55:01
【问题描述】:
我有一个列表,里面有很多联系人。我使用异步类加载列表中的联系人。
在列表类中,我实现了一个 setOnItemClickListener。在这个 setOnItemClickListener 中,我将选定的联系人放在一个数组中,这样我就可以向他们发送文本。这一切都像梦一样工作。
但是有一个小问题。在列表中选择一个联系人时,会选择多个联系人,如下所示:
-normal contact-
-selected contact-
-normal contact-
-normal contact-
-normal contact-
-normal contact-
-selected contact-
我使用此代码加载联系人:
@Override
protected void onPostExecute(ArrayList<list_item_person> contacts) {
// TODO Auto-generated method stub
super.onPostExecute(contacts);
pd.cancel();
PersonListAdapter adapter = new PersonListAdapter(getActivity().getApplicationContext(),
R.layout.list_item_person, contacts);
list.setAdapter(adapter);
}
这是异步类
onclick 是这样的:
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
list_item_person o = (list_item_person) list.getItemAtPosition(position);
SearchResults selected = new SearchResults("");
int i = 0;
for( SearchResults number:contact_selected){
if(o.phoneNumber.equals(number.getPhoneNumber())){
i ++;
selected = number;
}
};
if(i == 0) {
contact_selected.add(new SearchResults(String.valueOf(o.phoneNumber)));
arg1.setBackgroundColor(Color.RED);
Log.e("e", String.valueOf(o.phoneNumber));
}else{
contact_selected.remove(selected);
arg1.setBackgroundColor(Color.parseColor("#162229"));
}
我在这里忘记了什么?
【问题讨论】:
-
你甚至没有说出你认为什么是选定的项目。用户如何看到一个项目被选中?
-
When selecting a contact in the list there are multiple contacts selected like this:。不要这么想。只有在用户滚动列表之后。 -
@greenapps an selected item 是背景颜色为红色且数字的值放在contact_selected中的item
-
请对我的滚动声明做出反应。
-
@greenapps 在滚动之前和之后都是这种情况
标签: android asynchronous android-listview onitemclicklistener