删除选中的项目复选框后自动检查下一个项目。我尝试覆盖 getcount 方法但没有结果
CountryAdapter.java
CountryAdapter extends ArrayAdapter<MyCountry>{
Context context; int layoutResourceId; ArrayList<MyCountry> countries; ContextualActionMode activity;
public CountryAdapter(Context context, int layoutResourceId,
ArrayList<MyCountry> countries) {
}
@Override
public int getCount() {
return countries.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final MyCountry country = countries.get(position);
ViewHolder viewHolder = null;
if(convertView == null)
{
viewHolder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(layoutResourceId, null);
viewHolder.nameEn = (TextView) convertView.findViewById(R.id.tvNameEn);
viewHolder.nameVi = (TextView) convertView.findViewById(R.id.tvNameVi);
viewHolder.flag = (ImageView) convertView.findViewById(R.id.ivFlag);
viewHolder.check = (CheckBox) convertView.findViewById(R.id.checkBox1);
convertView.setTag(viewHolder);
}
else
viewHolder = (ViewHolder) convertView.getTag();
viewHolder.nameEn.setText(countries.get(position).getNameEn());
viewHolder.nameVi.setText(countries.get(position).getNameVi());
viewHolder.flag.setImageDrawable(countries.get(position).getFlag());
viewHolder.check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
count = 0;
for (MyCountry country : countries) {
if(country.isCheck()) count++;
}
if(isChecked)
{
if(activity.actionMode == null || count == 0)
activity.actionMode = activity.startActionMode(activity.callback);
count++;
country.setCheck(true);
}
else
{
country.setCheck(false);
count--;
if(count == 0) activity.actionMode.finish();
}
}
});
return convertView;
}
int count = 0;
public class ViewHolder{
TextView nameEn;
TextView nameVi;
ImageView flag;
CheckBox check;
}
ContextualActionMode.java
public class ContextualActionMode extends Activity {
ArrayList<MyCountry> countries = new ArrayList<MyCountry>();
ListView listView;
CountryAdapter adapter;
ActionMode.Callback callback = new ActionMode.Callback() {
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.mnDelete:
for (int i = 0; i < countries.size(); i++)
{
if (countries.get(i).isCheck()) {
countries.remove(countries.get(i));
countries.get(i).setChecked(false)
}
}
adapter.notifyDataSetChanged();
mode.finish();
return true;
default:
break;
}
return false;
}
};
也许这里有错误,我找到了一些解决方案,但是我尝试更改循环没有任何效果,因为列表从 0 开始索引
// 我可以修复它,非常感谢 Armaan Stranger
链接到我的来源,谁和我有同样的问题 mediafire.com/?agnvic06c69cvw0
并在 CountryAdapter.java 中编辑
viewHolder.flag.setImageDrawable(countries.get(position).getFlag());
viewHolder.check.setChecked(false); --> right here, i forgot to add set check false as default.
viewHolder.check.setOnCheckedChangeListener(new OnCheckedChangeListener() {