failed to put View set to invisible

32 views Asked by At

I'm trying to put a view (in list_child) set to invisible if one child is null ( in an expandable listview popluated by external sqlite database and using simplecursortreeadapater) ..
I tried to do it like this:
1. Intialise the view inside the viewbinder class and 2. set the condition in case.child2..but i got null pointer for view.setvisibility (when the app crashes) ..what am i doing wrong? thanks

 public class MyViewBinder implements SimpleCursorTreeAdapter.ViewBinder {
    View view1=(View)findViewById(R.id.view1);
            @Override
            public boolean setViewValue(View view, final Cursor cursor, int columnIndex) {
                int viewID = view.getId();
                switch (viewID) {
                    case R.id.group1:
                        TextView groupName = (TextView) view;
                        String groupname;
                        groupname = cursor.getString(cursor.getColumnIndex(Database.DATABASE_GROUP_1));
                        groupName.setText(Html.fromHtml(groupname));

                        break;
                case R.id.child1:
                    TextView friendName = (TextView) view;
                    String friend_name;
                    friend_name = cursor.getString(cursor.getColumnIndex(Database.DATABASE_CHILD_1));
                    friendName.setText(Html.fromHtml(friend_name));
                    break;

                case R.id.child2:
                    final ImageView url = (ImageView) view;
                    final String urls;
                    urls = cursor.getString(cursor.getColumnIndex(Database.DATABASE_CHILD_2));
                    if (urls != null) {
                        Glide.with(AnatomyNostrils.this).load(urls).apply(new RequestOptions().dontTransform().format(DecodeFormat.PREFER_ARGB_8888)).into(url);
                        view.findViewById(R.id.child2).setVisibility(View.VISIBLE);
view1.setVisibility(View.VISIBLE);

                    } else {
                        view.findViewById(R.id.child2).setVisibility(View.GONE);
view1.setVisibility(View.GONE);
                    }
                    url.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            AlertDialog.Builder mBuilder = new AlertDialog.Builder(AnatomyNostrils.this);
                            View mView = getLayoutInflater().inflate(R.layout.dialog_custom_layout, null);
                            PhotoView photoView = mView.findViewById(R.id.imageView);
                            photoView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                            photoView.setImageDrawable(((ImageView) view).getDrawable());
                            Glide.with(AnatomyNostrils.this).load(urls).apply(new RequestOptions().dontTransform().format(DecodeFormat.PREFER_ARGB_8888)).into(photoView);
                            mBuilder.setView(mView);
                            AlertDialog mDialog = mBuilder.create();
                            mDialog.show();
                        }
                    });
                    break;

list_child.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="12dp">

    <TextView
        android:id="@+id/child1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="@dimen/textsizechildexpandablelistview"
        android:textIsSelectable="true"
        android:paddingBottom="5dp"
        android:paddingLeft="0dp"
        android:paddingRight="0dp"
        android:paddingTop="2dp"
        android:textColor="@android:color/black"

        />

    <LinearLayout
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/child1"
        android:visibility="visible"
        android:orientation="horizontal">


        <ImageView
            android:layout_weight="1"
            android:id="@+id/child3"
            android:layout_width="match_parent"
            android:layout_height="@dimen/heightimageviewchildexpandablelistview"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp" />

        <View
            android:layout_width="1dp"
            android:id="@+id/view2"
            android:layout_height="match_parent"
            android:layout_margin="6dp"
            android:background="#cc0033" />

        <ImageView
            android:layout_weight="1"
            android:id="@+id/child2"
            android:layout_width="match_parent"
            android:layout_height="@dimen/heightimageviewchildexpandablelistview"
            android:layout_marginBottom="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginTop="8dp" />

        <View
            android:layout_width="1dp"
            android:id="@+id/view1"
            android:layout_height="match_parent"
            android:layout_margin="6dp"
            android:background="#cc0033" />

    </LinearLayout>

Image from the emulator: https://i.stack.imgur.com/OdqXc.png

1

There are 1 answers

2
AudioBubble On

If your code inside case R.id.child2: is executed then view is the View that you want to show/hide, so replace:

view.findViewById(R.id.child2).setVisibility(...);

with:

view.setVisibility(...);