ichimin
ichimin

Reputation: 25

Get random object from arraylist firebase

In Firebase i need Parsing2, where 1-video, 2- release date, 3-genre, 4-publisher, 5-img, 6-img2, 7-title, 8-description, 9- developer, 10-url link, 11-14 tags, 15-price. I need to get title and img in random object (0,n)

Class describing what to get

public class Random_game_texture {
String title;
String img;

public  Random_game_texture(String img, String title){
    this.title=title;
    this.img=img;
}

public void setTitle(String title) {
    this.title = title;
}

public void setImg(String img) {
    this.img = img;
}

public String getTitle() {
    return title;
}

public String getImg() {
    return img;
}}

Random game class where need realize get data from firebase and do random method

  public class Random_game extends AppCompatActivity implements View.OnClickListener{

  Button random_btn, link_btn;
  TextView title;
  ImageView img;
  DatabaseReference myRef;
  ArrayList<Random_game_texture> gamesList;
  Random randomGenerator;


@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_random_game);

    ImageButton Home = (ImageButton) findViewById(R.id.home);
    Home.setOnClickListener(this);
    ImageButton Lupa = (ImageButton) findViewById(R.id.lupa);
    Lupa.setOnClickListener(this);
    ImageButton Calendar = (ImageButton) findViewById(R.id.calendar);
    Calendar.setOnClickListener(this);
    ImageButton Kubik = (ImageButton) findViewById(R.id.kubik);
    Kubik.setOnClickListener(this);
    ImageButton Profile = (ImageButton) findViewById(R.id.profile);
    Profile.setOnClickListener(this);

    random_btn=findViewById(R.id.random_game);
    link_btn=findViewById(R.id.link_page);

    title=findViewById(R.id.name_random);
    img=findViewById(R.id.img_random);

    myRef = FirebaseDatabase.getInstance().getReference();
    gamesList=new ArrayList<>();




    random_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


        }
    });

}


@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.home:
            startActivity(new Intent(this, News.class));
            break;
        case R.id.lupa:
            startActivity(new Intent(this, Games.class));
            break;
        case R.id.calendar:
            startActivity(new Intent(this, Calendar.class));
            break;
        case R.id.kubik:
            startActivity(new Intent(this, Random_game.class));
            break;
        case R.id.profile:
            startActivity(new Intent(this, Profile.class));
            break;
    }
}}

Upvotes: 1

Views: 91

Answers (1)

Adrixb
Adrixb

Reputation: 153

Your nodes are numbered so that makes it easier try with this code i did for you. Where my code says "max" and "min" place your first and last node. Also cant copy and paste your node names from the image. Hope it works for you, sorry if it doesn't im still a beginner.

random_btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
     readNodes();

    }
});

//Place the method out of onCreate()

private void readNodes() {

int range = ("max" - "min") + 1;     
int random (int)(Math.random() * range) + "min"; 
String node = String.valueOf(random);


    DatabaseReference ref = myRef.child("Parsing2").child(node);

        ref.addValueEventListener(new ValueEventListener() {

            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

               
                

                String title1 = dataSnapshot.child("title node name").getValue().toString();
                String img1 = dataSnapshot.child("img node name").getValue().toString();

                Random_game_texture randomObject = new Random_game_texture(img1, title1);

                title.setText(randomObject.getTitle());
                img.setText(randomObject.getImg());

            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {


            }


        });
    

}

Upvotes: 2

Related Questions