Click ListView Item跳转Activity

时间:2023-12-27 13:29:31

今天学习了ListView点击Item跳转,修改上一篇代码bindData方法

lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
ListView listView = (ListView)arg0;
HashMap<String, String> map = (HashMap<String, String>)listView.getItemAtPosition(arg2);
String id = map.get("id"); Bundle bundle = new Bundle(); //bundle可以添加多个参数
bundle.putString("id", id);
//bundle.putString("info", "info");
Intent intent = new Intent(MainActivity.this, LifeCycleActivity.class); //create intent object
//intent.putExtra("id",id); //parma
intent.putExtras(bundle); //params
MainActivity.this.startActivity(intent); //start activity
}
});

添加activitylifecycle.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" /> </LinearLayout>

添加activity类:LifeCycleActivity

public final String TAG="Actity:";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activitylifecycle);
Log.d(TAG, "------onCreate------");
//get params
Bundle bundle = this.getIntent().getExtras();
String id = bundle.getString("id");
Log.e("params:", "--------------------:"+id+"----------------"); Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() { public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(LifeCycleActivity.this, MainActivity.class);
startActivity(intent);
LifeCycleActivity.this.finish();
}
});
}

跳转到LifeCycleActivity 见下面效果

Click ListView  Item跳转Activity

上面即点击ListView点击Item跳转.