2012-11-26 71 views
0

簡單的程序:2個按鈕(上一個/下一個)和一個文本視圖來顯示文本。在此如何更新文本視圖

button1.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 
     String result = "1"; 
     Intent returnIntent = new Intent(); 
     returnIntent.putExtra("result",result); 
     setResult(RESULT_OK,returnIntent);  
     finish(); 
    } 
}); 

Main.class

String value = "1"; 
final String prog1[] = new String[16]; 
final String prog2[] = new String[105]; 
final String prog3[] = new String[66]; 

int a; 
int b; 
int c=3; 

int array1start = 0; int array1end = 15; 
int array2start = 0; int array2end = 105; 
int array3start = 0; int array3end = 65; 



@Override 
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 


if (value.equals("1")){ 

     a = array1start; 
     b = array1end; 
     prog=prog1; 

    } 
    else if (value.equals("2")){ 
     a = array2start; 
     b = array2end; 
     prog=prog2; 

textView1.setText(""+prog[a]); 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

    if (requestCode == 1) { 

     if(resultCode == RESULT_OK){ 

      String result=data.getStringExtra("result"); 
      value=result; 
      Toast toast2=Toast.makeText(this,"value: "+value,Toast.LENGTH_LONG); 
      toast2.show(); 
    } 

    if (resultCode == RESULT_CANCELED) { 
     //Write your code on no result return 

    }} 
    }//onAcrivityResult 

由意圖我創建的索引的方法(內)

private void index(){ 
    Intent i = new Intent(this, Index.class); 
    startActivityForResult(i, 1); 
} 

Index.class(的三個鍵)點,選擇索引類中的選項應將結果字符串更改爲主類中的「值」字符串

在array1或通過數組2類指數

所以,我不

private void index(){ 


    Intent i = new Intent(this, Index.class); 
    startActivityForResult(i, 1); 



} 

我的TextView取數據」瞭解更新的TextView(因爲索引值是正確的)。

感謝您的幫助

+0

我不明白你的問題。值=結果? – njzk2

+0

結果是Index.class中的一個變量(用戶通過button1或button2選擇)。如果用戶單擊button1 then result =「1」,則在主類中如果result = 1,則a = array1start; b = array1end; PROG = PROG1;但textview不會改變。 –

+0

你的onActivityResult()方法在哪裏。找不到 –

回答

1

把你的代碼到功能onActivityResult

if (value.equals("1")){ 

     a = array1start; 
     b = array1end; 
     prog=prog1; 

    } 
    else if (value.equals("2")){ 
     a = array2start; 
     b = array2end; 
     prog=prog2; 

textView1.setText(""+prog[a]); 
+0

看起來很完美!非常非常感謝!波爾 –