Skip to content Skip to sidebar Skip to footer

Displaying Results Of OnItemClickListener On A Different Activity

I used OnItemClickListener to select an item from the list view as follows: listView.setTextFilterEnabled(true); final TextView disp = (TextView) findViewById(R.id.textVie

Solution 1:

Write this inside your click listener

Intent myIntent = new Intent(YourCurentActivity.this, SecondActivity.class);
myIntent.putExtra("key", temp);
startActivity(myIntent);

Do this in second activity-

   Bundle extras = getIntent().getExtras();
if(extras!=null)
{
 String temp = extras.getString("key");
 textview.setText(temp);
}   

Solution 2:

Use Intents

@Override
public void onItemClick(AdapterView<?> arg0, View view, int position,long id) {
    String temp = (String) listView.getItemAtPosition(position);
    // or String temp = (String) arg0.getItemAtPosition(position);
    disp.setText(temp);// display on screen
    Intent i = new Intent(ActivityName.this,SecondActivity,class);
    i.putExtra("key",temp);
}
});

In SecondActivity

Have a textview

Bundle extras = getIntent().getExtras();
if(extras!=null)
{
     String value = extras.getString("key");
     // textview.setText(value);
}  

Solution 3:

You can add "putExtra" to the intent object

intent.putExtra("listViewItem", temp);
startActivity(intent);

and to recover it in second activity use:

listViewItem= getIntent().getExtras().getString("listViewItem");

Solution 4:

 @Override
public void onItemClick(AdapterView<?> arg0, View view, int position,long id) {
String temp = (String) listView.getItemAtPosition(position);
disp.setText(temp);// display on screen

Bundle bund = new Bundle();

bund.putString("YourKey",disp);

Intent intent = new Intent(CurrentActivity.this,NextActivity,class);
intent.putExtra(bund);

startActivity(intent);

} });

In NextActivity.class

Bundle data = getIntent().getExtras();

if(data!=null)
   String temp = data.getString("YourKey");

Post a Comment for "Displaying Results Of OnItemClickListener On A Different Activity"