我有CheckedTextView的列表视图。我想在选择项目时将其值添加到列表中,并在取消选择时从列表中删除值。我不知道为什么我无法获得准确的价值。在此先感谢........
这是我的代码
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent,
View view, int position, long id) {
CheckedTextView ctv=(CheckedTextView)view.findViewById(R.id.addonsItem);
if (ctv.isChecked()) {
Temp.addonsIdList.add(addonsiDList.get(position));
Temp.addonsNameList.add(addonsList.get(position));
Temp.addonsPriceList.add(addonsPriceList.get(position));
ctv.setChecked(false);
}else{
Temp.addonsIdList.remove(addonsiDList.get(position));
Temp.addonsNameList.remove(addonsList.get(position));
Temp.addonsPriceList.remove(addonsPriceList.get(position));
ctv.setChecked(true);
}
适配器getView代码
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.addons_text, parent, false);
CheckedTextView checkTV=(CheckedTextView)convertView.findViewById(R.id.addonsItem);
checkTV.setText(Temp.addonsName.get(position));
}
return convertView;
}
答案 0 :(得分:0)
试试这个
您需要首先实现其他部分,如下所示
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.addons_text, parent, false);
holder.checkTV=(CheckedTextView)convertView.findViewById(R.id.addonsItem);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.checkTV.setText(Temp.addonsName.get(position));
然后创建类
class ViewHolder {
CheckedTextView checkTV;
}
修改1:
对于滚动问题,您需要按照步骤操作。
Step 1.
在ArrayList中添加您选择的行值
Step 2.
添加了一个条件
if(yourArrayList.contains(selected_value)){
holder.checkTV.setChecked(true);
}else{
holder.checkTV.setChecked(false);
}