non-final variable or global? I want to add 1 then loop back but how


Ask by : andy November 04, 2012 12:09

im totally new to java and android dev so hopefully i can describe what i am trying to achieve well enough (hence why i have so many comments in my code as well, try and help me along).

I have a set of pictures in the assets folder that I want to display, as the ibnext button is clicked i want the picture (and associated txt file) to change to the next one. To keep things simple the txt file and picture are names 1.txt & 1.jpg, 2.txt & 2.jpg etc.

The following is my code and it all works for displaying the first image and txt, but I cannot get to a point that I can add 1 to "progressL1" and then redraw the image and txt. I am stuck in a loop searching non final and global variables, which doesnt make much sense to me.

Help well received please, sorry its such a newbie question, but im at a loss with this...

public class Lessonsinglegroup extends Activity implements OnInitListener {


//define the image and text view for use later
ImageView Image;
TextView Text;

//define the texttospeak stuff

private int MY_DATA_CHECK_CODE = 0;
public int progressL1 = 0;
private TextView inputText;
private TextToSpeech tts;
private ImageButton speakButton;
private ImageButton nextButton;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_lessonsinglegroup);
    getActionBar().setDisplayHomeAsUpEnabled(true);

    //link the image and text boxes to the xml
    Image = (ImageView)findViewById(R.id.image);
    Text = (TextView)findViewById(R.id.text);
    loadDataFromAsset(progressL1);

    //finish with the asset load

    //define tts stuff
    inputText = (TextView) findViewById(R.id.text);
    speakButton = (ImageButton) findViewById(R.id.ibtalk);
    nextButton = (ImageButton) findViewById(R.id.ibnext);


    //start the speech stuff
    speakButton.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        String text = inputText.getText().toString();
        if (text!=null && text.length()>0) {
            tts.speak(text, TextToSpeech.QUEUE_ADD, null);
        }
    }
    });

    Intent checkIntent = new Intent();
        checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);


        }
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
    if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
        //sucess with TTS create it
        tts = new TextToSpeech(this, this);
        }
    else {
        //missing TTS so install it
        Intent installIntent = new Intent();
        installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
        startActivity(installIntent);
    }
}


}

public void onInit(int status) {
    if (status == TextToSpeech.SUCCESS) {
        Toast.makeText(Lessonsinglegroup.this, "Text to Speech initialised", Toast.LENGTH_LONG).show();
            }
    else if (status == TextToSpeech.ERROR) {
        Toast.makeText(Lessonsinglegroup.this, "Error starting Text to Speech",     Toast.LENGTH_LONG).show();
    }
}

//actually load the text file and image file   
public void loadDataFromAsset(int progressL1) {
    //load the asset files themselves
    try {
        InputStream is = getAssets().open(progressL1 + ".txt");
        //check file size
        int size = is.available();
        //create a buffer to handle it
        byte[] buffer = new byte[size];
        //send the data to the buffer
        is.read(buffer);
        //close the stream down
        is.close();
        //set the text we recovered to the TextView
        Text.setText(new String(buffer));
    }
    catch (IOException ex) {
        return;
    }

    //image file next
    try {
        InputStream ims = getAssets().open(progressL1 + ".jpg");
        //load the image as drawable
        Drawable d = Drawable.createFromStream(ims,  null);
        //set the drawable image to the imageview
        Image.setImageDrawable(d);
    }
    catch (IOException ex) {
        return;
            }





//end of image and text file loading.

//when next is clicked start doing this
nextButton.setOnClickListener(new View.OnClickListener() {


      public void onClick(View v) {
          try {
              progressL1++;
              Lessonsinglegroup.loadDataFromAsset();
          }
          catch (IOException ex) {
                return;
                    }
      }   
          });
      }

====================================================================================================================================EDIT==============================================

With some tidier code i think i am closer to what i want, but I still dont know how to call the public loadDataFromAsset section after the progressL1 has been ++.

I think this code is better? Thoughts anyone?

public class Lessonsinglegroup extends Activity implements OnClickListener, OnInitListener {
//old version did not have OnClcikListener in it here-------------->

//define the image and text view for use later
ImageView Image;
TextView Text;

//define the texttospeak stuff

private int MY_DATA_CHECK_CODE = 0;
public int progressL1 = 0;
private TextView inputText;
private TextToSpeech tts;
private ImageButton speakButton;
private ImageButton nextButton;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_lessonsinglegroup);
    getActionBar().setDisplayHomeAsUpEnabled(true);

    //link the image and text boxes to the xml
    Image = (ImageView)findViewById(R.id.image);
    Text = (TextView)findViewById(R.id.text);
    loadDataFromAsset(progressL1);

    //finish with the asset load

    //define tts stuff
    inputText = (TextView) findViewById(R.id.text);
    speakButton = (ImageButton) findViewById(R.id.ibtalk);
    nextButton = (ImageButton) findViewById(R.id.ibnext);


    //start the speech stuff
//    speakButton.setOnClickListener(new View.OnClickListener() {

    //new onclick listener style

    speakButton.setOnClickListener(this);
    nextButton.setOnClickListener(this);

}
    public void onClick(View v) {
        if (v==nextButton) {
            progressL1++;
            Toast.makeText(Lessonsinglegroup.this, "progress +1", Toast.LENGTH_LONG).show();
        //HERE IS WHERE I AM CONFUSED HOW TO CALL FORWARD-------<<<<<<  

        }

        if (v==speakButton){

        String text = inputText.getText().toString();
        if (text!=null && text.length()>0) {
            tts.speak(text, TextToSpeech.QUEUE_ADD, null);
        }

    };

    Intent checkIntent = new Intent();
        checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);


        }
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
    if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
        //sucess with TTS create it
        tts = new TextToSpeech(this, this);
        }
    else {
        //missing TTS so install it
        Intent installIntent = new Intent();
           installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
        startActivity(installIntent);
    }
}


}

public void onInit(int status) {
    if (status == TextToSpeech.SUCCESS) {
        Toast.makeText(Lessonsinglegroup.this, "Text to Speech initialised", Toast.LENGTH_LONG).show();
            }
    else if (status == TextToSpeech.ERROR) {
        Toast.makeText(Lessonsinglegroup.this, "Error starting Text to Speech", Toast.LENGTH_LONG).show();
    }
}

//actually load the text file and image file   
public void loadDataFromAsset(int progressL1) {
    //load the asset files themselves
    try {
        InputStream is = getAssets().open(progressL1 + ".txt");
        //check file size
        int size = is.available();
        //create a buffer to handle it
        byte[] buffer = new byte[size];
        //send the data to the buffer
        is.read(buffer);
        //close the stream down
        is.close();
        //set the text we recovered to the TextView
        Text.setText(new String(buffer));
    }
    catch (IOException ex) {
        return;
    }

    //image file next
    try {
        InputStream ims = getAssets().open(progressL1 + ".jpg");
        //load the image as drawable
        Drawable d = Drawable.createFromStream(ims,  null);
        //set the drawable image to the imageview
        Image.setImageDrawable(d);
    }
    catch (IOException ex) {
        return;
            }







      }
View original question

Answer by : SimonNovember 04, 2012 12:09

Your question is still very unclear. I think you mean "how can I call loadDataFromAsset from within the onClick?"

Your onClick is an "anonymous inner class". To call a method in the outer class, Lessonsinglegroup, use this:

Lessonsinglegroup.this.loadDataFromAsset()
View original answer