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

来源:互联网 发布:三层交换机端口配置ip 编辑:程序博客网 时间:2024/05/24 04:39

  有好久都没有碰安卓了,考试加五一双组合也真是让人蛋疼。好在都平安的度过了,所以又有时间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(){@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {// TODO Auto-generated method stubInformation 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




0 0
原创粉丝点击