notifyDataSetChanged() 在 BaseAdapter 中无法使用 android 中的复选框列表
Posted
技术标签:
【中文标题】notifyDataSetChanged() 在 BaseAdapter 中无法使用 android 中的复选框列表【英文标题】:notifyDataSetChanged() is not working in BaseAdapter with Checkbox list in android 【发布时间】:2018-01-18 05:17:06 【问题描述】:https://i.stack.imgur.com/vUpUM.png
我将列表视图和复选框作为列表项。正如您在图像描述中看到的,Action Master 是父级,Add,Delete 是子级。父级和子级由编码分隔。现在选中 Action Master,添加和删除也应该选中,反之亦然。一切正常好吧,但主要问题是当我们单击 Action Master 时,整个列表应该刷新,应该检查 ADD 和 DELETE,但它没有出现在 UI 上,简而言之,我正在登录但没有在 UI 中获取。
这是我填写时的代码:
dataAdapter = new MyCustomAdapter(AddRolePermission.this, listPageActionMaster);
listView.setAdapter(dataAdapter);
这里是自定义适配器的代码:
class MyCustomAdapter extends BaseAdapter
AddRolePermission addRolePermission;
List<ClsPageActionMaster> list_role_master;
public MyCustomAdapter(AddRolePermission addRolePermission, List<ClsPageActionMaster> lstDemo)
this.addRolePermission=addRolePermission;
this.list_role_master=lstDemo;
@Override
public int getCount()
return list_role_master.size();
@Override
public Object getItem(int position)
return position;
@Override
public long getItemId(int position)
return position;
@Override
public View getView(final int position, View convertView, ViewGroup parent)
if (convertView == null)
///
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.permissions_checkbox, null);
CheckBox checkBox1 = (CheckBox) convertView.findViewById(R.id.checkBox1);
TextView pageid = (TextView) convertView.findViewById(R.id.pageid);
ClsPageActionMaster ObjPageAction = new ClsPageActionMaster();
ObjPageAction = list_role_master.get(position);
Log.v("RolePermissionData", "BEFORE CHECKED");
Log.v("RolePermissionData", "CheckBox-" + String.valueOf(ObjPageAction.getSelected()));
Log.v("RolePermissionData", "PageID-" + String.valueOf(ObjPageAction.getPageid()));
Log.v("RolePermissionData", "PageName-" + String.valueOf(ObjPageAction.getPage_name()));
Log.v("RolePermissionData", "type" + String.valueOf(ObjPageAction.getType()));
Log.v("RolePermissionData", "position" + String.valueOf(position));
Log.v("RolePermissionData", "ActionName" + String.valueOf(ObjPageAction.getAction_name()));
if (ObjPageAction.getType().toString().equalsIgnoreCase("parent"))
checkBox1.setText(" " + ObjPageAction.getPage_name());
checkBox1.setTypeface(null, Typeface.BOLD);
else
checkBox1.setText(" " + ObjPageAction.getAction_name());
checkBox1.setChecked(ObjPageAction.getSelected());
pageid.setText(String.valueOf(ObjPageAction.getPageid()));
checkBox1.setTag(String.valueOf(position));
checkBox1.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
CheckBox cb = (CheckBox) v;
Toast.makeText(AddRolePermission.this, cb.getText().toString()+"-"+cb.isChecked(), Toast.LENGTH_SHORT).show();
Integer _POSSITION = Integer.parseInt(String.valueOf(cb.getTag()));
ClsPageActionMaster _ObjAction = list_role_master.get(_POSSITION);
//check is parent? or not
//if yes then find all chied and check them if not
if (_ObjAction.getType().equalsIgnoreCase("parent"))
Log.v("CHECKBOXlist", "parent true -" + String.valueOf(_ObjAction.getSelected()));
Integer _parentPageID = _ObjAction.getPageid();
Log.v("CHECKBOXlist", "_parentPageID -" + String.valueOf(_parentPageID));
for (ClsPageActionMaster ObjPageActionMaster : list_role_master)
if (_parentPageID == ObjPageActionMaster.getPageid())
Log.v("CHECKBOXlist", "pageid == actionPagID -" + String.valueOf(_ObjAction.getPageid()));
Log.v("CHECKBOXlist", "_parentPageID -" + String.valueOf(ObjPageActionMaster.getPageid()));
ObjPageActionMaster.setSelected(cb.isChecked());
list_role_master.set(list_role_master.indexOf(ObjPageActionMaster), ObjPageActionMaster);
dataAdapter.notifyDataSetChanged();
else
Log.v("CHECKBOXlist", "CHK_CHECKED-" + String.valueOf(_ObjAction.getSelected()));
_ObjAction.setSelected(cb.isChecked());
list_role_master.set(_POSSITION, _ObjAction);
Log.v("CHECKBOXlist", "AFTER CHECKED");
Log.v("CHECKBOXlist", "CHK_POSITION-" + String.valueOf(_POSSITION));
Log.v("CHECKBOXlist", "CHK_ID-" + String.valueOf(_ObjAction.getId()));
Log.v("CHECKBOXlist", "chk_name-" + String.valueOf(_ObjAction.getAction_name()));
Log.v("CHECKBOXlist", "type" + String.valueOf(_ObjAction.getType()));
Log.v("CHECKBOXlist", "Position" + String.valueOf(position));
Log.v("CHECKBOXlist", "CHK_CHECKED-" + String.valueOf(_ObjAction.getSelected()));
);
return convertView;
即使我已经写了 dataAdapter.notifyDataSetChanged(); 仍然没有刷新我已经在适配器中编写了复选框单击事件,所以我该如何刷新这就是问题,通过每个复选框单击,它应该是刷新,以便我可以获得所需的输出。 感谢您的帮助。谢谢
【问题讨论】:
你不需要声明自定义适配器,您必须在复选框单击事件上修改列表(在适配器中传递),并在列表中更新后使用 dataAdapter.notifyDataSetChanged()。希望它对你有用 你能告诉我一些代码吗,因为我没有得到...... @GuptaShashwat 【参考方案1】:假设你有一个列表和适配器:
ArrayList<String> my_list=new ArrayList<>();
my_list.add(contents);
ArrayAdapter<> my_adapter = new ArrayAdapter<>(AgentCreateIntimation.this, android.R.layout.simple_spinner_dropdown_item, my_list);
找到你的复选框
CheckBox ChkBx = ( CheckBox ) findViewById( R.id.checkbox );
ChkBx.setOnCheckedChangeListener(new OnCheckedChangeListener()
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
if ( isChecked )
my_list.add(contents);
my_adapter.notifyDataSetChanged()
// perform logic
);
【讨论】:
以上是关于notifyDataSetChanged() 在 BaseAdapter 中无法使用 android 中的复选框列表的主要内容,如果未能解决你的问题,请参考以下文章
[完成]notifyDataSetChanged() 不会自动更新 ListActivity
在 notifyDataSetChanged 后 Android ListView 不刷新
ArrayAdapter.NotifyDataSetChanged() 不工作?