0

I'm a beginner at android and am working on my first app. I chose a tutorial using the google Places api and worked through it. Afterwards I decided I wanted to add a button that would choose a restaurant at random from the results. The app runs and I get no errors, testing on my galaxy tab 2. However when I click the Random button on the app, all that is displayed is the first restaurant in the list. Any help is much appreciated.

btnGetRand = (Button) findViewById(R.id.btn_get_rand);

    btnGetRand.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) { 
            Time t = new Time();
            t.setToNow();
            Random randomGenerator = new Random(t.toMillis(false));
            int count = nearPlaces.results.size();
            int r = randomGenerator.nextInt(count);
            int id = nearPlaces.results.indexOf(r);
            lv.performItemClick(lv, r, id);             
        }
    }); 

    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            String reference = ((TextView) view.findViewById(R.id.reference))
                               .getText().toString();

            Intent in = new Intent(getApplicationContext(),
                    SinglePlaceActivity.class);
            in.putExtra(KEY_REFERENCE, reference);
            startActivity(in);
        }
    });
2
  • where you getting stuff in that code Commented May 16, 2013 at 18:08
  • Try to debug and see the value of your Random generator and variables change through the flow of code. Commented May 16, 2013 at 18:10

3 Answers 3

1

Looking at this code it's impossible to say if you're random logic is wrong or your code to display is wrong.

However, Random is pretty straightforward (even if you're unnecessarily seeding it with time in milliseconds??) and looks ok.

My guess is that you're not using the API correctly.

Get in there with a debugger and print out the random index chosen on ever button click so you can verify it.

Sign up to request clarification or add additional context in comments.

4 Comments

Let me just follow up and be explicit: Ditch the random seed. The default constructor works perfectly
Thanks for the answer. I ditched the seed, checked to make sure I was using the API correctly, and then went through with the debugger like you said and it says "The source attachment does not contain the source file for Random.class" and gives me the option to change attached source. Which source should I be selecting? My attached JARs are:
My attached JARs are: android-support-v4 android google-api-client 1.10.3 google-api-client-android2 1.10.3 google-http-client 1.10.3 google-http-client-android2 1.10.3 google-oauth-client 1.10.1 gson 2.1 guava 11.0.1 jackson-core-asl 1.9.4 jsr305 1.3.9 photobuf-java 2.2.0
Glad you got it working but we would like to know what it was.
0

Do you have to trigger a click on the ListView? Why not just get the place data using nearPlaces.results.get(r) and just call startActivity() on it?

Comments

0

Try and use SecureRandom instead of the Random you are using now. No need to seed it either. Note that if your time is stuck (simulator, dead battery, whatever) then the current Random will be seeded with the same time at every call.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.