我有一个从数据库更新的ArrayAdapter。更新数据时,将调用notifyDataSetChanged();
并更新数据列表。我的问题是onListItemClick没有更新。当用户点击第一项列表项时,每个1-9-18 ..都会被选中并且背景颜色会发生变化。
这应该是因为“ListView回收它的行,所以你需要确保你正在重置适配器的getView()方法中的视图状态。” Link
这是怎么做到的?
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
contacts.clear();
activity = getActivity();
final View rootView = inflater.inflate(R.layout.activity_tab_contacts, container, false);
context=inflater.getContext();
ListView listView=(ListView) rootView.findViewById(R.id.list);
RequestParams params = new RequestParams();
params.put("type", "get_users");
clientGetUsers.post(address, params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
try {
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++) {
phoneNumberArray.add(jsonArray.getJSONObject(i).getString("mobile"));
}
} catch (JSONException e) {
e.printStackTrace();
}
matchPhoneNumbersWithAppUsers();
arrayAdapter.notifyDataSetChanged();
Log.w("response", response);
}
});
arrayAdapter = new InteractiveArrayAdapter(activity, R.layout.rowbuttonlayout,contacts);
setListAdapter(arrayAdapter);
return rootView;
}
onListItemClick
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Log.w("click2", "virker " + position);
CheckBox checkbox = (CheckBox) v.findViewById(R.id.checkbox_users);
if (checkbox.isChecked() == false) {
checkbox.setChecked(true);
} else {
checkbox.setChecked(false);
v.setBackgroundColor(Color.TRANSPARENT);
}
}