0

I make a filter with java for chose a specific continent and when you click refresh you get a random filtered data this is my code with java

    public class Util {

    public static CountriesData getCountrie(){

        List<Country> mList = new ArrayList<>();
        mList.add(new Country(R.drawable.ae, "Emarat","asia"));
        mList.add(new Country(R.drawable.tm, "Turkmenistan","asia"));
        mList.add(new Country(R.drawable.bj, "Benin","Afriqua"));
        mList.add(new Country(R.drawable.va, "Seal of Virginia ","N.America"));
        mList.add(new Country(R.drawable.us, "USA","N.America"));
        mList.add(new Country(R.drawable.at, "Austria","Europe"));
        mList.add(new Country(R.drawable.pt, "Portugal","Europe"));
        mList.add(new Country(R.drawable.ki, "Kiribati ","S.America"));
        mList.add(new Country(R.drawable.cu, "Cuba","S.America"));
        mList.add(new Country(R.drawable.ht, "Haiti","S.America"));

        return new CountriesData(mList);

    }
}

my question is how make a code for my button to get data when i click

   mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showRandomCountry();
        }
    });

    }

public void showRandomCountry(){
    schuffleAsia();
   // int r = new Random().nextInt(mList.size());

   mImageView.setImageResource(mList.get().getMcountry());
 //  mTextView.setText(mList.get(m).getmFact());

`

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • https://developer.android.com/reference/android/widget/Button – Ryan Leach Jun 04 '18 at 01:43
  • Possible duplicate of [Android: how to handle button click](https://stackoverflow.com/questions/14782901/android-how-to-handle-button-click) – Ryan Leach Jun 04 '18 at 01:44
  • Take a look at the `Random` class and how to use `nextInt`. Also consider that an `ArrayList` has a `size()` method and that the index is Zero base. That should help you get your solution. Good Luck! – Barns Jun 04 '18 at 01:49
  • 1
    What is wrong with `mList.get(new Random().nextInt(mList.size()))`? – OneCricketeer Jun 04 '18 at 03:28
  • Possible duplicate of [Retrieving a random item from ArrayList](https://stackoverflow.com/questions/5034370/retrieving-a-random-item-from-arraylist) – ישו אוהב אותך Jun 04 '18 at 05:01

1 Answers1

0

Place this function in your Util class:-

 public Country getRandomCountry() {  
     return mList.get(new Random().nextInt(list.size()));//To get random item from the array.
 }

And For Getting Shuffled Array Use This function:-

 public ArrayList<Country> getShuffledList(int sizeOfArray) {
     Util util = new Util();
     List<Country> shuffledList = new ArrayList<>();

     for (int i = 0; i < sizeOfArray; i++) {
         shuffledList.add(util.getRandomCountry());
     }
     return shuffledList; 
 }
alexrnov
  • 2,346
  • 3
  • 18
  • 34
Shashank Mishra
  • 542
  • 4
  • 12