2012-11-04 1 views
0

메신저 완전히 새로운 자바와 안드로이드 dev에 그래서 잘하면 내가 뭘하려고 노력하고 설명 할 수 있습니다 (따라서 나는 내 코드에 너무 많은 의견을 가지고 시도하고 도움이 될 수 있습니다. 나와 함께).최종 변수가 아니거나 전역 변수입니까? 나는 1을 추가하고 다시 루프를 추가하고 싶지만 어떻게

ibnext 버튼을 클릭하면 그림 (및 연관된 txt 파일)이 다음 그림으로 변경되기를 원하기 때문에 자산 폴더에 그림 집합이 있습니다. 간단하게 유지하려면 txt 파일과 사진 이름 1.TXT & 1.JPG, 2.txt 있습니다 & 2.JPG 등

다음은 내 코드와 첫 번째 이미지 및 TXT를 표시하기위한 모든 작동하지만 "progressL1"에 1을 더한 다음 이미지와 txt를 다시 그릴 수는 없습니다. 나는 나에게 많은 의미를 가지지 않는 최종 변수와 전역 변수를 검색하는 루프에 갇혀있다.

도움말을 잘하십시오, 미안 그 같은 초보자 질문을하지만,이와 손실에 임 ...받은

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 ============================== ============

내가 원하는 것에 더 가깝다고 생각하지만, progressL1이 ++ 이후 public loadDataFromAsset 섹션을 호출하는 방법을 아직도 모른다.

이 코드가 더 좋다고 생각하십니까? 누구를 생각해?

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; 
      } 







     } 
+1

"나는 루프에 갇혀 있어요 나에게 많은 의미가 나던 비 최종 전역 변수, 검색"그렇지 난 두려워를 나에게도 많은 의미가 있습니다;) 이것을 다시 말하십시오. 당신의 문제는 정확히 무엇입니까? – Simon

+0

당신이 가진 문제를 정말로 이해할 수 없었습니다. 더 잘 설명 할 수 있습니까? –

+0

'Lessonsinglegroup.this.loadDataFromAsset (progressL1); 사용 –

답변

0

귀하의 질문은 아직 명확하지 않습니다. 나는 "onClick 내에서 loadDataFromAsset을 어떻게 호출 할 수 있습니까?"라고 생각합니다.

귀하의 onClick은 "익명의 내부 클래스"입니다. 외부 클래스의 메소드를 호출하려면, Lessonsinglegroup이를 사용

Lessonsinglegroup.this.loadDataFromAsset() 
+0

감사합니다. 제 질문이 일주일 내내 일어나는 일을 포기하고 있었기 때문에 제 질문이 분명하지 않았던 것 같습니다. – andy

관련 문제