0

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.imgur.com/cj9dWSX.png

Zouhair
  • 115
  • 1
  • 2
  • 11

1 Answers1

0

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(...);
  • Thanks @mTak for taking time to answer my question ..just to explain: i'm trying to hide a line (which is represented as simple view in xml file) , precisly the line to the right(see photo from the emulator at the end of the post) ..when there is no string in database sqlite case, or in other word the case=null (Database.DATABASE_CHILD_2=null) i want both the Image view with id=child2 and the second view(the right one with id=view2) set to invisible ..please see photo from the emulator to understand . – Zouhair Aug 21 '18 at 22:11
  • PS: the code works great without the lines: view1.setVisibility(View.VISIBLE); ..i added the views(= the two lines) to style the childviews ..but when there is two images i want just one line to appear – Zouhair Aug 21 '18 at 22:13