首页 > 代码库 > 如何把一个listview中选中的值展示到另外一个 activity中
如何把一个listview中选中的值展示到另外一个 activity中
我正在创建一个简单的app,有一个功能是从第一个listView中取到选中的值展示到第二个activity中,中间是用了Intent传递,但是现在有个问题时我不知道如何才能在第二个activity中。
MainActivity.java
[java] view plaincopy
- 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的代码:
[html] view plaincopy
- +@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代码
[html] view plaincopy
- 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中
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。