码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-12-26 18:41:44      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:android   android开发   

我正在创建一个简单的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中

标签:android   android开发   

原文地址:http://blog.csdn.net/androidmylove/article/details/42174595

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!