-1

I want to select an item from a list. When this item is clicked, I want to open a new activity and have the related item shown in the next activity. This is the code for my click listener:

listView.setOnClickListener(new AdapterView.OnClickListener() {
    String item = (String) adapterView.getItemAtPosition(i);
    public onClick(AdapterView<?> adapterView, View view, int i, long l) {
        // Getting item text to be able to show it in a toast.
        String item = (String) adapterView.getItemAtPosition(i);

        Intent intent = new Intent(MainActivity.this, AddPlaneActivity.class);
        startActivity(intent);
    }
});
David Wasser
  • 93,459
  • 16
  • 209
  • 274
René B
  • 1
  • 2
  • what is your error? – Bentaye Apr 05 '18 at 09:51
  • Cannot resolve symbol adapterview. And on the onClick method: invalid method declaration, return type required – René B Apr 05 '18 at 09:56
  • why do you have `String item = (String) adapterView.getItemAtPosition(i);` twice ? the one outside the `onClick` method does not know the `adapterView `. It probably is what you error comes from – Bentaye Apr 05 '18 at 10:02

2 Answers2

0

From here:

Initial Activity:

Intent intent = new Intent(getBaseContext(), AddPlaneActivity.class);
intent.putExtra("EXTRA_STUFF_HERE", item);
startActivity(intent);

Access information from that intent on next activity:

String s = getIntent().getStringExtra("EXTRA_STUFF_HERE");
Edwin Chua
  • 658
  • 1
  • 8
  • 20
  • Thanks for your swift reply. The onClick method keeps complaining about not having a return type. – René B Apr 05 '18 at 10:05
0

try with

 lv.setOnItemClickListener(new OnItemClickListener()
    {
        @Override 
        public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
        { 
            Intent intent = new Intent(getBaseContext(), AddPlaneActivity.class);
            intent.putExtra("Item", item);
            startActivity(intent);
        }
    });

with android:descendantFocusability="blocksDescendants" in layout

Nik
  • 1,991
  • 2
  • 13
  • 30