取消选中所有选中的复选框onBackPressed在android
问题描述:
我有一个列表视图与复选框。我在一个EventHandler类中安排我的列表视图。我想在按下后退按钮时取消选中所有复选框。我在另一个活动中调用onBackPressed方法。所以我应该编写另一个函数来返回一个视图。请帮忙。我搜索了很多,但我找不到确切的解决方案。取消选中所有选中的复选框onBackPressed在android
课内:我得到一个Viewholder的数组列表来取消复选框的选择。
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.
答
按我的意见的工作流程应该是这样的:
您的活动:
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;
}
}
+0
谢谢Yurii比我需要更多。 – gunescelil
答
我建议你复选框值存储在PREF和改变复选框的值onbackpress
要商店布尔数组中县
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检查值
在activity中创建一个回调函数,在onBackPressed()中,f ire回调方法,在您的适配器中实现回调并在回调方法体中取消选中复选框。 – EagleEye
但是也要考虑您希望禁用一个复选框或仅显示或仅显示所有这些复选框的情况。 –
我想要清除所有这些 – gunescelil