Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
143 views
in Technique[技术] by (71.8m points)

android - Unable to set visibility of constraintlayout to gone if string is null

I have paths in my sqlite database that i transform them in picture with glide library ..the imageviews are grouped in constraintlayout ..i want to set that constraintlayout to invisible if there is no path in mysqlite database..

code that'ive tried:

Public class Nostrils extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
private android.support.v7.widget.Toolbar mToolbar;
ExpandableListView expandableListView;
Database mDatabase;
private static RequestOptions noTransformOptions;
ConstraintLayout constraintLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nostrils);


    mDatabase = new Database(this);
    mDatabase.open();
    Cursor cursor = mDatabase.getDatabase();
    startManagingCursor(cursor);

    String[] childFrom = new String[]{Database.DATABASE_CHILD_1,Database.DATABASE_CHILD_2,Database.DATABASE_CHILD_3};
    String[] groupFrom = new String[]{Database.DATABASE_GROUP_1};

    int[] groupTo = {R.id.group1};
    int[] childTo = {R.id.child1,R.id.child2,R.id.child3};

    SimpleCursorTreeAdapter simplecursortreeAdapter = new ExpandableListViewAdapter(
            this,
            cursor,
            R.layout.list_group,
            groupFrom,
            groupTo,
            R.layout.list_child,
            childFrom,
            childTo
    );

    simplecursortreeAdapter.setViewBinder(new MyViewBinder());

    expandableListView = findViewById(R.id.expandableListview2);
    expandableListView.setAdapter(simplecursortreeAdapter);
}

protected void onDestroy() {
    super.onDestroy();
    mDatabase.close();
}


public class MyViewBinder implements SimpleCursorTreeAdapter.ViewBinder {

    @Override
    public boolean setViewValue(View view, 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(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(friend_name);
                break;

                    contactProfile.setBackgroundResource(0);
                }
                break;


            case R.id.child2 :
                ImageView url = (ImageView) view;
                String urls;


          urls = cursor.getString(cursor.getColumnIndex(Database.DATABASE_CHILD_2));
            ConstraintLayout constraintLayout=(ConstraintLayout) findViewById(R.id.gridview);
            if(urls!= null) {
                constraintLayout.setVisibility(View.VISIBLE);}
            Glide.with(Nostrils.this).load(urls).apply(noTransformation()).into(url);
            url.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    AlertDialog.Builder mBuilder = new AlertDialog.Builder(Nostrils.this);
                    View mView = getLayoutInflater().inflate(R.layout.dialog_custom_layout, null);
                    PhotoView photoView = mView.findViewById(R.id.imageView);
                    photoView.setImageDrawable(((ImageView)view).getDrawable());
                    mBuilder.setView(mView);
                    AlertDialog mDialog = mBuilder.create();
                    mDialog.show();
                }
            });
            break;

        case R.id.child3 :
            ImageView url3 = (ImageView) view;
            String urls3;
            urls3 = cursor.getString(cursor.getColumnIndex(Database.DATABASE_CHILD_3));
            Glide.with(Nostrils.this).load(urls3).apply(noTransformation()).into(url3);
            url3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    AlertDialog.Builder mBuilder = new AlertDialog.Builder(Nostrils.this);
                    View mView = getLayoutInflater().inflate(R.layout.dialog_custom_layout, null);
                    PhotoView photoView = mView.findViewById(R.id.imageView);
                    photoView.setImageDrawable(((ImageView)view).getDrawable());
                    mBuilder.setView(mView);
                    AlertDialog mDialog = mBuilder.create();
                    mDialog.show();
                }
            });
            break;


        }
        return true;
    }

}
 private class ExpandableListViewAdapter extends SimpleCursorTreeAdapter {
        private ExpandableListViewAdapter(
                Context context,
                Cursor cursor,
                int groupLayout,
                String[] groupFrom,
                int[] groupTo,
                int childLayout,
                String[] childFrom,
                int[] childTo) { super(context, cursor, groupLayout, groupFrom, groupTo, childLayout, childFrom, childTo); }

        protected Cursor getChildrenCursor(Cursor groupCursor) {
            return mDatabase.getID(groupCursor.getInt(groupCursor.getColumnIndex(Database.DATABASE_ID)));
        }

actibity_nostrils.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/activity_main3"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Nostrils"
    >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <include layout="@layout/navigationactionbarbleu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>


    <ExpandableListView
        android:id="@+id/expandableListview2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</LinearLayout>
<android.support.design.widget.NavigationView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    app:menu="@menu/navigation_menu"
    android:layout_gravity="start"
    android:id="@+id/nv3"
    android:background="@color/colorbar"
    app:headerLayout="@layout/navigation_header"
    >

</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

child.xml

<?xml version="1.0" encoding="utf-8"?>
<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="8dp">

    <TextView
        android:id="@+id/child1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:paddingBottom="5dp"
        android:paddingLeft="0dp"
        android:paddingRight="0dp"
        android:paddingTop="2dp"
        android:fontFamily="@font/times"
        android:textColor="@android:color/black"

        />

    <android.support.constraint.ConstraintLayout
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_below="@+id/child1"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/child2"
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0" />

        <ImageView
            android:id="@+id/child3"
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="20dp"
            android:layout_marginTop="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/child4"
            app:layout_constraintHorizontal_bias="0.227"
            app:layout_constraintStart_toEndOf="@+id/child2"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0" />

        <ImageView
            android:id="@+id/child4"
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.942"
            app:layout_constraintStart_toEndOf="@+id/child2"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0" />

    </android.support.constraint.ConstraintLayout>

</LinearLayout>

The error msg:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.constraint.ConstraintLayout.setVisibility(int)' on a null object reference

But the app keep stopping when i open a child! Thanks for the help! PS i can't add full class(as was asked) without having too much text ..so ths bloc is unrelated and have no meaning

'Morocco has a population of over 33.8 million and an area of 710,850 km2 (274,460 sq mi). Its capital is Rabat, and the largest city is Casablanca. Other major cities include Marrakesh, Tangier, Salé, Fes, Meknes and Oujda. A historically prominent regional power, Morocco has a history of independence not shared by its neighbours. Since the foundation of the first Moroccan state by Idris I in 788 AD, the country has been ruled by a series of independent dynasties, reaching its zenith under the Almoravid dynasty and Almohad dynasty, spanning parts of Iberia and northwestern Africa. The Marinid and Saadi dynasties continued the struggle against foreign domination, and Morocco remained the only Northwest African country to avoid Ottoman occupation. The Alaouite dynasty, the current ruling dynasty, seized power in 1631. In 1912, Morocco was divided into French and Spanish protectorates, with an international zone in Tangier, and regained its independence in 1956. Moroccan culture is a blend of Berber, Arab, West African and European influences.'

See Question&Answers more detail:<a href="https://stackoverflow.com/questions/51695866/

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

try

ConstraintLayout constraintLayout=(ConstraintLayout) view.getParent()

instead of

ConstraintLayout constraintLayout=(ConstraintLayout) findViewById(R.id.gridview);

Reason is You are doing this under case R.id.child2 It means you need to access the parent of this view


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...