当我选中listview
中的复选框并向下滚动时,他自己会检查另一个listrow。我怎么解决这个问题?请查看它并通知我应该更改代码的位置
PS:我尝试删除
viewHolder.checkBox = (CheckBox) workingView.findViewById(com.example.iit.quizzproject.R.i d.cbSelected);
但是我无法获得我想要的位置
public class InviteFriends extends FragmentActivity {
ListView listView;
AdapterUser adapterUser;
CheckBox cbSelected;
ArrayList<String> selections = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.example.iit.quizzproject.R.layout.invite_friend);
cbSelected = (CheckBox) findViewById(com.example.iit.quizzproject.R.id.cbSelected);
listView = (ListView) findViewById(com.example.iit.quizzproject.R.id.list);
adapterUser = new AdapterUser(this, com.example.iit.quizzproject.R.layout.list_item);
listView.setAdapter(adapterUser);
for (User entry :getNewsEntries ()) {
adapterUser.add(entry);
}
}
public void onCheckboxClicked(View view) {
boolean checked = ((CheckBox) view).isChecked();
int pos = listView.getPositionForView(view);
User user = adapterUser.getItem(pos);
switch (view.getId()) {
case com.example.iit.quizzproject.R.id.cbSelected:
if (checked) {
selections.add("user");
Snackbar.make(listView, user.getName(), Snackbar.LENGTH_SHORT).show();
} else {
selections.remove("user");
Snackbar.make(listView, user.getName(), Snackbar.LENGTH_LONG).show();
}
break;
}
}
private List<User> getNewsEntries() {
boolean b = false;
final List<User> entries = new ArrayList<User>();
for (int i = 1; i < 50; i++) {
entries.add(
new User(
"Amin Ben Mahfoudh ",
"24 ans ",
i % 2 == 0 ? com.example.iit.quizzproject.R.drawable.lurecas : com.example.iit.quizzproject.R.drawable.elisa,
b
)
);
}
return entries;
}
}
-
public class AdapterUser extends ArrayAdapter<User> {
private final int newsItemLayoutResource;
public AdapterUser(final Context context, final int newsItemLayoutResource) {
super(context, 0);
this.newsItemLayoutResource = newsItemLayoutResource;
}
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
final View view = getWorkingView(convertView);
final ViewHolder viewHolder = getViewHolder(view);
final User entry = getItem(position);
viewHolder.titleViewname.setText(entry.getName());
viewHolder.titleViewage.setText(entry.getAge());
viewHolder.imageView.setImageResource(entry.getPhotoId());
viewHolder.checkBox.setTag(entry);
return view;
}
private View getWorkingView(final View convertView) {
View workingView = null;
if (null == convertView) {
final Context context = getContext();
final LayoutInflater inflater = (LayoutInflater) context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
workingView = inflater.inflate(newsItemLayoutResource, null);
} else {
workingView = convertView;
}
return workingView;
}
private ViewHolder getViewHolder(final View workingView) {
final Object tag = workingView.getTag();
ViewHolder viewHolder = null;
if (null == tag || !(tag instanceof ViewHolder)) {
viewHolder = new ViewHolder();
viewHolder.titleViewname = (TextView) workingView.findViewById(com.example.iit.quizzproject.R.id.person_name);
viewHolder.titleViewage = (TextView) workingView.findViewById(com.example.iit.quizzproject.R.id.person_age);
viewHolder.imageView = (ImageView) workingView.findViewById(com.example.iit.quizzproject.R.id.person_photo);
viewHolder.checkBox = (CheckBox) workingView.findViewById(com.example.iit.quizzproject.R.id.cbSelected);
workingView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) tag;
}
return viewHolder;
}
private static class ViewHolder {
public TextView titleViewname;
public TextView titleViewage;
public ImageView imageView;
public CheckBox checkBox;
}
}
答案 0 :(得分:0)
您未在setChecked()
的{{1}}上致电checkBox
。因此,当行被回收时,您将getView()
置于其恰好处于的任何状态。正如您CheckBox
等反映此行应显示的内容,您需要更新{{ 1}}。