rating stars are hiding as i click over the star image

62 Views Asked by At

My requirement is to create 5 stars when i click bitmap changes,When "Unclicked" bitmap changes back to default.and if star 1 is not clicked then all 4 stars after must not be clickable, if star 2 is clicked then star 3 must be clickable and stars 4-5 must not be clickable, (and then backwards) if all 5 stars have been selected only star5 must be clickable, if star 5 and 4 are unclicked then star3must be clickable stars 2-1 must not be clickable, etc.

public class Starscreen extends MainScreen
{
protected static BitmapField Star1 = null;
protected static BitmapField Star2 = null;
protected static BitmapField Star4 = null;
protected static BitmapField Star5 = null;
protected static final Bitmap StarClicked = null;
protected static BitmapField Star3 = null;
BitmapField bitmapField1;
private Bitmap StarNotClicked;
public  Starscreen(Secondscreen secondscreen)
{
    setTitle("Star Screen"); 
LabelField RateDeal = new LabelField("Rating: ");
     Mainlayout.add(RateDeal);
        HorizontalFieldManager StarManager=new    HorizontalFieldManager   
        (USE_ALL_WIDTH);
        final Bitmap StarNotClicked = Bitmap.getBitmapResource("Star3.png");
        final Bitmap StarClicked = Bitmap.getBitmapResource("Star4.png");

     Star1 = new BitmapField(StarNotClicked,BitmapField.FOCUSABLE){
         private Object StarClicked;

        protected boolean navigationClick(int status, int time){
         fieldChangeNotify(1);
         Star1.setBitmap((Bitmap) StarClicked);
         Star2.setBitmap(StarNotClicked);
         Star3.setBitmap(StarNotClicked);
         Star4.setBitmap(StarNotClicked);
         Star5.setBitmap(StarNotClicked);
         AmountOfStarsSelected(1);
         return true;
         }
         };
         Star2 = new BitmapField(StarNotClicked,BitmapField.FOCUSABLE){
         private Object StarClicked;

        protected boolean navigationClick(int status, int time){
         fieldChangeNotify(1);
         Star1.setBitmap((Bitmap) StarClicked);
         Star2.setBitmap((Bitmap) StarClicked);
         Star3.setBitmap(StarNotClicked);
         Star4.setBitmap(StarNotClicked);
         Star5.setBitmap(StarNotClicked);
         AmountOfStarsSelected(2);
         return true;
         }
         };
         Star3 = new BitmapField(StarNotClicked,BitmapField.FOCUSABLE){
        // private Object StarClicked;

        protected boolean navigationClick(int status, int time){
         fieldChangeNotify(1);

         Star1.setBitmap((Bitmap) StarClicked);
         Star2.setBitmap((Bitmap) StarClicked);
         Star3.setBitmap((Bitmap) StarClicked);
         Star4.setBitmap(StarNotClicked);
         Star5.setBitmap(StarNotClicked);
         AmountOfStarsSelected(3);
         return true;
         }
         };
         Star4 = new BitmapField(StarNotClicked,BitmapField.FOCUSABLE){
         protected boolean navigationClick(int status, int time){
         fieldChangeNotify(1);

         Star1.setBitmap(StarClicked);
         Star2.setBitmap(StarClicked);
         Star3.setBitmap(StarClicked);
         Star4.setBitmap(StarClicked);
         Star5.setBitmap(StarNotClicked);
         AmountOfStarsSelected(4);
         return true;
         }
         };
         Star5 = new BitmapField(StarNotClicked,BitmapField.FOCUSABLE){
         protected boolean navigationClick(int status, int time){
         fieldChangeNotify(1);

         Star1.setBitmap(StarClicked);
         Star2.setBitmap(StarClicked);
         Star3.setBitmap(StarClicked);
         Star4.setBitmap(StarClicked);
         Star5.setBitmap(StarClicked);
         AmountOfStarsSelected(5);
         return true;
         }
         };
         StarManager.add(Star1);
         StarManager.add(Star2);
         StarManager.add(Star3);
         StarManager.add(Star4);
         StarManager.add(Star5);
         Mainlayout.add(StarManager);
         add(Mainlayout);
 }
  }

By executing above code i am getting an error and it shows create method for AmountOfStarsSelected(); but i dont have any idea what condition should i use inside AmountOfStarsSelected(); please help me i am new to blackberry

1

There are 1 best solutions below

0
Kevin On

I'd say make a separate class for this, extended from HorizontalFieldManager. Have that class create the 5 buttons and lay them out, and contain a local state variable int level. Create a public setLevel(int level) and getLevel() method in this class. Where level refers to the star currently selected.

In setLevel put the following code:

public void setLevel(int level)
{
    if(Math.abs(this.level - level) != 1)
    {
        return; // this.level refers to your local state. return because there is no change, or the user didn't click 1 up or down.
    }

    if(level == 0)
    {
          Star1.setBitmap(StarClicked);
    }
    else
    {
          Star1.setBitmap(StarNotClicked);
    }

    if(level == 1)
    {
          Star2.setBitmap(StarClicked);
    }
    else
    {
          Star2.setBitmap(StarNotClicked);
    }

    if(level == 2)
    {
          Star3.setBitmap(StarClicked);
    }
    else
    {
          Star3.setBitmap(StarNotClicked);
    }

    if(level == 3)
    {
          Star4.setBitmap(StarClicked);
    }
    else
    {
          Star4.setBitmap(StarNotClicked);
    }

    if(level == 4)
    {
          Star5.setBitmap(StarClicked);
    }
    else
    {
          Star5.setBitmap(StarNotClicked);
    }

    this.level = level;
}

In each of your stars you should call this setLevel method

 Star1 = new BitmapField(StarNotClicked, BitmapField.FOCUSABLE){
      protected boolean navigationClick(int status, int time)
      {
          setLevel(0); // If these fields are the only thing in your horizontal field you can get away with using setLevel(getIndex());
          return true;
      }
 };

Remember that in your constructor you should set level to -1, then call setLevel(0) so that the first star can be selected by default.