【发布时间】:2015-10-04 12:08:59
【问题描述】:
我有一个列表适配器,每个项目都有一个复选框。我还可以选择从该列表中删除用户检查的所有项目。 应该删除所选项目的部分效果很好,但是.. 例如,如果我有
1. 0000
2. 5555
3. 9999
如果我检查 5555 和 9999,然后单击删除,它们会消失,但即使我没有按下它,也会检查 0000。
(historyList 是历史片段中的所有项目。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());
}
}
});
我错过了什么吗?
【问题讨论】:
-
请分享您的代码....
-
@BVtp 您需要创建一个列表,列表项将是位置处的复选框 listitemclick 侦听器为复选框进行操作
标签: android list checkbox view adapter