在不同的活动上显示OnItemClickListener的结果

时间:2013-09-01 16:06:09

标签: android android-listview onitemclicklistener

我使用OnItemClickListener从列表视图中选择一个项目,如下所示:

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

现在我想在另一个活动中显示结果。我怎样才能实现它?

4 个答案:

答案 0 :(得分:0)

使用意图

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

在SecondActivity中

拥有textview

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

答案 1 :(得分:0)

您可以将“putExtra”添加到intent对象

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

并在第二次活动中恢复它:

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

答案 2 :(得分:0)

将此内容写入点击侦听器

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

在第二项活动中执行此操作 -

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

答案 3 :(得分:0)

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

} });

NextActivity.class

Bundle data = getIntent().getExtras();

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