我尝试使用复选框创建列表视图。所以每当我点击一个复选框并向下或向上滚动它时,这就是问题,它会自动检查列表中我没有点击的其他复选框。我已经在这里搜索并阅读了同样的问题,但没有一个解决了我的问题。
这是我到目前为止所做的。
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_songs, null, true);
viewHolder = new ViewHolder();
viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
viewHolder.tvSongName = (TextView) convertView.findViewById(R.id.tvSongName);
viewHolder.tvSongArtist = (TextView) convertView.findViewById(R.id.tvArtistName);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.tvSongName.setText(_songs.get(position).getSongname());
viewHolder.tvSongArtist.setText(_songs.get(position).getArtistname());
viewHolder.checkBox.setTag(R.integer.btnplusview, convertView);
viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
_songs.get(getPosition).setSelected(buttonView.isChecked());
if (_songs.get(getPosition).isSelected()) {
_songs.get(getPosition).setSelected(false);
Toast.makeText(getApplicationContext(), "position " + position + " " + _songs.get(position).getSongUrl(),
Toast.LENGTH_LONG).show();
Log.e("URL", _songs.get(position).getSongUrl() + " ");
} else {
_songs.get(getPosition).setSelected(true);
}
}
});
convertView.setTag(R.id.tvSongName, viewHolder.tvSongName);
convertView.setTag(R.id.tvArtistName, viewHolder.tvSongArtist);
convertView.setTag(R.id.checkBox, viewHolder.checkBox);
viewHolder.checkBox.setTag(position);
viewHolder.checkBox.setChecked(_songs.get(position).isSelected());
return convertView;
}
我该怎么做才能解决这个问题?
提前致谢!
答案 0 :(得分:2)
每当您滚动setOnCheckedChangeListener
时都会调用
删除setOnCheckedChangeListener
并实施setOnClickListener
viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (viewHolder.checkbox.isChecked()) {
your logic id true
} else {
your logic id false
}
}
});
答案 1 :(得分:0)
滚动时,会调用setOnCheckedChangeListener。因此,要在获取视图中处理它,您需要执行以下操作 -
viewHolder.checkBox.setOnCheckedChangeListener(null);
viewHolder.checkBox.setChecked(_songs.get(position).isSelected());
首先需要设置onchecked null,以便在填充视图时滚动并且您为复选框视图指定值时,不要调用onCheckedListener。
然后你需要调用OnCheckedChangeListener
viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
//remove this line from the code. The below one
_songs.get(getPosition).setSelected(buttonView.isChecked());
if (_songs.get(getPosition).isSelected()) {
_songs.get(getPosition).setSelected(false);
Toast.makeText(getApplicationContext(), "position " + position + " " + _songs.get(position).getSongUrl(),
Toast.LENGTH_LONG).show();
Log.e("URL", _songs.get(position).getSongUrl() + " ");
} else {
_songs.get(getPosition).setSelected(true);
}
}
});