我确实有一个从数据库更新的 ArrayAdapter。当数据更新notifyDataSetChanged();
时被调用并更新数据列表。我的问题是 onListItemClick 没有更新。当用户单击第一个列表项时,会检查每个 1-9-18.. 并更改背景颜色。
应该这样做是因为“ListView 会回收其行,因此您需要确保在适配器的 getView() 方法中重置视图的状态。” 关联
这是怎么做到的?
@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);
}
}