您的位置:首页 > 其它

ListView Item点击事件跳转详情界面

2015-05-07 21:51 2171 查看
有好久都没有碰安卓了,考试加五一双组合也真是让人蛋疼。好在都平安的度过了,所以又有时间gun回来弄弄我的小博客(虽然没多少人看= =)。看了之前做的几个小demo,模式都差不多,但是每次写的时候都要在看一遍知识点,(笨DIE)。这次mark一下,成为自己的东西!

-----------------------------------------------------吐槽分割线-----------------------------------------------------------------

这次写的是ListView 每个Item的点击事件跳转详情界面的知识。

我的DEMO是用ListView展示SQLite里的每一条数据。(这个内容之后再总结一篇)。每一个Item里有TextView显示数据库里的信息。

首先,给Item绑定监听事件,然后使用bundle类,顾名思义,就是将信息捆成一捆传递给下一个ACTIVITY。

这里用到bundle里的putString(String key,String value)方法,具体API解释:

Inserts a String value into the mapping of this Bundle, replacing any existing value for the given key. Either key or value may be null.(key值是另一个Activity需要用到的关键值,value是你需要传进去的值)。

之后就是想下一个活动传递数据操作:

新建一个意图关联当前的活动和下一个活动:Intent intent = new Intent(MainActivity.this,detailInfo.class);

调用Intent的putExtras(Bundle extras)方法:将之前的bundle参数传进去即可。

startActivity(intent)就ok。

listView.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Information info = list.get(position);
				Bundle bundle = new Bundle();
				
				bundle.putString("fromWhere", info.getFromWhere());
				bundle.putString("toWhere", info.getToWhere());
				bundle.putString("time",info.getTime());
				bundle.putString("content", info.getContent());
				
				Intent intent = new Intent(MainActivity.this,detailInfo.class);
				intent.putExtras(bundle);
				finish();
				startActivity(intent);
			}								
		});


下一个Activity接受数据,模式如下:

Textview fromWhere=(TextView)findViewById(R.id.fromWhere);
		Bundle b=getIntent().getExtras();
		  //获取Bundle的信息
		String info=b.getString("fromWhere");
		fromWhere.setText("起始地:"+info);


其中的getString方法中传入的就是之前的key值。这样就能在TextView里面显示Item想要传的数据

----------------------------------------------------正经分割线-------------------------------------------------------------------------

自己都觉得好不专业的样子= =!!!只是一个超级简单的传递数据功能。之后这类知识会出一个姊妹篇什么的,剧本已经写好,就差女主了(什么鬼!)。

好了,写完感觉自己萌萌哒,喝杯百事压压惊~bye - bye
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