2014-12-04 60 views
2

在實現隨機瑣事生成器的分鐘上處理Android應用程序。我有隨機生成的事實使用一個按鈕來加載數組中保存的另一個字符串。將圖像添加到隨機事實生成器(Android)

但是,我想要一個特定的圖像與使用ImageView每個報價出現,但不太確定如何編碼它。我是否需要創建一個對象數組,然後使用生成器調用每個對象,或者使用另一個維度將圖像(R.drawable ...)添加到數組中?

public class Trivia extends Activity 
{ 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.trivia); 

    Button button = (Button) findViewById(R.id.button); 
    TextView textView = (TextView) findViewById(R.id.textView); 

    Random generator = new Random(); 
    int i; 

    String[] facts; 
    facts = new String [10]; 

    facts[0]= "At 5'3 Muggsy Bogues is the smallest player to ever play in the NBA."; 
    facts[1]= "Tim Duncan was training to become a member of the 1992 U.S. Men’s Olympic Swim Team until Hurricane Hugo destroyed the only pool he could train in. His mortal fear of sharks kept him from using the ocean temporarily. So to keep in shape, he began playing basketball."; 
    facts[2]= "When Wilt Chamberlain became the first NBA player to earn $100,000 in salary in 1965, his longtime rival Bill Russell demanded that his own salary be raised to $100,001. His salary was immediately raised."; 
    facts[3]= "Kobe Bryant’s parents had to cosign his first NBA contract because he was only 17 when he was drafted."; 
    facts[4]= "Latrell Sprewell (famous for choking his coach) turned down a $21 million contract offer claiming it wasn’t enough to feed his family. He never played again and went bankrupt."; 
    facts[5]= "Shaquille O’Neal challenged Hakeem Olajuwon to one on one after losing the 1995 NBA Finals with a typewritten, signed and hand-delivered note."; 
    facts[6]= "The guy featured in the NBA logo is former Laker Jerry West."; 
    facts[7]= "Paul Pierce was stabbed 11 times in the face, back, and neck and still played all 82 games of the 2000-2001 NBA Season."; 
    facts[8]= "Within five years of retirement, an estimated 60% of former NBA players are financially broke."; 
    facts[9]= "Air Jordan’s were banned upon introduction by the NBA. However, Jordan continued to wear them anyways, as Nike was willing to pay the fine each game."; 

    i = generator.nextInt(10); 
    textView.setText(facts[i]); 

    button.setOnClickListener(new View.OnClickListener() 
    { 
    @Override 
    public void onClick(View v) 
    { 
     Intent intent = new Intent(Trivia.this, Trivia.class); 
     startActivity(intent); 
    } 
    }); 
} 
}  

回答

0

很簡單:

public class Trivia extends Activity { 

      @Override 
      public void onCreate (Bundle savedInstanceState) { 

       super.onCreate (savedInstanceState); 
       setContentView (R.layout.sample); 

       Button button = (Button) findViewById (R.id.button); 
       TextView textView = (TextView) findViewById (R.id.textView); 

       Random generator = new Random(); 
       int i; 

       ArrayList<SampleModel> list = new ArrayList<MainActivity.Trivia.SampleModel>(); 
       list.add (new SampleModel ("At 5'3 Muggsy Bogues is the smallest player to ever play in the NBA.", R.drawable.ic_launcher)); 
list.add (new SampleModel ("At 5'3 Muggsy Bogues is the smallest player to ever play in the NBA.", R.drawable.ic_launcher)); 
list.add (new SampleModel ("At 5'3 Muggsy Bogues is the smallest player to ever play in the NBA.", R.drawable.ic_launcher)); 
list.add (new SampleModel ("At 5'3 Muggsy Bogues is the smallest player to ever play in the NBA.", R.drawable.ic_launcher)); 

       i = generator.nextInt (10); 
       textView.setText (list.get (i).getQuote()); 

       button.setOnClickListener (new View.OnClickListener() { 
        @Override 
        public void onClick (View v) { 

         Intent intent = new Intent (Trivia.this, Trivia.class); 
         startActivity (intent); 
        } 
       }); 
      } 

      public class SampleModel { 

       String quote; 
       int drawable; 



       public SampleModel (String quote, int drawable) { 

        super(); 
        this.quote = quote; 
        this.drawable = drawable; 
       } 

       public String getQuote() { 

        return quote; 
       } 

       public void setQuote (String quote) { 

        this.quote = quote; 
       } 

       public int getDrawable() { 

        return drawable; 
       } 

       public void setDrawable (int drawable) { 

        this.drawable = drawable; 
       } 

      } 
     } 
+0

這看起來不錯,但沒有與該行的錯誤:textView.setText(list.get(I)); – Jon 2014-12-05 12:50:27

+0

它不能解決的方法 – Jon 2014-12-05 12:51:20

+0

檢查更新.. – 2014-12-05 12:52:45

0

使用可繪製資源ID表並隨機選擇其中一個ID。然後使用setImageResource方法將適當的drawable加載到ImageView。如果每條消息都嚴格與特定的可繪製連接使用一個Map。