Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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);
    }