Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Shane McC
3,005 PointsAndroid Hide Text if not data is found
I'm extracting data from a database and displaying it on my app. Not every item I'm displaying on my app has the same information so in my text view I'm getting blank items that are appearing. That's something I def. don't want. My question is, is their a android method that will not display blank information? Can I somehow hide the xml?
Shane McC
3,005 PointsDavid, I can see what your saying and thanks for your help but this doesn't completely answer my question. If I'm pulling null or blank data from my database then i don't want that white space to be visible in my view. How would I account for that white space? Thanks
Below is my syntax account for not a null value but I'm still getting white space in my view?
infoHolder.yelp = (TextView) convertView.findViewById(R.id.theYelp);
if(infoHolder.yelp !=null){
infoHolder.yelp.setVisibility(View.VISIBLE);
} else {
infoHolder.yelp.setVisibility(View.INVISIBLE);
}
David Hope
19,876 PointsOkay. then you need to use (View.GONE) instead of View.INVISIBLE for your else condition, Shane. If that doesn't work then using an Adapter for your the data in your TextView and the method isEmpty() for the else condition should hopefully do the trick. Here's the documentation for it https://developer.android.com/reference/android/widget/Adapter.html
Hope this helps and thanks for clarifying what your problem is.
-David.
Shane McC
3,005 PointsHi David
Thanks for the help. I appreciate it but I'm still at loggerheads.
Below is an example of my XML (I've opted not to display the entire file)
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="Facebook"
android:autoLink="web"
android:id="@+id/theFacebook"
android:layout_column="0"
android:layout_weight="1" />
</TableRow>
Here is my logic. Here I'm using isEnabled() method
infoHolder.facebook = (TextView) convertView.findViewById(R.id.theFacebook);
if(infoHolder.facebook != null){
infoHolder.facebook.setVisibility(View.VISIBLE);
} else if (infoHolder.facebook.isEnabled()) {
infoHolder.facebook.setVisibility(View.GONE);
Toast.makeText(activity, "Facebook Gone", Toast.LENGTH_LONG).show();
}
I tested it out again and here is my other logic. Here I typed cast Adapter and used isEmpty()
infoHolder.facebook = (TextView) convertView.findViewById(R.id.theFacebook);
if(infoHolder.facebook != null){
infoHolder.facebook.setVisibility(View.VISIBLE);
} else if (((Adapter) infoHolder.facebook).isEmpty()) {
infoHolder.facebook.setVisibility(View.GONE);
Toast.makeText(activity, "Facebook Gone", Toast.LENGTH_LONG).show();
}
It's still not working. What am I doing wrong?
Thanks again
David Hope
19,876 PointsDavid Hope
19,876 PointsYou can and here's the Android Documentation for it, Shane.
https://developer.android.com/reference/android/view/View.html#attr_android:visibility https://developer.android.com/reference/android/view/View.html#setVisibility%28int%29
Hope this helps! -David