Pergunta

i have 2 images on one page and i want to change it 3 times by using next and previous button like (1-2 images) then (3-4 images) and then (5-6 images) and then i click on previous button then it show like (3-4 images) then (1-2 images). My code is working only once when i complete it's loop then click on next or previous button then nothing happen, if you find any mistake in my code then correct me, here's my java code:

 public class AlifPics extends Activity  {  

    TextView text1;
    TextView text2;
    TextView text3;
    TextView text4;
    TextView text5;  
    TextView text6;   
    TextView text7;
    ImageView img1;
    ImageButton imgBut1;
    ImageButton imgBut2; 
    ImageButton nextBtn;        
    ImageButton preBtn;
protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.alif_pics);
  text1 = (TextView) findViewById(R.id.textView1);
  text2 = (TextView) findViewById(R.id.textView2);
  text3 = (TextView) findViewById(R.id.textView3);
  text4 = (TextView) findViewById(R.id.textView4);
  text5 = (TextView) findViewById(R.id.textView5); 
  text6 = (TextView) findViewById(R.id.textView6);  
  text7 = (TextView) findViewById(R.id.textView7);
 img1 = (ImageView) findViewById(R.id.imageView1);
 imgBut1 = (ImageButton) findViewById(R.id.imageButton1);
 imgBut2 = (ImageButton) findViewById(R.id.imageButton2);
 nextBtn = (ImageButton) findViewById(R.id.next);
 preBtn = (ImageButton) findViewById(R.id.previous);         
                  }   
      // this Menu Button method goes to MainActivity
           public void indexHuroof(View v) {
         Intent i = new Intent(this, MainActivity.class);
        startActivity(i);
          }

 int counter = 1;
  String[] textvalue1 = { "a2", "a3" };
  String[] textvalue2 = { "b2", "b3" };
  String[] textvalue3 = { "c2", "c3" };
  String[] textvalue4 = { "d2", "d3" }; 
  String[] textvalue5 = { "e2", "e3" };
  String[] textvalue6 = { "f2", "f3" };
  String[] textvalue7 = { "g2", "g3" };
      int[] myImageViewList1 = new int[] { R.drawable.img2, R.drawable.img3 }; // 123

   int[] myImageButtonList1 = new int[] { R.drawable.ibut2, R.drawable.ibut3 }; //     1,2,3

  int[] myImageButtonList2 = new int[] { R.drawable.ibut5, R.drawable.ibut6 }; // 4,5,6

  // my next ImageButton
    public void nextClick(View v) {
       try {

switch (counter) {
case 1: // first click
    text1.setText(textvalue1[0]);
    text2.setText(textvalue2[0]);
    text3.setText(textvalue3[0]);
    text4.setText(textvalue4[0]);
    text5.setText(textvalue5[0]);
    text6.setText(textvalue6[0]);
    text7.setText(textvalue7[0]);
    img1.setImageResource(myImageViewList1[0]);
    imgBut1.setImageResource(myImageButtonList1[0]);
    imgBut2.setImageResource(myImageButtonList2[0]);

    break;
case 2: // second click
    text1.setText(textvalue1[1]);
    text2.setText(textvalue2[1]);
    text3.setText(textvalue3[1]);
    text4.setText(textvalue4[1]);
    text5.setText(textvalue5[1]);
    text6.setText(textvalue6[1]);
    text7.setText(textvalue7[1]);
    img1.setImageResource(myImageViewList1[1]);
    imgBut1.setImageResource(myImageButtonList1[1]);
    imgBut2.setImageResource(myImageButtonList2[1]);
    break;
case 3: // third click
    text1.setText(textvalue1[2]);
    text2.setText(textvalue2[2]);
    text3.setText(textvalue3[2]);
    text4.setText(textvalue4[2]);
    text5.setText(textvalue5[2]);
    text6.setText(textvalue6[2]);
    text7.setText(textvalue7[2]);
    img1.setImageResource(myImageViewList1[2]);
    imgBut1.setImageResource(myImageButtonList1[2]);
    imgBut2.setImageResource(myImageButtonList2[2]);
    break;

default:
    break;
}

 } catch (Exception e) {
}
     counter++;
 }

// my previous ImageButton
       public void previousClick(View v) {
try
  {
       switch (counter) {
case 4: 
    text1.setText(textvalue1[1]);
    text2.setText(textvalue2[1]);
    text3.setText(textvalue3[1]);
    text4.setText(textvalue4[1]);
    text5.setText(textvalue5[1]);
    text6.setText(textvalue6[1]);
    text7.setText(textvalue7[1]);
    img1.setImageResource(myImageViewList1[1]);
    imgBut1.setImageResource(myImageButtonList1[1]);
    imgBut2.setImageResource(myImageButtonList2[1]);

    break;
case 3: 
    text1.setText(textvalue1[0]);
    text2.setText(textvalue2[0]);
    text3.setText(textvalue3[0]);
    text4.setText(textvalue4[0]);
    text5.setText(textvalue5[0]);
    text6.setText(textvalue6[0]);
    text7.setText(textvalue7[0]);
    img1.setImageResource(myImageViewList1[0]);
    imgBut1.setImageResource(myImageButtonList1[0]);
    imgBut2.setImageResource(myImageButtonList2[0]);
    break;
case 2: 
    text1.setText(R.string.textview1_default);
    text2.setText(R.string.textview1_default);
    text3.setText(R.string.textview1_default);
    text4.setText(R.string.textview1_default);
    text5.setText(R.string.textview1_default);
    text6.setText(R.string.textview1_default);
    text7.setText(R.string.textview1_default);
    img1.setImageResource(R.drawable.img1);
    imgBut1.setImageResource(R.drawable.ibut1);
    imgBut2.setImageResource(R.drawable.ibut2);

    break;

default:
    break;
}

}    
      catch (Exception e) {
   }
counter++;
    }
 }
Foi útil?

Solução

You are asking the same question again.Anyways, it should be

counter--;

instead of

counter++;

in the last line of previousClick(View v)

Outras dicas

First of all, catching Exception and doing nothing is a really bad practice. At least have some sort of logging.

The actual problem seems to be caused from indefinitely incrementing count variable. You should reset it or use modulo.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top