简体   繁体   English

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

[英]Adding Image to random fact generator (Android)

working on an android app at the minute that implements a random trivia generator. 在实现一个随机琐事生成器的那一刻在Android应用程序上工作。 I have the facts generating at random using a button to load another string held in an array. 我有一个事实,可以使用按钮随机生成数组中保存的另一个字符串。

However, I want a specific image to appear with each quote using ImageView but not too sure how to code it. 但是,我希望使用ImageView在每个报价中都显示特定的图像,但又不太确定如何编码。 Do I need to create an array of objects and subsequently call each object using the generator or add the images (R.drawable...) to the array using another dimension? 我需要创建一个对象数组,然后使用生成器调用每个对象,还是使用另一个维度将图像(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);
     }
  });
}
}    

Use table of drawables resource ids and randomly choose one of those ids. 使用可绘制资源ID表,并随机选择其中一个ID。 Then load appropriate drawable to ImageView using setImageResource method. 然后使用setImageResource方法将适当的drawable加载到ImageView。 If every message is strictly connected with particular drawable use a Map. 如果每条消息都与特定可绘制对象严格关联,请使用地图。

Very Simple: 很简单:

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

            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM