• Click ListView Item跳转Activity


    今天学习了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 见下面效果

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

  • 相关阅读:
    矩阵快速幂
    快速排序
    用闭包来实现命令模式
    闭包和面向对象设计
    闭包及其作用
    阿里笔试
    如何在节点上添加样式
    getComputedStyle与currentStyle获取样式(style/class)
    今日头条笔试
    牛客网JavaScript编程规范
  • 原文地址:https://www.cnblogs.com/mlgblog/p/3419992.html
Copyright © 2020-2023  润新知