在android中按OnBack时取消选中所有复选框

在android中按OnBack时取消选中所有复选框,android,listview,android-checkbox,Android,Listview,Android Checkbox,我有一个带有复选框的列表视图。我在EventHandler类中排列我的listview。当按下后退按钮时,我想取消选中所有复选框。我在另一个活动中调用onBackPressed方法。所以我应该为它编写另一个返回视图的函数。请帮忙。我找了很多,但找不到确切的答案 在类内部:我得到一个视图持有者的arraylist以取消选中复选框 private ArrayList<ViewHolder> mViewHolders = new ArrayList<>(); getView:

我有一个带有复选框的列表视图。我在EventHandler类中排列我的listview。当按下后退按钮时,我想取消选中所有复选框。我在另一个活动中调用onBackPressed方法。所以我应该为它编写另一个返回视图的函数。请帮忙。我找了很多,但找不到确切的答案

在类内部:我得到一个视图持有者的arraylist以取消选中复选框

private ArrayList<ViewHolder> mViewHolders = new ArrayList<>();
getView:

private static class ViewHolder {
    TextView topView;
    TextView bottomView;
    TextView dateView;
    ImageView icon;
    CheckBox checkBox;
}
public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder mViewHolder;
    int num_items = 0;
    String temp = mFileMang.getCurrentDir();
    File file = new File(temp + "/" + mDataSource.get(position));
    String[] list = file.list();

    if(list != null)
        num_items = list.length;

    if(convertView == null) {
        LayoutInflater inflater = (LayoutInflater) mContext.
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.tablerow, parent, false);

        mViewHolder = new ViewHolder();
        mViewHolder.topView =(TextView)convertView.findViewById(R.id.top_view);
        mViewHolder.bottomView = (TextView)convertView.findViewById(R.id.bottom_view);
        mViewHolder.dateView =(TextView) convertView.findViewById(R.id.date_view);
        mViewHolder.icon = (ImageView)convertView.findViewById(R.id.row_image);
        mViewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.row_checkBox);
        convertView.setTag(mViewHolder);

        } else {
            mViewHolder = (ViewHolder)convertView.getTag();

        }  // I also make some text setting inside this.

我建议您将复选框值存储在pref中,并更改复选框onbackpress的值

在Pref中存储布尔数组的步骤

