2014-12-04 61 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。