1

我制作了一个应用程序以两种语言显示一个国家/地区的名称,以及一个用于上下文操作模式的复选框。删除所选项目后,复选框自动选中下一个项目。

我尝试覆盖 getcount 但它不起作用。

我的代码:

我的国家.java

public class MyCountry {
private String nameEn;
private String nameVi;
private Drawable flag;
private boolean isCheck;
public String getNameEn() {
    return nameEn;
}
public void setNameEn(String nameEn) {
    this.nameEn = nameEn;
}
public String getNameVi() {
    return nameVi;
}
public void setNameVi(String nameVi) {
    this.nameVi = nameVi;
}
Drawable getFlag() {
    return flag;
}
void setFlag(Drawable flag) {
    this.flag = flag;
}
public MyCountry(String nameEn, String nameVi, Drawable flag) {
    // TODO Auto-generated constructor stub
    this.nameEn = nameEn;
    this.nameVi = nameVi;
    this.flag = flag;
}
public boolean isCheck() {
    return isCheck;
}
public void setCheck(boolean isCheck) {
    this.isCheck = isCheck;
}

}

CountryAdapter.java

public class CountryAdapter extends ArrayAdapter<MyCountry>{
Context context;
int layoutResourceId;
ArrayList<MyCountry> countries;
ContextualActionMode activity;
public CountryAdapter(Context context, int layoutResourceId,
        ArrayList<MyCountry> countries) {
    super(context, layoutResourceId, countries);
    // TODO Auto-generated constructor stub
    this.context = context;
    this.countries = countries;
    this.layoutResourceId = layoutResourceId;
    activity = (ContextualActionMode) context;
}
@Override
public int getCount() { 
    return countries.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    //View v;
    final MyCountry country = countries.get(position);
    ViewHolder viewHolder = null;
    if(convertView == null) 
    {
        viewHolder = new ViewHolder();
        //bươc 1: khoi tao doi tuong layoutInflater
        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) {
            // 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();
            }
        }
    });
    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 onPrepareActionMode(ActionMode mode, Menu menu) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        // TODO Auto-generated method stub
        getMenuInflater().inflate(R.menu.floating_menu, menu);
        return true;
    }
    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        // TODO Auto-generated method stub

        switch (item.getItemId()) {
        case R.id.mnDelete:
            //for(int i = countries.size()-1; i >= 0; i--)
            for (int i = 0; i < countries.size(); i++)
            {
                if (countries.get(i).isCheck()) {

                    countries.remove(countries.get(i));

                }

            }

            adapter.notifyDataSetChanged();
            mode.finish();
            return true;
        default:
            break;
        }
        return false;
    }
};


ActionMode actionMode;

void initData() {
    String[] arrNameVi = getResources().getStringArray(R.array.nameVi);
    String[] arrNameEn = getResources().getStringArray(R.array.nameEn);
    TypedArray arrFlag = getResources().obtainTypedArray(
            R.array.country_flag);
    //Boolean[] checkbox=getResources().
    for (int i = 0; i < arrNameEn.length; i++)
        countries.add(new MyCountry(arrNameEn[i], arrNameVi[i], arrFlag
                .getDrawable(i)));

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initData();
    listView = (ListView) findViewById(R.id.listView1);
    adapter = new CountryAdapter(this, R.layout.item_listview_layout,
            countries);

    listView.setAdapter(adapter);

    listView.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int position, long arg3) {
            // TODO Auto-generated method stub
            actionMode = startActionMode(callback);
            actionMode.setTag(position);
            return false;
        }
    });
}

}

也许这里有问题

case R.id.mnDelete:
            //for(int i = countries.size()-1; i >= 0; i--)
            for (int i = 0; i < countries.size(); i++)
            {
                if (countries.get(i).isCheck()) {
                 countries.remove(countries.get(i));    
                }

            }

            adapter.notifyDataSetChanged();
            mode.finish();
4

0 回答 0