Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView OnItemClickListener with a new activity

I have a listView with an OnItemClickListener. When I am clicking on an item, I would like to open a new wiew in a new Activity like this:

final ListView lv1 = (ListView) findViewById(R.id.ListView02);
    lv1.setAdapter(new SubmissionsListAdapter(this,searchResults));

    lv1.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
            int position, long id) {
            Intent myIntent = new Intent(v.getContext(), UserSubmissionLog.class);
            startActivityForResult(myIntent, 0);
            UserSubmissionLog userSubmissionLogs= new UserSubmissionLog(position);
            System.out.println("Position "+position);
            }
        }
    );

The problem is that I have to transfer the clicked position number to the new activity and don't know how to do this.

Thank you.

like image 242
Milos Cuculovic Avatar asked Dec 22 '22 02:12

Milos Cuculovic


1 Answers

You should add it to the intent:

Intent myIntent = new Intent(v.getContext(), UserSubmissionLog.class);
myIntent.putExtra("position", position);
startActivityForResult(myIntent, 0);

and in the new Activity, call:

int prePosition = getIntent().getIntExtra("position", someDefaultIntValue);
like image 66
MByD Avatar answered Mar 28 '23 08:03

MByD