2011-10-18 13 views
0

以下類旨在顯示xml文件中包含的一組字符串。在onCreate方法中,從資源文件中提取一個字符串數組。這些字符串是一組老舊的笑話,添加到由字符串構造的Joke對象(m_arrJokeList)的ArrayList中。Android滾動視圖不顯示字符串

在onCreate方法中調用的addJoke方法旨在將滾動視圖中的這些笑話顯示爲文本。然而,這似乎都不能在我的設備或模擬器上工作,所以代碼肯定有問題。我想知道如何解決這個問題以及關於如何使用這些視圖的一些提示。

以下是代碼,未完全實施

package edu.calpoly.android.lab2; 
import java.util.ArrayList; 
    import android.app.Activity; 
    import android.content.Context; 
    import android.content.res.Resources; 
    import android.os.Bundle; 
    import android.widget.Button; 
    import android.widget.EditText; 
    import android.widget.LinearLayout; 
    import android.widget.ScrollView; 
    import android.widget.TextView; 
    import android.widget.Toast; 

    public class SimpleJokeList extends Activity { 

     // Contains the list Jokes the Activity will present to the user 
     protected ArrayList<Joke> m_arrJokeList; 

     // LinearLayout used for maintaining a list of Views that each display Jokes 
     protected LinearLayout m_vwJokeLayout; 

     // EditText used for entering text for a new Joke to be added to m_arrJokeList. 
     protected EditText m_vwJokeEditText; 

     // Button used for creating and adding a new Joke to m_arrJokeList using the 
     // text entered in m_vwJokeEditText. 
     protected Button m_vwJokeButton; 

     // Background Color values used for alternating between light and dark rows 
     // of Jokes. 
     protected int m_nDarkColor; 
     protected int m_nLightColor; 

     @Override 
     public void onCreate(Bundle savedInstance) { 
      super.onCreate(savedInstance); 
      initLayout(); 
      Resources localRsrc; 
      localRsrc = this.getResources(); 
      ArrayList<Joke> jokeList = new ArrayList<Joke>(); 
      String[] jokeStrings = localRsrc.getStringArray(R.array.jokeList); 

      int size = jokeStrings.length; 
      Joke tempJoke = new Joke(); 
      for(int i=0;i < size;i++) 
      { 
       tempJoke.setJoke(jokeStrings[i]); 
       jokeList.add(tempJoke); 
       addJoke(tempJoke); 
      } 
     } 


     // Method used to encapsulate the code that initializes and sets the Layout 
     // for this Activity. 
     protected void initLayout() { 
      // TODO 
      //LinearLayout rootLayout; 
      m_arrJokeList = new ArrayList<Joke>(); 
      m_vwJokeLayout = new LinearLayout(this); // why pass "this" 
      m_vwJokeLayout.setOrientation(LinearLayout.VERTICAL); 

      ScrollView extendedView = new ScrollView(this.getApplicationContext()); 
      extendedView.addView(m_vwJokeLayout); 
      setContentView(extendedView); 

     } 

     // Method used to encapsulate the code that initializes and sets the Event 
     // Listeners which will respond to requests to "Add" a new Joke to the list. 
     protected void initAddJokeListeners() { 
      // TODO 
     } 

     // Method used for encapsulating the logic necessary to properly initialize 
     // a new joke, add it to m_arrJokeList, and display it on screen. 
     // @param strJoke 
     //   A string containing the text of the Joke to add. 
     protected void addJoke(Joke jk) { 
      m_arrJokeList.add(jk); 
      TextView textJoke = new TextView(this); 
      textJoke.setText(jk.getJoke()); 
      m_vwJokeLayout.addView(textJoke); 
     } 
    } 
+0

把笑話tempJoke = new笑話(); in for循環for(int i = 0; i user370305

+0

@ user370305這實際上修復了它,應該已經做出了答案而不是評論。 – user1001619

+0

我回答了,請標記爲正確,以便它可以幫助您,也可以幫助其他用戶。 – user370305

回答

0

Joke tempJoke = new Joke(); 

在for循環

for(int i=0;i < size;i++) 
{ 
    Joke tempJoke = new Joke(); 
    tempJoke.setJoke(jokeStrings[i]); 
    jokeList.add(tempJoke); 
    addJoke(tempJoke); 
} 

那就試試吧。並讓我知道發生了什麼。

+0

發生了什麼事情,它將所有內容都固定在一個地方。謝謝。 – user1001619

+0

是什麼意思?我不明白你的意思。 – user370305

+0

在你的代碼中,你只是初始化了Joke的對象一次,而在for循環中你使用的是同一個對象,所以它覆蓋了整個值,你只需要得到最後一個值或空值。所以每次使用for循環新對象時,都要在jokeList中添加不同的對象。你可以訪問整個列表。 – user370305