0

删除选中的项目复选框后自动检查下一个项目。我尝试覆盖 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() {
4

3 回答 3

2

尝试这个:

只需在您的 onCheckChanged() 事件之前添加这一行,就像这样。

viewHolder.nameEn.setText(countries.get(position).getNameEn());
    viewHolder.nameVi.setText(countries.get(position).getNameVi());
    viewHolder.flag.setImageDrawable(countries.get(position).getFlag());
    viewHolder.check.setChecked(false);
    viewHolder.check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
            count = 0;
            for (MyCountry country : countries) {
                if(country.isCheck())
                    count++;
            }
            if(isChecked)
            {
                if(activity.actionMode == null || count == 0)//chua co
                activity.actionMode = activity.startActionMode(activity.callback);
                count++;
                country.setCheck(true);
            }
            else
            {
                country.setCheck(false);
                count--;
                if(count == 0)
                    activity.actionMode.finish();
            }
        }
    });

希望能帮助到你!!

于 2013-08-19T11:55:12.897 回答
1

你必须在 notifyDataSetChanged(); 之前设置你的列表视图。

public void dellistview() { listviewdata.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

    SparseBooleanArray selected = listAdapterData.getCheckedItemPositions();

    if (selected != null) {
        try {

            for (int i = (selected.size() - 1); i >= 0; i--)
            {
                if (selected.valueAt(i)) {

                    String str[] = arrList.get(selected.keyAt(i));
                    fmdbAccess.removelistitm(sourceTable, str[1]);
                    if (arrList != null)
                        arrList.remove(str);

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        selected.clear();
        arrList = fmdbAccess.getGenricTable(sourceTable, colName);
        if (arrList != null && arrList.size() > 0)
        setListAdapter();

        listAdapterData.notifyDataSetChanged();
      //  finish();
    }

}
于 2015-10-26T11:11:56.063 回答
-1

请尝试android:checked="false"CheckBox您的 xml 文件中使用

于 2013-08-19T07:39:58.947 回答