开启新的activity获取他的返回值

package visizen.com.getnewactivityreturndata;

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

public class MainActivity extends AppCompatActivity {

    private Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn= (Button) findViewById(R.id.button);
    }

    public void gotoA(View view){
        System.out.println("xxxxxxx");
        Intent intent=new Intent(this,SecondActivity.class);
        startActivityForResult(intent,1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(data!=null){
            String data1 = data.getStringExtra("data");
            if(requestCode==1){
                btn.setText(data1);
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
package visizen.com.getnewactivityreturndata;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/12/9 0009.
 */
public class SecondActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activiey_second);

        ListView listView = (ListView) this.findViewById(R.id.listView);

        List<Map<String,String>> data=new LinkedList<Map<String,String>>();

        for (int i=0;i<100;i++){
            Map<String,String> map=new HashMap<String,String>();
            map.put("name","name"+i);
            map.put("phone","phone"+i);
            data.add(map);
        }

        /**
         * Constructor
         *
         * @param context The context where the View associated with this SimpleAdapter is running
         * @param data A List of Maps. Each entry in the List corresponds to one row in the list. The
         *        Maps contain the data for each row, and should include all the entries specified in
         *        "from"
         * @param resource Resource identifier of a view layout that defines the views for this list
         *        item. The layout file should include at least those named views defined in "to"
         * @param from A list of column names that will be added to the Map associated with each
         *        item.
         * @param to The views that should display column in the "from" parameter. These should all be
         *        TextViews. The first N views in this list are given the values of the first N columns
         *        in the from parameter.
         */

        listView.setAdapter(new SimpleAdapter(this,data,R.layout.item,new String[]{"name","phone"},new int[]{R.id.name,R.id.phone}));

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            /**
             *
             * @param parent 当前列表对象,如ListView对象
             * @param view  被单击的列表项对象
            * @param position  被单击的列表项在列表中的索引值
             * @param id  被单击的列表项在列表中所处的行的索引值
             */
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //System.out.println(parent);

               Button nameBtn = (Button) view.findViewById(R.id.name);
               Button phoneBtn = (Button) view.findViewById(R.id.phone);

                Intent data=new Intent();
                data.putExtra("data",nameBtn.getText().toString()+phoneBtn.getText());
                setResult(100,data);

                finish();
            }
        });
    }
}

 

posted on 2015-12-09 17:54  jayhtt  阅读(76)  评论(0)    收藏  举报