ListView OnItemClickListener with a new action

I have a listView with OnItemClickListener. When I click on an element, I would like to open a new wiew in a new form:

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 need to transfer the number of the pressed position to a new activity and do not know how to do it.

Thanks.

+4
source share
3 answers

You must add it to the intent:

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

and in the new action call:

 int prePosition = getIntent().getIntExtra("position", someDefaultIntValue); 
+10
source

Try it,

 public class yourClassName { private static listIndex = 0; ...... ...... 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) { listIndex = position; Intent myIntent = new Intent(v.getContext(), UserSubmissionLog.class); startActivityForResult(myIntent, 0); UserSubmissionLog userSubmissionLogs= new UserSubmissionLog(position); System.out.println("Position "+position); } } ); // make new static method to access listIdex from another class private static int getListIndex() { return position; } } 
+1
source
 Intent myIntent = new Intent(v.getContext(), UserSubmissionLog.class); myIntent.putExtra("your_key_name_for_this_extra", position); startActivityForResult(myIntent, 0); 

And for the receiving activity, get the int value through

 int receivedValue = getIntent().getIntExtra("your_key_name_for_this_extra", default_value); 
0
source

Source: https://habr.com/ru/post/1396785/


All Articles