在 android 中 onBackPressed 时取消选中所有选中的复选框
Uncheck all checked checkboxes when onBackPressed in android
我有一个带复选框的列表视图。我在 EventHandler class 中安排我的列表视图。我想在按下后退按钮时取消选中所有复选框。我在另一个 activity 中调用了 onBackPressed 方法。那么我是否应该为那个 returns 视图编写另一个函数。请帮忙。我搜索了很多,但找不到确切的解决方案。
在 class 中:我得到一个 Viewholders 数组列表来取消选中复选框。
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.
我建议您将复选框值存储在 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 检查值
根据我的评论,工作流程应该是这样的:
你的 Activity :
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;
}
}
我有一个带复选框的列表视图。我在 EventHandler class 中安排我的列表视图。我想在按下后退按钮时取消选中所有复选框。我在另一个 activity 中调用了 onBackPressed 方法。那么我是否应该为那个 returns 视图编写另一个函数。请帮忙。我搜索了很多,但找不到确切的解决方案。
在 class 中:我得到一个 Viewholders 数组列表来取消选中复选框。
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.
我建议您将复选框值存储在 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 检查值
根据我的评论,工作流程应该是这样的:
你的 Activity :
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;
}
}