Domanda

I have a simple app which implement Activity.
I am implementing list-view onItemClickListener like below. However, I need to create an Intent inside this function.

How can I achieve that?

listView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView <? > arg0, View arg1, int arg2,
        long arg3) {
        // TODO Auto-generated method stub
        Toast.makeText(getBaseContext(), "Click", Toast.LENGTH_LONG).show();
        //need to create intent here to load view

        Intent myintent = new Intent(this, MYclass.class);
        // but this gives me error
        myintent.putExtra("id", "test");
        startActivity(myintent);

    }

});
È stato utile?

Soluzione

you should make some search before posting, I believe you can find number of examples well I think your problem is

Intent myintent = new  Intent(this,MYclass.class);

change it to

Intent myintent = new  Intent(YourCurrentActivityName.this,MYclass.class);

Altri suggerimenti

you are inside an anonymous inner class so this wont work here use like this :

Intent myintent = new  Intent(CurrentClassName.this,MYclass.class);

Change this line to

Intent myintent = new Intent(**YourCurrentActivity**.this,MYclass.class);

new Intent() takes context as first parameter. Inside anonymous OnItemClickListener, 'this' is not representing a context hence not working. Try using new

Intent([ThisClass].this, MYclass.class);
Use
Intent i = new  Intent(YourClassName.this,MYclass.class);
startActivity(i);

OR

Intent i = new  Intent(getApplicationContext(),MYclass.class);
startActivity(i);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top