Bahamit
Bahamit

Reputation: 103

new intent after click callback

I have a listview and when it clicks on a certain option I want it to create a new intent. Like a menu item. This is my first time working with list views, so I'm probably just missing the right way to do this, but is there an easy fix?

private void registerClickCallback() {
    ListView list = (ListView) findViewById(R.id.mainlist);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {
        TextView textView = (TextView) viewClicked;
        Toast.makeText(main.this, position, Toast.LENGTH_SHORT).show();
        if(textView.getText().toString() == "my Profile"){
            Intent intent = new Intent(this.getApplicationContext(), myProfile.class);
            startActivity(intent);
        }
    }
});

cannot resolve method 'getapplicationcontext'

Upvotes: 1

Views: 115

Answers (2)

fredmaggiowski
fredmaggiowski

Reputation: 2248

You can't use this.getApplicationContext() since you're inside a new AdapterView.OnItemClickListener() and there,this doesn't have that method.

You should create a class field containing the application context and use it or use viewClicked.getContext() as suggested by @codeMagic.

Also, as you might have been read in the comments, you should use .equals for string comparison. The == operator is not enough.

Upvotes: 2

Shadab Ansari
Shadab Ansari

Reputation: 7070

Try -

Intent intent = new Intent(MainActivity.this, myProfile.class);
startActivity(intent);

Where MainActivity is he activity where you implemented onItemClick(). You should replace it by your activity name.

Upvotes: 1

Related Questions