Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Wing Chow
Wing Chow
845 Points

Codes for Refresh button goes wrong!

So, i was able to add a button to the main list menu, but i face difficulties when i try to figure out what codes should be added but the refresh button is clicked. here is what i wrote.

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main_list, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();

        if (itemId== R.id.action_refresh) {
            Intent intent2 = new Intent(this, GetBlogPostsTask.class);
            startActivity(intent2);
        }

        return super.onOptionsItemSelected(item);
    }

However, when i press the refresh button. The app quit.

thank you very much!

Wing Chow
Wing Chow
845 Points

I have make some modification and now it does refresh but it brings up another mainlistactivity every single time when i press it.

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();

        if (itemId== R.id.action_refresh) {
            Intent refresh = new Intent(MainListActivity.this, MainListActivity.class);
            startActivity(refresh);
        }
        return super.onOptionsItemSelected(item);
    }

1 Answer

Wing Chow
Wing Chow
845 Points

OMG, it so simple! the answer has always been there! here is the solution:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();

        if (itemId== R.id.action_refresh) {
            mProgressBar.setVisibility(View.VISIBLE);
            GetBlogPostsTask r_getBlogPostsTask = new GetBlogPostsTask();
            r_getBlogPostsTask.execute();
        }

        return super.onOptionsItemSelected(item);
    }