0

I'm using ViewFLipper with Random.nextInt(). to change layouts onClick (Button). Now I have 3 xml layouts. 1_view.xml 2_view.xml 3_view.xml. Starting from 1_view.xml I have a button there. When button clicked I should get a random layout. it works. But the problem is now, sometimes I get the same layout (1_view.xml). When a user clicks a button on (1_view.xml), I want them to go to the layouts I have left (2_view.xml and 3_view.xml).

Codes

Main.xml

 <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dip" >

        <TextView
            android:id="@+id/TVLeft"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_gravity="left|top"
            android:text="0"
            android:textColor="#4CB848"
            android:textSize="40sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/TVRight"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/TVLeft"
            android:gravity="right"
            android:text="0"
            android:textColor="#FF0000"
            android:textSize="40sp"
            android:textStyle="bold" />
    </RelativeLayout>

    <ViewFlipper
        android:id="@+id/viewFlipper1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <include
            android:id="@+id/1_View"
            layout="@layout/1_view" />

        <include
            android:id="@+id/2_View"
            layout="@layout/2_view" />

        <include
            android:id="@+id/3_View"
            layout="@layout/3_view" />
    </ViewFlipper>
</LinearLayout>

Main.java
        // FlipperView
        MyViewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper1);

        TVRight = (TextView) findViewById(R.id.TVRight);
        TVLeft = (TextView) findViewById(R.id.TVLeft);

        // 1_view.xml
        Q1_btn1 = (Button) findViewById(R.id.btn_1);
        Q1_btn2 = (Button) findViewById(R.id.btn_2);
        Q1_btn3 = (Button) findViewById(R.id.btn_3);
        Q1_btn4 = (Button) findViewById(R.id.btn_4);

        Q1_btn1.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Wrongnum++;
                WrongResult.setText(Integer.toString(Wrongnum));

            }

        });

        Q1_btn2.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                Rightnum++;
                RightResult.setText(Integer.toString(Rightnum));

                Random RandomView = new Random();
                MyViewFlipper.setDisplayedChild(RandomView.nextInt(3));

            }
        });
        Q1_btn3.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Wrongnum++;
                WrongResult.setText(Integer.toString(Wrongnum));

            }
        });
        Q1_btn4.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Wrongnum++;
                WrongResult.setText(Integer.toString(Wrongnum));

            }
        });
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
JamesBoy90
  • 43
  • 1
  • 5
  • Can't you just call Random.int() again, until it doesn't match the current viewFlipper's view index? – azgolfer Jul 11 '12 at 22:35

1 Answers1

0

You can do this in your code:

 Random RandomView = new Random();
 int nextViewIndex = RandomView.nextInt(3);
 while (nextViewIndex == MyViewFlipper.getDisplayedChild()) {
     nextViewIndex = RandomView.nextInt(3);
 }
 MyViewFlipper.setDisplayedChild(nextViewIndex);

Basically just call Random.nextInt() until it doesn't match current viewFlipper's index.

To only show a viewFlipper once randomly:

// Intialize this once
List<Integer> vfIndices = new ArrayList<Integer>();
for (int i = 0; i < MyViewFlipper.getChildCount(); i++) {
   vfIndices.add(i);
}

// when button is clicked
if (vfIndices.size() == 0) {
   return; // no more view flipper to show!
}
int viewIndex = RandomView.nextInt(vfIndices.size());
MyViewFlipper.setDisplayedChild(vfIndices.get(viewIndex));
vfIndicies.remove(viewIndex);

The idea is use an ArrayList to keep track of the viewFlipper index that has not been shown. When button is clicked, pick an index randomly from this array and set the viewFlipper to. Then remove that index from the ArrayList. Next time the button is clicked, it will show one of the remaining flippers.

azgolfer
  • 15,087
  • 4
  • 49
  • 46
  • thx for answer. i dont exatly understand what you mean by just call Random.... until it dosen't match. 1_view (Button): `int nextViewIndex = RandomView.nextInt(3);` 2_view (Button): `int nextViewIndex = RandomView.nextInt(2?);` – JamesBoy90 Jul 11 '12 at 23:41
  • You said when Random.nextInt(3) is called, it may be the index of the current viewFlipper right? e.g. current index is 0, the random result is 0. Then you can just call Random.nextInt(3) again, until it returns 1 or 2, which is the index your viewFlipper will switch to. – azgolfer Jul 12 '12 at 00:24
  • Thx it worked! but i have another problem now. when i get to (2_view or 3_view) and click on the button sometimes i get back to (1_view). how can i fix that? – JamesBoy90 Jul 12 '12 at 00:52