user2870902
user2870902

Reputation: 81

how to pass the selected item (string) from listview

i have a listview and when the user select an item will open a new activity that will be compared with the data in database ( sqlite ) and retrieve the data. i can only pass the position of the item selected but i want to pass the string itself.

code from the first activity:

  list.setAdapter(adapter);  
       list.setOnItemClickListener(new OnItemClickListener() {   
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) {      

                Intent myIntent = new Intent(view.getContext(), ShowWhereToGo.class); 
                myIntent.putExtra("id", position);
                startActivityForResult(myIntent, 0); // display SubView.class               }
            }

            @SuppressWarnings("unused")
            public void onItemClick1(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub

            }});

the code from second activity :

 Bundle extras = getIntent().getExtras();
     if (extras != null) {
         String temp = extras.getString("id");

      Toast.makeText(getApplicationContext(),
                    "Position :"+temp , Toast.LENGTH_LONG)
                .show();
     }

Upvotes: 0

Views: 1799

Answers (2)

Jagadesh Seeram
Jagadesh Seeram

Reputation: 2664

In the ItemClick of the Listview

  public void onItemClick(AdapterView<?> parent, View view,int position, long id)   {      
      Intent myIntent = new Intent(view.getContext(), ShowWhereToGo.class); 
      myIntent.putExtra("item", adapter.getItem(position));
      myIntent.putExtra("id", position);
      startActivityForResult(myIntent, 0); // display SubView.class               }
  }

While retrieving

     String item = getIntent().getExtras().getString("item");

Upvotes: 1

Raghunandan
Raghunandan

Reputation: 133560

Use the below

 String value =(String) parent.getItemAtPosition(position);
 myIntent.putExtra("value", value);

Remove onItemClick1

Upvotes: 1

Related Questions