将图像添加到随机事实生成器(安卓)


在实现

随机琐事生成器的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);
     }
  });
}
}    
非常简单

    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;
                }
            }
        }

使用可绘制对象资源 ID 表并随机选择其中一个 ID。然后使用 setImageResource 方法将适当的可绘制对象加载到 ImageView。如果每条消息都与特定的可绘制对象严格连接,请使用 Map。

最新更新