Question

I used OnItemClickListener to select an item from the list view as follows:

    listView.setTextFilterEnabled(true);

    final TextView disp = (TextView) findViewById(R.id.textView1); 
    listView.setOnItemClickListener(new OnItemClickListener() {

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

Now I want to display the result in another activity. How can i achieve it?

Was it helpful?

Solution

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);
}   

OTHER TIPS

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);
}  

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");
 @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");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top