简体   繁体   中英

Android: pass listView item position to next activity

I need to pass listview clicked row' index to next activity. For now I can only open an activity by clicking listview item, but I tried to pass index multiple times, without success. My listview is by the way - with custom adapter and view holder - if that changes anything. My attempt:

mListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {

         @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Intent myIntentJob = new Intent(ListviewActivity.this, JobActivity.class);

            mListView.setTag(position);
            int positionX = (Integer) view.getTag();

            myIntentJob.putExtra("paramPosition", positionX);
            startActivity(myIntentJob);
        }
    });

And here is method onCreate from second activity:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mTextViewPassedJob = (TextView)findViewById(R.id.jobPassed);
        mTextViewPassedComp = (TextView)findViewById(R.id.compPassed);



        Bundle extras = getIntent().getExtras();
        if (extras != null)
        {
            String myParam = extras.getString("paramPosition");
        }
        else
        {
            //..oops!
        }
    }
}

You're putting an int into the bundle but trying to retrieve a String value. Try

int myParam = extras.getInt("paramPosition");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM