- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
I have an expandable listview with checkboxes in group and child.
我想要的是当我选中组复选框时,它的所有子项都应该被选中。但是当我运行程序时,我得到了空指针异常。这是我的代码......谢谢
//ExpandableListAdapter.java:
import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class ExpandableListAdapter extends BaseExpandableListAdapter {
LayoutInflater inflater;
/*list of group */
private ArrayList<Group> groups;
private ArrayList<Boolean> isCheckedHeader=new ArrayList<Boolean>();
private HashMap<String, ArrayList<Boolean>> IsCheckedChild;
public ExpandableListAdapter(Context context,ArrayList<Group> groups,ArrayList<Boolean> isCheckedHeader,HashMap<String, ArrayList<Boolean>> IsCheckedChild) {
super();
this.groups=groups;
this.isCheckedHeader=isCheckedHeader;
this.IsCheckedChild=IsCheckedChild;
inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
/**
* @param child
* @param group
* use for adding item to list view
*/
public void addItem(Child child,Group group) {
if(!groups.contains(group)) {
groups.add(group);
}
int index=groups.indexOf(group);
ArrayList<Child> ch=groups.get(index).getChildrens();
ch.add(child);
groups.get(index).setChildrens(ch);
}
public Child getChild(int groupPosition, int childPosition) {
ArrayList<Child> ch=groups.get(groupPosition).getChildrens();
return ch.get(childPosition);
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public int getChildrenCount(int groupPosition) {
ArrayList<Child> ch=groups.get(groupPosition).getChildrens();
return ch.size();
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
Child child= (Child) getChild(groupPosition,childPosition);
TextView childName=null;
if(convertView==null) {
convertView=inflater.inflate(R.layout.child_view, null);
}
final CheckBox chkChild=(CheckBox)convertView.findViewById(R.id.checkBoxC);
childName=(TextView) convertView.findViewById(R.id.textViewChildName);
childName.setText(child.getChildName());
chkChild.setChecked(IsCheckedChild.get(this.groups.get(groupPosition)).get(childPosition));
final int iGroupPos = groupPosition;
final int iChildPos = childPosition;
chkChild.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(chkChild.isChecked()) //the child was enable (checked)
{
IsCheckedChild.get(groups.get(iGroupPos)).set(iChildPos, true);
}
else //the child was disable (non-checked)
{
IsCheckedChild.get(groups.get(iGroupPos)).set(iChildPos, false);
}
}
});
return convertView;
}
public Group getGroup(int groupPosition) {
return groups.get(groupPosition);
}
public int getGroupCount() {
return groups.size();
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
TextView groupName = null;
Group group=(Group) getGroup(groupPosition);
if(convertView==null) {
convertView=inflater.inflate(R.layout.group_view, null);
}
groupName=(TextView) convertView.findViewById(R.id.textViewGroupName);
groupName.setText(group.getGroupName());
final CheckBox chkParent=(CheckBox)convertView.findViewById(R.id.checkBoxH);
chkParent.setChecked(isCheckedHeader.get(groupPosition));
final int iPos = groupPosition;
chkParent.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(chkParent.isChecked()) {
isCheckedHeader.set(iPos, true);
//Checking all SubItem(Childs) of this
int iSizeChilds = getChildrenCount(iPos);
for(int i=0;i<iSizeChilds;i++) {
IsCheckedChild.get(groups.get(iPos)).set(i, true);
}
}
notifyDataSetChanged();
}
});
return convertView;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public boolean hasStableIds() {
return true;
}
}
//组.java:
import java.util.ArrayList;
public class Group {
public String groupId;
public String groupName;
public ArrayList<Child> childrens;
public Group(String groupId, String groupName,
ArrayList<Child> childrens) {
super();
this.groupId = groupId;
this.groupName = groupName;
this.childrens = childrens;
}
public String getGroupId() {
return groupId;
}
public void setGroupId(String groupId) {
this.groupId = groupId;
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
public ArrayList<Child> getChildrens() {
return childrens;
}
public void setChildrens(ArrayList<Child> childrens) {
this.childrens = childrens;
}
}
//Child.java:
public class Child {
public String chiledId;
public String childName;
public Child(String chiledId, String childName) {
super();
this.chiledId = chiledId;
this.childName = childName;
}
public String getChiledId() {
return chiledId;
}
public void setChiledId(String chiledId) {
this.chiledId = chiledId;
}
public String getChildName() {
return childName;
}
public void setChildName(String childName) {
this.childName = childName;
}
}
//在MainActivity中动态添加项目。
MainActivity.java:
public class MainActivity extends Activity {
/*our expandable adapter */
ExpandableListAdapter expandableListAdapter;
/*expandable list*/
ExpandableListView expandableListView;
/*list items*/
ArrayList<Group> groups=new ArrayList<Group>();
ArrayList<Boolean> isCheckedHeader;
HashMap<String, ArrayList<Boolean>> IsCheckedChild;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
isCheckedHeader=new ArrayList<Boolean>();
IsCheckedChild=new HashMap<String, ArrayList<Boolean>>();
/*genarate data for list view*/
genarateData();
/*instantiate adapter with our item list*/
expandableListAdapter=new ExpandableListAdapter(this, groups,isCheckedHeader,IsCheckedChild);
/*we get list view*/
expandableListView=(ExpandableListView) findViewById(R.id.expandableListView);
/*set adapter to list view*/
expandableListView.setAdapter(expandableListAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
//Generate dummy data for list view
public void genarateData() {
Group group;
for (int i = 0; i < 10; i++) {
ArrayList<Child> childrens=new ArrayList<Child>();
childrens.clear();
Child child;
for (int j = 0; j < 5; j++) {
child=new Child(""+j, "Child "+j);
childrens.add(child);
}
group=new Group(""+i, " Group "+i, childrens);
groups.add(group);
isCheckedHeader.add(false);
}
}
}
这是我的日志。
09-16 03:24:19.341: I/Process(1863): Sending signal. PID: 1863 SIG: 9
09-16 03:24:27.871: D/dalvikvm(1903): GC_FOR_ALLOC freed 59K, 5% free 2949K/3092K, paused 31ms, total 32ms
09-16 03:24:27.871: I/dalvikvm-heap(1903): Grow heap (frag case) to 3.562MB for 635812-byte allocation
09-16 03:24:27.921: D/dalvikvm(1903): GC_FOR_ALLOC freed 2K, 4% free 3567K/3716K, paused 40ms, total 40ms
09-16 03:24:28.441: D/AndroidRuntime(1903): Shutting down VM
09-16 03:24:28.441: W/dalvikvm(1903): threadid=1: thread exiting with uncaught exception (group=0xb3a12ba8)
09-16 03:24:28.451: E/AndroidRuntime(1903): FATAL EXCEPTION: main
09-16 03:24:28.451: E/AndroidRuntime(1903): Process: com.android.guide.expandablelistview, PID: 1903
09-16 03:24:28.451: E/AndroidRuntime(1903): java.lang.NullPointerException
09-16 03:24:28.451: E/AndroidRuntime(1903): at com.android.guide.expandablelistview.ExpandableListAdapter.getGroupView(ExpandableListAdapter.java:117)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:446)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.widget.AbsListView.obtainView(AbsListView.java:2263)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.widget.ListView.makeAndAddView(ListView.java:1790)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.widget.ListView.fillDown(ListView.java:691)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.widget.ListView.fillFromTop(ListView.java:752)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.widget.ListView.layoutChildren(ListView.java:1630)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.widget.AbsListView.onLayout(AbsListView.java:2091)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.view.View.layout(View.java:14817)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.view.ViewGroup.layout(ViewGroup.java:4631)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.view.View.layout(View.java:14817)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.view.ViewGroup.layout(ViewGroup.java:4631)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.view.View.layout(View.java:14817)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.view.ViewGroup.layout(ViewGroup.java:4631)
09-16 03:24:28.451: E/AndroidRuntime(1903): at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:374)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.view.View.layout(View.java:14817)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.view.ViewGroup.layout(ViewGroup.java:4631)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.view.View.layout(View.java:14817)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.view.ViewGroup.layout(ViewGroup.java:4631)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1987)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1744)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.view.Choreographer.doFrame(Choreographer.java:544)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.os.Handler.handleCallback(Handler.java:733)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.os.Handler.dispatchMessage(Handler.java:95)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.os.Looper.loop(Looper.java:136)
09-16 03:24:28.451: E/AndroidRuntime(1903): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-16 03:24:28.451: E/AndroidRuntime(1903): at java.lang.reflect.Method.invokeNative(Native Method)
09-16 03:24:28.451: E/AndroidRuntime(1903): at java.lang.reflect.Method.invoke(Method.java:515)
09-16 03:24:28.451: E/AndroidRuntime(1903): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-16 03:24:28.451: E/AndroidRuntime(1903): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-16 03:24:28.451: E/AndroidRuntime(1903): at dalvik.system.NativeStart.main(Native Method)
09-16 03:25:11.961: I/Process(1903): Sending signal. PID: 1903 SIG: 9
How can I correct this?
PS:I'm a beginner. Thanks :)
最佳答案
试试这段代码,
使用 final Parent parent = parents.get(groupPosition);
而不是使用
Group group=(Group) getGroup(groupPosition);
然后更改checkbox setcheck-
CheckBox checkbox = (CheckBox) convertView.findViewById(R.id.checkbox);
checkbox.setChecked(parent.isChecked());
查看此链接:link
关于android - 带有组和子复选框的可扩展 ListView 给出空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25862841/
我已经为使用 JGroups 编写了简单的测试。有两个像这样的简单应用程序 import org.jgroups.*; import org.jgroups.conf.ConfiguratorFact
我有一个通过 ajax 检索的 json 编码数据集。我尝试检索的一些数据点将返回 null 或空。 但是,我不希望将那些 null 或空值显示给最终用户,或传递给其他函数。 我现在正在做的是检查
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Why does one often see “null != variable” instead of “
嗨在我们公司,他们遵循与空值进行比较的严格规则。当我编码 if(variable!=null) 在代码审查中,我收到了对此的评论,将其更改为 if(null!=variable)。上面的代码对性能有影
我正在尝试使用 native Cordova QR 扫描仪插件编译项目,但是我不断收到此错误。据我了解,这是代码编写方式的问题,它向构造函数发送了错误的值,或者根本就没有找到构造函数。那么我该如何解决
我在装有 Java 1.8 的 Windows 10 上使用 Apache Nutch 1.14。我已按照 https://wiki.apache.org/nutch/NutchTutorial 中提
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: what is “=null” and “ IS NULL” Is there any difference bet
Three-EyedRaven 内网渗透初期,我们都希望可以豪无遗漏的尽最大可能打开目标内网攻击面,故,设计该工具的初衷是解决某些工具内网探测速率慢、运行卡死、服务爆破误报率高以及socks流
我想在Scala中像在Java中那样做: public void recv(String from) { recv(from, null); } public void recv(String
我正在尝试从一组图像补丁中创建一个密码本。我已将图像(Caltech 101)分成20 X 20图像块。我想为每个补丁创建一个SIFT描述符。但是对于某些图像补丁,它不返回任何描述符/关键点。我尝试使
我在验证器类中自动连接的两个服务有问题。这些服务工作正常,因为在我的 Controller 中是自动连接的。我有一个 applicationContext.xml 文件和 MyApp-servlet.
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 已关闭10 年前。 问题必须表现出对要解决的问题的最低程度的了解。告诉我们您尝试过做什么,为什么不起作用,以
大家好,我正在对数据库进行正常的选择,但是 mysql_num_rowsis 为空,我不知道为什么,我有 7 行选择。 如果您发现问题,请告诉我。 真的谢谢。 代码如下: function get_b
我想以以下格式创建一个字符串:id[]=%@&stringdata[]=%@&id[]=%@&stringdata[]=%@&id[]=%@&stringdata[]=%@&等,在for循环中,我得到
我正在尝试使用以下代码将URL转换为字符串: NSURL *urlOfOpenedFile = _service.myURLRequest.URL; NSString *fileThatWasOpen
我正在尝试将NSNumber传递到正在工作的UInt32中。然后,我试图将UInt32填充到NSData对象中。但是,这在这里变得有些时髦... 当我尝试将NSData对象中的内容写成它返回的字符串(
我正在进行身份验证并收到空 cookie。我想存储这个 cookie,但服务器没有返回给我 cookie。但响应代码是 200 ok。 httpConn.setRequestProperty(
我认为 Button bTutorial1 = (Button) findViewById(R.layout.tutorial1); bTutorial1.setOnClickListener
我的 Controller 中有这样的东西: model.attribute("hiringManagerMap",hiringManagerMap); 我正在访问此 hiringManagerMap
我想知道如何以正确的方式清空列表。在 div 中有一个列表然后清空 div 或列表更好吗? 我知道这是一个蹩脚的问题,但请帮助我理解这个 empty() 函数:) 案例)如果我运行这个脚本会发生什么:
我是一名优秀的程序员,十分优秀!