문제

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

    }

});
도움이 되었습니까?

해결책

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

다른 팁

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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top