提问人:LuckyStrike 提问时间:11/16/2023 最后编辑:MadProgrammerLuckyStrike 更新时间:11/16/2023 访问量:18
在 android Studio 中显示随机数据
Displaying randomized data in android Studio
问:
我正在尝试创建一个应用程序,其中数据从网站中提取,然后显示给用户进行交互。我已经成功地从网站上提取了数据,但我仍然是 android studio 的新手,并且在显示我想要的内容时遇到了问题。如果有人知道我做错了什么,可以提供帮助,那就太棒了。我正在使用 java。
package com.example.boardswipe;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import org.w3c.dom.Text;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
FirebaseAuth auth;
Button button; //logout button
Button save_tab_btn;
FirebaseUser user;
@Override
protected void onCreate(Bundle savedInstanceState) { //All of the Activity.java files come with this, just ignore it
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
auth = FirebaseAuth.getInstance();
button = findViewById(R.id.logout);
user = auth.getCurrentUser(); //get the user
if(user == null){
Intent intent = new Intent(getApplicationContext(), Login.class); //logout and go to login page if null
startActivity(intent); //start login activity page
finish(); //finish current activity
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FirebaseAuth.getInstance().signOut(); //sign out user from firebase
Intent intent = new Intent(getApplicationContext(), Login.class); //logout and go to login page
startActivity(intent); //start login activity page
finish(); //finish current activity
}
});
//code for if the save tab button is clicked --> go to that page
save_tab_btn = findViewById(R.id.Saves_Tab);
save_tab_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent savebtn = new Intent(getApplicationContext(), SavesTab.class);
startActivity(savebtn);
finish();
}
});
// Create and add profile views to the container
int hundredThousand = (int) (Math.random() * 10);
int countHThou = 0;
int tenThousand = (int) (Math.random() * 10);
int countTenThou = 0;
int thousand = (int) (Math.random() * 10);
int countThou = 0;
int hundred = (int) (Math.random() * 10);
int countHun = 0;
int ten = (int) (Math.random() * 10);
int countTen = 0;
int one = (int) (Math.random() * 10);
int countOne = 0;
ArrayList<Profile> profiles = new ArrayList<>();
while (countHThou != 11) {
while (countTenThou != 11) {
while (countThou != 11) {
while (countHun != 11) {
while (countTen != 11) {
while (countOne != 11) {
try{
Profile profile = new Profile(one, ten, hundred, thousand, tenThousand, hundredThousand);
profiles.add(profile);
} catch (Exception e) {
System.console();
}
one++;
if (one == 10) {
one = 0;
}
countOne++;
}
countTen++;
ten++;
if (ten == 10) {
ten = 0;
}
countOne = 0;
one = (int) (Math.random() * 10);
}
countHun++;
hundred++;
if (hundred == 10) {
hundred = 0;
}
countTen = 0;
ten = (int) (Math.random() * 10);
}
countThou++;
thousand++;
if (thousand == 10) {
thousand = 0;
}
countHun = 0;
hundred = (int) (Math.random() * 10);
}
countTenThou++;
tenThousand++;
if (tenThousand == 10) {
tenThousand = 0;
}
countThou = 0;
thousand = (int) (Math.random() * 10);
}
countHThou++;
hundredThousand++;
if (hundredThousand == 10) {
hundredThousand = 0;
}
countTenThou = 0;
tenThousand = (int) (Math.random() * 10);
}
ConstraintLayout layout = findViewById(R.id.layout);
TextView description = findViewById(R.id.textView);
ImageView gameImage = findViewById(R.id.imageView);
TextView players = findViewById(R.id.textView3);
TextView playTime = findViewById(R.id.textView4);
for(Profile profile : profiles){
gameImage.setImageBitmap(getBitmapFromURL(profile.getImageURL()));
description.setText(profile.getDescriptionText());
String minMaxPlayers = "Players: " + profile.getMinPlayersValue() + " - " + profile.getMaxPlayersValue();
players.setText(minMaxPlayers);
String minMaxPlayTime = "Time: " + profile.getMinPlaytimeValue() + " - " + profile.getMaxPlaytimeValue();
playTime.setText(minMaxPlayTime);
layout.addView(gameImage);
layout.addView(description);
layout.addView(players);
layout.addView(playTime);
}
}
@Nullable
public static Bitmap getBitmapFromURL(String src) {
try {
Log.e("src",src);
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
Log.e("Bitmap","returned");
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
Log.e("Exception",e.getMessage());
return null;
}
}
}
下面是 xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/layout"
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"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="681dp"
android:text="@string/boardswipe"
android:textColor="#1C37D1"
android:textSize="25sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/logout"
android:layout_width="75dp"
android:layout_height="62dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="4dp"
android:backgroundTint="#FA4810"
android:text="@string/logout"
android:textSize="8sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/Saves_Tab"
android:layout_width="160dp"
android:layout_height="50dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
android:backgroundTint="#5FDDD2"
android:text="Saves"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/BG_Tab"
android:layout_width="160dp"
android:layout_height="50dp"
android:layout_marginStart="16dp"
android:layout_marginBottom="8dp"
android:backgroundTint="#E91E63"
android:text="BoardGames"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<!-- This is a comment, everything below is hardcoded and not from database (TEMPORARY) -->
<ImageView
android:id="@+id/imageView"
android:layout_width="289dp"
android:layout_height="210dp"
android:layout_marginTop="40dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2"
app:srcCompat="@drawable/arnak" />
<TextView
android:id="@+id/textView"
android:layout_width="305dp"
android:layout_height="289dp"
android:text=""
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.44"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView"
app:layout_constraintVertical_bias="0.447" />
<TextView
android:id="@+id/textView3"
android:layout_width="127dp"
android:layout_height="34dp"
android:layout_marginStart="54dp"
android:layout_marginTop="314dp"
android:layout_marginEnd="230dp"
android:layout_marginBottom="383dp"
android:text=""
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView4"
android:layout_width="128dp"
android:layout_height="33dp"
android:layout_marginStart="208dp"
android:layout_marginTop="315dp"
android:layout_marginEnd="75dp"
android:layout_marginBottom="383dp"
android:text=""
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
我试过弄乱布局的东西,弄乱xml文件和各种不同的布局。这些都没有奏效,也没有显示我想要的东西。
答: 暂无答案
评论