删除项目选中后复选框自动检查下一项。 我试图覆盖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() {
答案 0 :(得分:2)
试试这个:
只需在你的onCheckedChanged()事件之前添加这一行。
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();
}
}
});
希望它能帮助!!
答案 1 :(得分:0)
您必须在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();
}
}
答案 2 :(得分:-1)
请尝试在xml文件中使用android:checked="false"
到CheckBox