public boolean storebaray(Boolean[] array, String arrayNam, Context mContext) {   

    SharedPreferences prefs = mContext.getSharedPreferences("prefname", 0);  
    SharedPreferences.Editor editor = prefs.edit();  
    editor.putInt(arrayNam +"size", array.length);  

    for(int i=0;i<array.length;i++)  
        editor.putBoolean(arrayNam + "_" + i, array[i]); 

    return editor.commit();  
}
public boolean storebaray(boolean[]数组,String arrayNam,Context mContext){
SharedReferences prefs=mContext.getSharedReferences(“prefname”,0);
SharedReferences.Editor=prefs.edit();
editor.putInt(arrayNam+“size”,array.length);

对于(int i=0;i我建议您将复选框值存储在pref中,并更改复选框onbackpress的值

在Pref中存储布尔数组的步骤

public boolean storebaray(Boolean[] array, String arrayNam, Context mContext) {   

    SharedPreferences prefs = mContext.getSharedPreferences("prefname", 0);  
    SharedPreferences.Editor editor = prefs.edit();  
    editor.putInt(arrayNam +"size", array.length);  

    for(int i=0;i<array.length;i++)  
        editor.putBoolean(arrayNam + "_" + i, array[i]); 

    return editor.commit();  
}
public boolean storebaray(boolean[]数组,String arrayNam,Context mContext){
SharedReferences prefs=mContext.getSharedReferences(“prefname”,0);
SharedReferences.Editor=prefs.edit();
editor.putInt(arrayNam+“size”,array.length);

对于(int i=0;i根据我的评论,工作流应如下所示:
您的活动:

public class MainActivity extends AppCompatActivity {

private ListView mListView;
private CustomAdapter mCustomAdapter;
private List<YourCustomClass> mYourCustomItems;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    // don't forget to init everything you need
    mYourCustomItems = new ArrayList<>();
    mListView = (ListView) findViewById(R.id.list_view);
    mCustomAdapter = new CustomAdapter(mYourCustomItems);
    mListView.setAdapter(mCustomAdapter);
}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (keyCode){
        case  KeyEvent.KEYCODE_BACK:
            //all your needed stuff
            for (YourCustomClass item : mYourCustomItems){
                item.setChecked(false);
            }
            mCustomAdapter.notifyDataSetChanged();
            return true;
    }
    return super.onKeyDown(keyCode, event);
}

private class CustomAdapter extends BaseAdapter{

    private List<YourCustomClass> mYourCustomClasses;

    public CustomAdapter(List<YourCustomClass> yourCustomClasses) {
        mYourCustomClasses = yourCustomClasses;
    }

    @Override
    public int getCount() {
        return mYourCustomClasses.size();
    }

    @Override
    public Object getItem(int position) {
        return mYourCustomClasses.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //all view holder related stuff and other initialization
        CheckBox checkBox = itemView.findViewById(R.id.checkbox);
        checkBox.setChecked(mYourCustomClasses.get(position).isChecked());
        return convertView;
    }
}
public类MainActivity扩展了AppCompatActivity{
私有列表视图;
专用自定义适配器mCustomAdapter;
私有列表mYourCustomItems;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//别忘了准备你需要的一切
mYourCustomItems=newarraylist();
mListView=(ListView)findViewById(R.id.list\u视图);
mCustomAdapter=新的自定义适配器(mYourCustomItems);
setAdapter(mCustomAdapter);
}
@凌驾
公共布尔onKeyDown(int-keyCode,KeyEvent事件){
开关(钥匙代码){
case KeyEvent.KEYCODE\u返回:
//所有你需要的东西
对于(YourCustomClass项:mYourCustomItems){
项。设置已检查(假);
}
mCustomAdapter.notifyDataSetChanged();
返回true;
}
返回super.onKeyDown(keyCode,event);
}
私有类CustomAdapter扩展了BaseAdapter{
私有列表myourcustomclass;
公共CustomAdapter(列出您的CustomClass){
mYourCustomClasses=yourCustomClasses;
}
@凌驾
public int getCount(){
返回mYourCustomClasses.size();
}
@凌驾
公共对象getItem(int位置){
返回mYourCustomClasses.get(position);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//所有与视图持有者相关的内容和其他初始化
CheckBox=itemView.findviewbyd(R.id.CheckBox);
setChecked(mYourCustomClasses.get(position.isChecked());
返回视图;
}
}

根据我的评论,工作流程应如下所示:
您的活动:

public class MainActivity extends AppCompatActivity {

private ListView mListView;
private CustomAdapter mCustomAdapter;
private List<YourCustomClass> mYourCustomItems;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    // don't forget to init everything you need
    mYourCustomItems = new ArrayList<>();
    mListView = (ListView) findViewById(R.id.list_view);
    mCustomAdapter = new CustomAdapter(mYourCustomItems);
    mListView.setAdapter(mCustomAdapter);
}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (keyCode){
        case  KeyEvent.KEYCODE_BACK:
            //all your needed stuff
            for (YourCustomClass item : mYourCustomItems){
                item.setChecked(false);
            }
            mCustomAdapter.notifyDataSetChanged();
            return true;
    }
    return super.onKeyDown(keyCode, event);
}

private class CustomAdapter extends BaseAdapter{

    private List<YourCustomClass> mYourCustomClasses;

    public CustomAdapter(List<YourCustomClass> yourCustomClasses) {
        mYourCustomClasses = yourCustomClasses;
    }

    @Override
    public int getCount() {
        return mYourCustomClasses.size();
    }

    @Override
    public Object getItem(int position) {
        return mYourCustomClasses.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //all view holder related stuff and other initialization
        CheckBox checkBox = itemView.findViewById(R.id.checkbox);
        checkBox.setChecked(mYourCustomClasses.get(position).isChecked());
        return convertView;
    }
}
public类MainActivity扩展了AppCompatActivity{
私有列表视图;
专用自定义适配器mCustomAdapter;
私有列表mYourCustomItems;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//别忘了准备你需要的一切
mYourCustomItems=newarraylist();
mListView=(ListView)findViewById(R.id.list\u视图);
mCustomAdapter=新的自定义适配器(mYourCustomItems);
setAdapter(mCustomAdapter);
}
@凌驾
公共布尔onKeyDown(int-keyCode,KeyEvent事件){
开关(钥匙代码){
case KeyEvent.KEYCODE\u返回:
//所有你需要的东西
对于(YourCustomClass项:mYourCustomItems){
项。设置已检查(假);
}
mCustomAdapter.notifyDataSetChanged();
返回true;
}
返回super.onKeyDown(keyCode,event);
}
私有类CustomAdapter扩展了BaseAdapter{
私有列表myourcustomclass;
公共CustomAdapter(列出您的CustomClass){
mYourCustomClasses=yourCustomClasses;
}
@凌驾
public int getCount(){
返回mYourCustomClasses.size();
}
@凌驾
公共对象getItem(int位置){
返回mYourCustomClasses.get(position);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//所有与视图持有者相关的内容和其他初始化
CheckBox=itemView.findviewbyd(R.id.CheckBox);
setChecked(mYourCustomClasses.get(position.isChecked());
返回视图;
}
}

在活动的onBackPressed()中创建回调,激发回调方法,在适配器中,实现回调,在回调方法主体中,取消选中复选框。但也要考虑到当您要禁用一个复选框,或仅可见,或仅禁用所有复选框时的情况。我想清除所有复选框。为什么您需要依赖ViewHolder?对我来说,最好是“错误选择”数据集中的布尔值取决于您将要设置的复选框的未选中状态。单击“上一步”按钮时,只需在数据集中进行检查,并使所有错误设置==false,然后调用notifyDataSetChanhed()@EagleEye我不知道如何做您能帮上一点忙的所有事情?在onBackPressed()中的“活动”中创建回调,激发回调方法,在适配器中实现回调,并在回调方法主体中取消选中复选框。但也要考虑要禁用一个复选框或仅禁用一个复选框的情况