首页 > 代码库 > 如何把一个listview中选中的值展示到另外一个 activity中

如何把一个listview中选中的值展示到另外一个 activity中

我正在创建一个简单的app,有一个功能是从第一个listView中取到选中的值展示到第二个activity中,中间是用了Intent传递,但是现在有个问题时我不知道如何才能在第二个activity中。

MainActivity.java

package com.devleb.listviewdemo;
 
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
 
public class MainActivity extends ListActivity {
    TextView txt;
    private static final String[] items = { "doctor", "engineer", "lawer",
            "developer", "employee", "business man", "auditer", "cashier" };
 
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, items));
        txt = (TextView) findViewById(R.id.txt);
 
    }
 
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
 
        // txt.setText(items[position]);
 
        Intent i = new Intent(this, SecondActivity.class);
        i.putExtra("testonArray", items);
        startActivity(i);
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
 
}
SecondActivity.java

package com.devleb.listviewdemo;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.EditText;
import android.widget.TextView;
 
public class SecondActivity extends Activity {
 
    TextView txt;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
 
        Bundle extras=getIntent().getExtras();
        String [] values = extras.getStringArray ("testonArray");
        txt = (TextView) findViewById (R.id.txt2);
        if (values != null && values.length > 0 && txt != null){
           txt.setText(values [0]);
        }
 
        }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.second, menu);
        return true;
    }
 
}


处理方法

First Activity的代码:

+@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
 
        // txt.setText(items[position]);
 
        // Try to send the items[position] in the intent
        Intent i = new Intent(this, SecondActivity.class);
        i.putExtra("testonArray", items[position].toString());
        startActivity(i);
    }

第二个activity代码

Bundle extras=getIntent().getExtras();
   String selected_item=extras.getString("testonArray");
   txt = (TextView) findViewById (R.id.txt2);
   txt.setText(selected_item);


原文地址:http://www.itmmd.com/201411/215.html 
该文章由 萌萌的IT人 整理发布,转载须标明出处。

如何把一个listview中选中的值展示到另外一个 activity中