Good day everyone.

After clicking on the listview list item, you need to transfer its position (list item number) to another activation. I use position, which is passed to onListItemClick. But for some other reason, null is passed for some reason.

Help me to understand))

public class MainActivity extends ListActivity { @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); Intent intent = new Intent(getApplicationContext(), AfterClickActivity.class); intent.putExtra("position",position); startActivity(intent); } @Override protected void onCreate(Bundle savedInstanceState) { Integer[] array = new Integer[100]; for (int i = 0; i <100 ; i++) { array[i] = i; } super.onCreate(savedInstanceState); ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(),R.layout.list_item,R.id.tvText,array); setListAdapter(adapter); 

}

 public class AfterClickActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_after_click); String position = getIntent().getStringExtra("position"); TextView view = (TextView)findViewById(R.id.tvView1); view.setText(String.valueOf(position)); } 
  • Language is guessed by your code, and the platform? - VladD
  • Android Studio / - 600

1 answer 1

Get in the second activation:

 int position = getIntent().getIntExtra("position", 0); 

When passing through an Intent it is important to respect the types. You put an int , and then try to get a String

  • Thank you very much. This is how int position = getIntent (). GetIntExtra ("position", 0); - 600