Android 自定义ExpandableListView未完全显示

Android 自定义ExpandableListView未完全显示,android,expandablelistview,Android,Expandablelistview,编辑:修复了空异常 尝试按照教程进行操作后,仅显示组1标题,没有子项。没有显示其他子项的标题。为什么会被切断 这是我的密码: public class WelcomeFragment extends Fragment { private UiLifecycleHelper uiHelper; WebView mWebView; TextView mName, mother, mlblName, mlblOther; ImageView mImage; L

编辑:修复了空异常

尝试按照教程进行操作后,仅显示组1标题,没有子项。没有显示其他子项的标题。为什么会被切断

这是我的密码:

public class WelcomeFragment extends Fragment {

    private UiLifecycleHelper uiHelper;
    WebView mWebView;
    TextView mName, mother, mlblName, mlblOther;
    ImageView mImage;
    LoginButton mAuthButton;


    final static int AUTHORIZE_ACTIVITY_RESULT_CODE = 0;
    String[] permissions = { "user_relationships" };

    private ArrayList<String> groups;
    private ArrayList<ArrayList<ArrayList<String>>> childs;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }       

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_welcome, null);

        uiHelper = new UiLifecycleHelper(getActivity(), callback);
        uiHelper.onCreate(savedInstanceState);

        mName = (TextView) view.findViewById(R.id.txtName);

        mother = (TextView) view.findViewById(R.id.txtOther);   
        mImage = (ImageView) view.findViewById(R.id.profilepicture);  
        mAuthButton = (LoginButton) view.findViewById(R.id.authButton);

        // Get the data
        Cursor c = getActivity().getContentResolver().query(StatusProvider.CONTENT_URI_USER, null, null, null, null);

        if((c.moveToFirst()) && (c.getCount()>0))
        {
            mAuthButton.setVisibility(View.GONE);
            mName.setText(c.getString(c.getColumnIndex(StatusData.KEY_USER_NAME))); //name


            //new ImageDownloader().execute(c.getString(c.getColumnIndex(StatusData.KEY_USER_EMAIL)); //call asynctask
            mother.setText(c.getString(c.getColumnIndex(StatusData.KEY_USER_OTHER))); //significant_other


            AQuery aq = new AQuery(view);

           //returns the cached file by url, returns null if url is not cached
            File file = aq.getCachedFile(c.getString(c.getColumnIndex(StatusData.KEY_USER_PICTURE)));

            if (file == null) {
                Log.i("PROJECTCARUSO", "Did not find user picture on file" );
                //load an image to an ImageView from network, cache image to file and memory
                aq.id(R.id.profilepicture).image(c.getString(c.getColumnIndex(StatusData.KEY_USER_PICTURE)));
            } else {
                Log.i("PROJECTCARUSO", "Found user picture on file" );
                Bitmap myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
                mImage.setImageBitmap(myBitmap);
            }

            Log.i("PROJECTCARUSO", "Found user information in database" );
        } else {

            mlblName = (TextView) view.findViewById(R.id.lblName);

            mlblOther = (TextView) view.findViewById(R.id.lblOther);

            //Hide everything
            mName.setVisibility(View.GONE);
            mother.setVisibility(View.GONE);
            mImage.setVisibility(View.GONE);
            mlblName.setVisibility(View.GONE);
            mlblOther.setVisibility(View.GONE);

            Log.i("PROJECTCARUSO", "Did not find user information in database" );
        }

        mAuthButton.setFragment(this);
        mAuthButton.setReadPermissions(Arrays.asList("email"));

        ExpandableListView l = (ExpandableListView) getActivity().findViewById(R.id.ExpandableListView01);

        loadData();

        myExpandableAdapter adapter = new myExpandableAdapter(getActivity(), groups, childs);
        l.setAdapter(adapter);     
        return view;
    }   

    public void refreshView(){

        // Get the data
        Cursor c = getActivity().getContentResolver().query(StatusProvider.CONTENT_URI_USER, null, null, null, null);

        if((c.moveToFirst()) && (c.getCount()>0))
        {
            mAuthButton.setVisibility(View.GONE);


            //Show everything
            mName.setVisibility(View.VISIBLE);

            mother.setVisibility(View.VISIBLE);
            mImage.setVisibility(View.VISIBLE);
            mlblName.setVisibility(View.VISIBLE);
            mlblOther.setVisibility(View.VISIBLE);

            mName.setText(c.getString(c.getColumnIndex(StatusData.KEY_USER_NAME))); //name


            //new ImageDownloader().execute(c.getString(c.getColumnIndex(StatusData.KEY_USER_EMAIL)); //call asynctask
            mother.setText(c.getString(c.getColumnIndex(StatusData.KEY_USER_OTHER))); //significant_other


            AQuery aq = new AQuery(getActivity());

           //returns the cached file by url, returns null if url is not cached
            File file = aq.getCachedFile(c.getString(c.getColumnIndex(StatusData.KEY_USER_PICTURE)));

            if (file == null) {
                Log.i("PROJECTCARUSO", "Did not find user picture on file" );
                //load an image to an ImageView from network, cache image to file and memory
                aq.id(R.id.profilepicture).image(c.getString(c.getColumnIndex(StatusData.KEY_USER_PICTURE)));
            } else {
                Log.i("PROJECTCARUSO", "Found user picture on file" );
                Bitmap myBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
                mImage.setImageBitmap(myBitmap);
            }

            Log.i("PROJECTCARUSO", "Found user information in database" );
        } else {
            Log.i("PROJECTCARUSO", "Did not find user information in database" );
        }


    }

    private void onSessionStateChange(Session session, SessionState state, Exception exception) {
        if (state.isOpened()) {

            // Request user data and show the results
            Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

                @Override
                public void onCompleted(GraphUser user, Response response) {
                    if (user != null) {
                        Utility.userUID = user.getId();
                        final String name = user.getName();
                        final String fname = user.getFirstName();
                        final String username = user.getUsername();
                        final String email = (String) user.getProperty("email");
                        String picURL = null;
                        String significant_other = null;
                        try {
                            String inputLine = user.getProperty("significant_other").toString();
                            JSONObject json = new JSONObject(inputLine.toString());
                            significant_other = json.get("name").toString();
                        } catch (JSONException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }

                        picURL = "http://graph.facebook.com/"+Utility.userUID+"/picture";
                        Log.i("PROJECTCARUSO", "Logged in...");

                        try{            
                                ContentValues values = new ContentValues();
                                values.put(StatusData.KEY_USER_ROWID, Utility.userUID);
                                values.put(StatusData.KEY_USER_NAME, name);
                                values.put(StatusData.KEY_USER_FNAME, fname);
                                values.put(StatusData.KEY_USER_USERNAME, username);
                                values.put(StatusData.KEY_USER_EMAIL, email);
                                values.put(StatusData.KEY_USER_PICTURE, picURL);
                                values.put(StatusData.KEY_USER_OTHER, significant_other);


                                StatusData StatusData = new StatusData(getActivity());

                                StatusData.insertOrReplaceUser(values);

                                Log.i("PROJECTCARUSO", "Insert of facebook information was successfull ");
                                refreshView();
                        } catch (Exception e ) {
                            Log.i("PROJECTCARUSO", "Insert of facebook information was not successfull ");
                        }
                    }
                }
            });

        } else if (state.isClosed()) {
            Log.i("PROJECTCARUSO", "Logged out...");
            Log.i("PROJECTCARUSO", "Exception: " + exception);
        }
    }

    private Session.StatusCallback callback = new Session.StatusCallback() {
        @Override
        public void call(Session session, SessionState state, Exception exception) {
            onSessionStateChange(session, state, exception);
        }
    };

    @Override
    public void onResume() {
        super.onResume();

        // For scenarios where the main activity is launched and user
        // session is not null, the session state change notification
        // may not be triggered. Trigger it if it's open/closed.
        Session session = Session.getActiveSession();
        if (session != null &&
               (session.isOpened() || session.isClosed()) ) {
            onSessionStateChange(session, session.getState(), null);
        }

        uiHelper.onResume();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        uiHelper.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        uiHelper.onSaveInstanceState(outState);
    }

    public class myExpandableAdapter extends BaseExpandableListAdapter {

        private ArrayList<String> groups;

        private ArrayList<ArrayList<ArrayList<String>>> children;

        private Context context;

        public myExpandableAdapter(Context context, ArrayList<String> groups, ArrayList<ArrayList<ArrayList<String>>> children) {
            this.context = context;
            this.groups = groups;
            this.children = childs;
        }


        @Override
        public boolean areAllItemsEnabled()
        {
            return true;
        }


        @Override
        public ArrayList<String> getChild(int groupPosition, int childPosition) {
            return children.get(groupPosition).get(childPosition);
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }


        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild,View convertView, ViewGroup parent) {

            String child = (String) ((ArrayList<String>)getChild(groupPosition, childPosition)).get(0);

            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.expandablelistview_child, null);
            }

            TextView childtxt = (TextView) convertView.findViewById(R.id.TextViewChild01);

            childtxt.setText(child);

            return convertView;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return children.get(groupPosition).size();
        }

        @Override
        public String getGroup(int groupPosition) {
            return groups.get(groupPosition);
        }

        @Override
        public int getGroupCount() {
            return groups.size();
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

            String group = (String) getGroup(groupPosition);

            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.expandablelistview_group, null);
            }

            TextView grouptxt = (TextView) convertView.findViewById(R.id.TextViewGroup);

            grouptxt.setText(group);

            return convertView;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public boolean isChildSelectable(int arg0, int arg1) {
            return true;
        }

    }

    private void loadData(){
        groups= new ArrayList<String>();
        childs= new ArrayList<ArrayList<ArrayList<String>>>();

        groups.add("Group 1");
        groups.add("Group 2");
        groups.add("Group 3");

        childs.add(new ArrayList<ArrayList<String>>());
        childs.get(0).add(new ArrayList<String>());
        childs.get(0).get(0).add("Child 1 group 1");
        childs.get(0).add(new ArrayList<String>());
        childs.get(0).get(1).add("Child 2 group 1");
        childs.get(0).add(new ArrayList<String>());
        childs.get(0).get(2).add("Child 3 group 1");

        childs.add(new ArrayList<ArrayList<String>>());
        childs.get(1).add(new ArrayList<String>());
        childs.get(1).get(0).add("Child 1 group 2");
        childs.get(1).add(new ArrayList<String>());
        childs.get(1).get(1).add("Child 2 group 2");
        childs.get(1).add(new ArrayList<String>());
        childs.get(1).get(2).add("Child 3 group 2");

        childs.add(new ArrayList<ArrayList<String>>());
        childs.get(2).add(new ArrayList<String>());
        childs.get(2).get(0).add("Child 1 group 3");
        childs.get(2).add(new ArrayList<String>());
        childs.get(2).get(1).add("Child 2 group 3");
        childs.get(2).add(new ArrayList<String>());
        childs.get(2).get(2).add("Child 3 group 3");
    }
}
公共类WelcomeFragment扩展了片段{
私人UiLifecycleHelper uiHelper;
网络视图;
TextView mName,mother,mlblName,mlblotter;
图像视图模拟图像;
登录按钮mAuthButton;
最终静态整数授权\活动\结果\代码=0;
字符串[]权限={“用户关系”};
私有ArrayList组;
私人ArrayList childs;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}       
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
super.onCreate(savedInstanceState);
视图=充气机。充气(R.layout.fragment\u welcome,null);
uiHelper=newUILifecycleHelper(getActivity(),回调);
uiHelper.onCreate(savedInstanceState);
mName=(TextView)view.findViewById(R.id.txtName);
mother=(TextView)view.findViewById(R.id.txtOther);
mImage=(ImageView)view.findViewById(R.id.profilepicture);
mAuthButton=(LoginButton)view.findViewById(R.id.authButton);
//获取数据
游标c=getActivity().getContentResolver().query(StatusProvider.CONTENT\u URI\u USER,null,null,null);
if((c.moveToFirst())&&(c.getCount()>0))
{
mAuthButton.setVisibility(View.GONE);
mName.setText(c.getString(c.getColumnIndex(StatusData.KEY_USER_NAME));//NAME
//new ImageDownloader().execute(c.getString(c.getColumnIndex(StatusData.KEY_USER_EMAIL));//调用asynctask
mother.setText(c.getString(c.getColumnIndex(StatusData.KEY_USER_OTHER));//重要的_OTHER
水渠aq=新水渠(视图);
//按url返回缓存的文件,如果url未缓存,则返回null
File File=aq.getCachedFile(c.getString(c.getColumnIndex(StatusData.KEY\u USER\u PICTURE));
if(file==null){
Log.i(“PROJECTCARUSO”,“未在文件中找到用户图片”);
//将图像从网络加载到ImageView,将图像缓存到文件和内存
aq.id(R.id.profilepicture).image(c.getString(c.getColumnIndex(StatusData.KEY\u USER\u PICTURE));
}否则{
Log.i(“PROJECTCARUSO”,“在文件中找到用户图片”);
位图myBitmap=BitmapFactory.decodeFile(file.getAbsolutePath());
设置图像位图(myBitmap);
}
Log.i(“PROJECTCARUSO”,“在数据库中找到用户信息”);
}否则{
mlblName=(TextView)view.findViewById(R.id.lblName);
mlblotter=(TextView)view.findViewById(R.id.lblOther);
//隐藏一切
mName.setVisibility(View.GONE);
母亲.setVisibility(View.GONE);
mImage.setVisibility(View.GONE);
mlblName.setVisibility(View.GONE);
mlblotter.setVisibility(View.GONE);
Log.i(“PROJECTCARUSO”,“在数据库中未找到用户信息”);
}
mAuthButton.setFragment(this);
mAuthButton.setReadPermissions(Arrays.asList(“email”);
ExpandableListView l=(ExpandableListView)getActivity().findViewById(R.id.ExpandableListView01);
loadData();
myExpandableAdapter=新的myExpandableAdapter(getActivity(),groups,child);
l、 设置适配器(适配器);
返回视图;
}   
公共视图(){
//获取数据
游标c=getActivity().getContentResolver().query(StatusProvider.CONTENT\u URI\u USER,null,null,null);
if((c.moveToFirst())&&(c.getCount()>0))
{
mAuthButton.setVisibility(View.GONE);
//展示一切
mName.setVisibility(View.VISIBLE);
setVisibility(View.VISIBLE);
mImage.setVisibility(View.VISIBLE);
mlblName.setVisibility(View.VISIBLE);
mlblotter.setVisibility(View.VISIBLE);
mName.setText(c.getString(c.getColumnIndex(StatusData.KEY_USER_NAME));//NAME
//new ImageDownloader().execute(c.getString(c.getColumnIndex(StatusData.KEY_USER_EMAIL));//调用asynctask
mother.setText(c.getString(c.getColumnIndex(StatusData.KEY_USER_OTHER));//重要的_OTHER
AQuery aq=新的AQuery(getActivity());
//按url返回缓存的文件,如果url未缓存,则返回null
File File=aq.getCachedFile(c.getString(c.getColumnIndex(StatusData.KEY\u USER\u PICTURE));
if(file==null){
Log.i(“PROJECTCARUSO”,“未在文件中找到用户图片”);
//将图像从网络加载到ImageView,将图像缓存到文件和内存
aq.id(R.id.profilepicture).image(c.getString(c.getColumnIndex(StatusData.KEY\u USER\u PICTURE));
}否则{
Log.i(“PROJECTCARUSO”,“在文件中找到用户图片”);
位图myBitmap=BitmapFactory.decodeFile(file.getAbsolutePath());
设置图像位图(myBitmap);
}
Log.i(“PROJECTCARUSO”,“在数据库中找到用户信息”);
}否则{
Log.i(“PROJECTCARUSO”,“在数据库中未找到用户信息”);
}
}
private void OnSessionState更改(会话、会话状态、异常){
if(state.isOpened()){
//请求用户数据并显示结果
Request.executeRequestAsync(会话,新请求.GraphUserCallback(){
@凌驾
未完成公共无效(GraphUser用户,响应){
如果(用户!=null){
Utility.userUID=user
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="6dip"
        android:layout_marginTop="20dp"
        android:background="#ffcccccc"
        android:textIsSelectable="false" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#ffcccccc" >

        <ImageView
            android:id="@+id/profilepicture"
            android:layout_width="100sp"
            android:layout_height="100sp"
            android:layout_alignParentLeft="true"
            android:contentDescription="@string/lblPicture"
            android:src="@drawable/photofemale" />

        <!-- Name -->

        <TextView
            android:id="@+id/txtName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/lblName"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="20dp"
            android:layout_toRightOf="@+id/profilepicture"
            android:text="@string/txtname"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <!-- significant_other -->

        <TextView
            android:id="@+id/lblOther"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/txtName"
            android:layout_below="@+id/txtName"
            android:text="@string/lblother"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <!-- Logged in Credentials -->

        <TextView
            android:id="@+id/lblName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/txtName"
            android:layout_below="@+id/textView1"
            android:text="@string/lblname"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/txtOther"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/lblOther"
            android:layout_below="@+id/lblOther"
            android:text="@string/txtother"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <!-- Logged in Credentials -->
        <com.facebook.widget.LoginButton
            android:id="@+id/authButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/textView1" />

    </RelativeLayout>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="fill_parent"
        android:layout_height="6dip"
        android:layout_marginBottom="5dp"
        android:background="#ffcccccc"
        android:textIsSelectable="false" />


    <ExpandableListView 
        android:id="@+id/ExpandableListView01" 
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
    >
    </ExpandableListView>

</LinearLayout>
</ScrollView>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#666666"
>

<TextView
    android:id="@+id/TextViewChild01" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="30px"
>
</TextView>

<TextView
    android:id="@+id/TextViewChild02" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10px"
>
</TextView>

<TextView
    android:id="@+id/TextViewChild03" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10px"
>
</TextView>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<TextView
    android:id="@+id/TextViewGroup" 
    android:layout_width="wrap_content"
    android:layout_height="50px"
    android:layout_marginLeft="50px"
    android:gravity="center_vertical"
>
</TextView>

</LinearLayout>
ExpandableListView l = (ExpandableListView) getActivity().findViewById(R.id.ExpandableListView01);
ExpandableListView l = (ExpandableListView) view.findViewById(R.id.ExpandableListView01);