在android中onBackPressed时取消选中所有选中的复选框

时间:2016-07-14 10:46:21

标签: android listview android-checkbox

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

在课堂内:我得到一个观察者的arraylist来取消选中复选框。

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

ViewHolder:

private static class ViewHolder {
    TextView topView;
    TextView bottomView;
    TextView dateView;
    ImageView icon;
    CheckBox checkBox;
}

getView:

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.

2 个答案:

答案 0 :(得分:2)

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

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;
    }
}

答案 1 :(得分:1)

我建议您在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[] loadAry(String arrayNam, Context mContext) {  

    SharedPreferences prefs = mContext.getSharedPreferences("prefname", 0);  
    int size = prefs.getInt(arrayNam + "size", 0);  
    Boolean array[] = new Boolean[size];  
    for(int i=0;i<size;i++)  
        array[i] = prefs.getBoolean(arrayNam + "_" + i, false);  

    return array;  
}

然后onBackPressed检查值