后一个Activity向前一个Activity数据

1.后一个Activity向前一个Activity传递数据用到了一个新的方法startActivityForresult(Inten,requestCode);

给我的代码给你们参考参考;

2.MainActivity.java

package com.example.administrator.activity2;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private Button button;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       button=(Button) findViewById(R.id.button);
       button.setOnClickListener(this);



    }
    //设置按钮事件
    @Override
      public void onClick(View v){
          Intent it=new Intent(MainActivity.this,Main2Activity.class);
          startActivityForResult(it,0x123);
          }
//设置回传的方法,Ctrl+o,输入onAcivityResult()
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==0x123&&resultCode==0x123){
            Bundle db=data.getExtras();
          int imageId=db.getInt("image");
          imageView=(ImageView) findViewById(R.id.iview);
          imageView.setImageResource(imageId);
          }
    }
}

3.Main2Activity.java

package com.example.administrator.activity2;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Main2Activity extends AppCompatActivity {
    private int[] image={R.mipmap.icon1,R.mipmap.icon2,R.mipmap.icon3,R.mipmap.icon4,R.mipmap.icon5,R.mipmap.icon6,
            R.mipmap.icon7,R.mipmap.icon8,R.mipmap.icon9};
    private Context mContext;
    private GridView grid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        mContext=Main2Activity.this;
      grid=(GridView) findViewById(R.id.grid);
        List<HashMap<String,Object>> list=new ArrayList<HashMap<String, Object>>();
        for (int i=0;i<image.length;i++){
            HashMap<String,Object> map=new HashMap<String, Object>();
            map.put("image",image[i]);
            list.add(map);
        }
        SimpleAdapter adapter=new SimpleAdapter(mContext,list,R.layout.grid_item,new String[]{"image"},new int[]{
                R.id.image});
        grid.setAdapter(adapter);
        grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               Intent it =getIntent();
               Bundle db=new Bundle();
               db.putInt("image",image[position]);
               it.putExtras(db);
               setResult(0x123,it);
               finish();

            }
        });
    }

}

4.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv"
        android:text="选择你喜欢的APP图标"/>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/iview"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请选择你的图标"
        android:id="@+id/button"/>

</LinearLayout>

5.activity_main2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Main2Activity">
    <GridView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="3"
        android:id="@+id/grid"/>

</LinearLayout>

6.还有一个grid_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_width="86dp"
        android:layout_height="86dp"
        android:id="@+id/image"
        android:layout_centerInParent="true"
        />

</RelativeLayout>

7.效果图



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值