<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.jatin.foreignlanguagefinal.French.FrenchFacts">
<ImageView
android:id="@+id/imageFact"
android:layout_width="match_parent"
android:layout_height="300dp"
android:src="@drawable/booksf" />
<TextView
android:id="@+id/textFact"
android:layout_width="match_parent"
android:layout_height="220dp"
android:layout_below="@+id/imageFact"
android:gravity="center"
android:maxLines="200"
android:scrollbars="vertical"
android:text="France is the world's most popular tourist destination – some 83.7 million visitors arrived in France, according to the World Tourism Organization report published in 2014, making it the world's most-visited country."
android:textColor="@color/colorPrimary"
android:textSize="20dp" />
<Button
android:id="@+id/nextFact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textFact"
android:layout_centerInParent="true"
android:layout_margin="3dp"
android:text="Next" />
</RelativeLayout>
public class FrenchFacts extends AppCompatActivity {
ArrayList<String> facts;
ImageView imageFacts;
TextView textFacts;
Button nextFact;
Map<String,Integer> mapFacts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_french_facts);
imageFacts = (ImageView) findViewById(R.id.imageFact);
textFacts = (TextView) findViewById(R.id.textFact);
nextFact = (Button) findViewById(R.id.nextFact)
mapFacts = new HashMap<>();
mapFacts.put("France is the world's most popular tourist destination – some 83.7 million visitors arrived in France, according to the World Tourism Organization report published in 2014, making it the world's most-visited country.",R.drawable.frances1);
mapFacts.put("France is the largest country in the EU, and known as 'the hexagon' – with an area of 551,000 sq km it's almost a fifth of the EU’s total area, and due to its six-sided shape France is sometimes referred to as l’hexagone. About a quarter is covered by forest; only Sweden and Finland have more.",R.drawable.frances2 );
mapFacts.put("Louis XIX was the king of France for just 20 minutes, the shortest ever reign – he ascended to the French throne in July 1830 after his father Charles X abdicated, and abdicated himself 20 minutes later in favour of his nephew, the Duke of Bordeaux.",R.drawable.frances3);
mapFacts.put("Liberté, égalitié, fraternité meaning ‘liberty, equality and fraternity’ (or brotherhood) is the national motto of France – it first appeared around the time of the Revolution (1789–1799), and was written into the constitutions of 1946 and 1958. Today you’ll see it on coins, postage stamps and government logos often alongside ‘Marianne’ who symbolises the ‘triumph of the Republic’.",R.drawable.frances4);
mapFacts.put("The French Army was the first to use camouflage in 1915 (World War I) – the word camouflage came from the French verb ‘to make up for the stage’. Guns and vehicles were painted by artists called camofleurs.",R.drawable.frances5);
mapFacts.put("In France you can marry a dead person – under French law, in exceptional cases you can marry posthumously, as long as you can also prove that the deceased had the intention of marrying while alive and you receive permission from the French president.",R.drawable.frances6);
mapFacts.put("France was the first country in the world to ban supermarkets from throwing away or destroying unsold food – since February 2016, shops must donate wastage to food banks or charities.",R.drawable.frances7);
mapFacts.put("The Académie Française has aimed to preserve the French language since 1634 – by attempting to ban, somewhat unsuccessfully, foreign words such as blog, hashtag, parking, email and weekend. It was started by a small group of French intellects and officially recognised by Louis XIII in 1635.",R.drawable.frances8);
mapFacts.put("At least 40 percent of the music on private radio stations must be of French origin – since 1996, the country’s top media regulator the Conseil Supérieur de L’Audiovisuel (CSA) has been charged with enforcing this French law. The CSA also requires half of the French music quota to be less than six months old.",R.drawable.frances9);
mapFacts.put("France legalised same-sex marriage in 2013 – when President Françoise Holland signed the bill into law on 18 May 2013, France became the ninth country in Europe and 14th in the world to legalise same-sex marriage.",R.drawable.frances10);
nextFact.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Random random = new Random();
int color = Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256));
//String holder = facts.get(random.nextInt(facts.size()));
textFacts.setMovementMethod(new ScrollingMovementMethod());
for (Map.Entry<String,Integer> entry : mapFacts.entrySet()) {
String text = entry.getKey();
textFacts.setText(text);
textFacts.setTextColor(color);
int image = entry.getValue();
Glide.with(getApplicationContext())
.load(image)
.into(imageFacts);
}
}
});
У меня есть просмотр изображений, просмотр текста и кнопка.
При нажатии кнопки должны отображаться различные изображения и текст.
Я попытался использовать hashmap, но во время итерации нажатие кнопки работает только один раз. После этого неважно, какое изображение и текст не меняются. Поэтому, пожалуйста, скажите мне, каким образом я мог бы отображать текст и изображение.
PS: я много искал в сети и не смог найти ответ. Пожалуйста, прокомментируйте решение и код подробно. Любая помощь приветствуется.