1

i have alistview when i click the list view it goes to login screen, when login is successful it comes back to the listview with a icon ..so far its working good, problem is when the icon appear again if i click on the listview the loginscreen is comming ..i dnt want the login screen to load again once the icon is visible..i tried the following but it is giving errors

if (img.getVisibility() == 8) { 
    Intent intent = new Intent(MainActivity.this,LoginActivity.class);                  
    startActivity(intent);
}

Any help is appreciated.

Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
teekib
  • 2,821
  • 10
  • 39
  • 75

5 Answers5

4

use

if (img.getVisibility() != View.Visible)

Dont use hardcode values.

sandeep
  • 481
  • 1
  • 6
  • 14
2

Change your condition as:

if (img.getVisibility() == View.Visible) 

EDIT : or better way you can use View.isShown() for checking View or it's child's are Visible or not

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
1

You should not really on your UI state for app logic. You would better use startActivityForResult, then in onActivityResult set a flag and use this flag in your click listener, use it also to make the icon visible or not.

You should also consider setting the flag in the shared preferences for persistence if you leave your activity.

Snicolas
  • 37,840
  • 15
  • 114
  • 173
  • http://stackoverflow.com/questions/10407159/android-how-to-manage-start-activity-for-result. That's a good start. It allows to create a second activity and get some result back to the first. – Snicolas Nov 30 '12 at 15:33
  • @Snicolas..can u suggest how to set the flag in shared preference..thankyou – teekib Dec 04 '12 at 15:19
1

what kind of error you face ?? can you show it to use ? you should not use a hard code value to compare the visibility use one of these View.GONE or View.INVISIBLE or VIEW.VISIBLE

like this

if(img.getVisibility != View.VISIBLE){
// do something 
}
confucius
  • 13,127
  • 10
  • 47
  • 66
  • tried the above peice of code..still error..getting nullpointer exception on tht line – teekib Nov 30 '12 at 11:15
  • this line return null img = (ImageView) view.findViewById(R.id.redeye); – confucius Nov 30 '12 at 14:52
  • where you make setOnClickListener with a Loginplaces class instance – confucius Nov 30 '12 at 15:02
  • in an adapter..Loginmyplaces loginmyplaces=new Loginmyplaces(activity); layer.setOnClickListener(loginmyplaces); – teekib Nov 30 '12 at 15:19
  • note that Loginmyplaces class should contains an imageview with id of "redeye" so when you make findViewById it will not return null if the id redeye exist in it :) – confucius Nov 30 '12 at 16:07
0

Use

if (img.getVisibility() != View.Visible)

instead of

if (img.getVisibility() == 8)

check http://developer.android.com/reference/android/view/View.html#setVisibility%28int%29

Nermeen
  • 15,883
  • 5
  • 59
  • 72