2011-10-18 18 views
0

次のクラスは、xmlファイルに含まれる一連の文字列を表示するためのクラスです。 onCreateメソッド内では、文字列の配列がリソースファイルから取得されます。これらの文字列は、文字列から構築されたJokeオブジェクト(m_arrJokeList)のArrayListに追加される一式の冗談です。Androidスクロールビューで文字列が表示されない

addJokeメソッドは、onCreateメソッド内で呼び出され、これらのジョークをテキストとしてスクロールビューに表示するためのものです。しかし、これは私のデバイスやエミュレータでは動作しないようですので、コードには間違いがあります。これらのビューをどのように操作するかに関するヒントについても、これを修正する方法を知りたいと思います。

ここにコードは完全に実装されていませんです。

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

put Joke tempJoke = new Joke(); forループfor(int i = 0; i user370305

+0

@ user370305それは実際にはそれを修正した、コメントの代わりに答えをしている必要があります。 – user1001619

+0

私はそれに答えるために正しいとマークしてください、それはあなたと他のユーザーのためにも役立ちます。 – user370305

答えて

0

はそれを試してみてくださいforループ

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

Joke tempJoke = new Joke(); 

を置きます。何が起こるか教えてください。

+0

何が起こったのですか。一度にすべてを修正しました。ありがとう。 – user1001619

+0

を意味しますか?私はあなたを得ない。 – user370305

+0

あなたのコードでは、ジョークのオブジェクトを一度だけ初期化していますが、forループでは同じオブジェクトを使用しているため、値全体がオーバーライドされ、最後の値またはnullが返されます。したがって、forループ内の新しいオブジェクトを初期化するたびに、jokeList内の異なるオブジェクトを追加します。あなたは全体のリストにアクセスすることができます。 – user370305

関連する問題