gpt4 book ai didi

android - 列表适配器中的复选框行为异常

转载 作者:行者123 更新时间:2023-11-30 02:01:16 25 4
gpt4 key购买 nike

我有一个列表适配器,每个项目都有一个复选框。我还可以选择从该列表中删除用户检查的所有项目。应该删除所选项目的部分运行良好,但是..例如,如果我有

1. 0000
2. 5555
3. 9999

如果我选中 5555 和 9999,然后单击删除,它们就会消失,但是即使我没有按下它,0000 也会被选中。

(historyList是历史 fragment 中的所有项目。listToRemove只是选中的项目)

 @Override
public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState )
{
View view = inflater.inflate( R.layout.fragment_history, container, false );
adapter = new PasswordAdapter( historyList );
setListAdapter(adapter);
ImageView removeButton = ( ImageView ) view.findViewById( R.id.removeButton );
removeButton.setOnClickListener( new OnClickListener(){
@Override
public void onClick( View v )
{
if ( !listToRemove.isEmpty() )
{
listToRemove.clear();

adapter.updateList( historyList );
}
}
});

return view;
}
...
...
...
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final View result;

if (convertView == null)
{
result = LayoutInflater.from(parent.getContext()).inflate(R.layout.history_list_item, parent, false);
}
else
{
result = convertView;
}

final Password item = getItem( position );

( (TextView) result.findViewById( R.id.historyPasswordTextView) ).setText(item.getPasswordString());
( (TextView) result.findViewById( R.id.historyDateTextView ) ).setText(item.getPasswordString());
CheckBox checkBoxToRemove = (CheckBox) result.findViewById( R.id.removePasswordFromHistoryCheckBox ) ;
checkBoxToRemove.setOnCheckedChangeListener( new OnCheckedChangeListener(){
@Override
public void onCheckedChanged( CompoundButton buttonView, boolean isChecked )
{
if( isChecked )
{
listToRemove.add( item );
// Log.d( "TAG", listToRemove.toString() );

for( int i=0; i<listToRemove.size(); i++)
Log.d("TAG","checked after add: "+ listToRemove.get(i).getPasswordString());
}
else
{
listToRemove.remove( item );
for( int i=0; i<listToRemove.size(); i++)
Log.d("TAG","checked after remove: "+ listToRemove.get(i).getPasswordString());
}

}
});

我错过了什么吗?

最佳答案

像这样尝试也许有用

     public View getView(int position, View convertView, ViewGroup parent) {
final View result;
ViewHolder holder;

if (convertView == null)
{
holder = new ViewHolder();
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.history_list_item, parent, false);

holder. checkBoxToRemove = (CheckBox) convertView.findViewById( R.id.removePasswordFromHistoryCheckBox ) ;
convertView.setTag(holder);
}
else
{

holder = (ViewHolder) convertView.getTag();
}

final Password item = getItem( position );


holder .checkBoxToRemove.setOnCheckedChangeListener( new OnCheckedChangeListener(){
@Override
public void onCheckedChanged( CompoundButton buttonView, boolean isChecked )
{
if( isChecked )
{
listToRemove.add( item );
// Log.d( "TAG", listToRemove.toString() );

for( int i=0; i<listToRemove.size(); i++)
Log.d("TAG","checked after add: "+ listToRemove.get(i).getPasswordString());
}
else
{
listToRemove.remove( item );
for( int i=0; i<listToRemove.size(); i++)
Log.d("TAG","checked after remove: "+ listToRemove.get(i).getPasswordString());
}

}
});
return convertView;
}
class ViewHolder {
CheckBox checkBoxToRemove;
}

然后在您的 getView 中添加:

if(listToRemove.contains(item){
holder.checkBoxToRemove.setChecked(true);
}else{
holder.checkBoxToRemove.setChecked(false);
}

关于android - 列表适配器中的复选框行为异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31430614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com