• startActivityForResult


    从一个activity A跳转到另一个activity B,需要获取B的数据时,就使用startActivityForResult去启动B

    比如从当前activity跳转到系统联系人的activity,并获取联系人信息

    public class DemoActivity extends Activity {
        EditText et, et2;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            et = (EditText) this.findViewById(R.id.et_contact1);
            et2 = (EditText) this.findViewById(R.id.et_contact2);
        }
    
        public void click(View view) {
            Intent intent = new Intent(this, ContactActivity.class);
            // startActivity(intent);
            startActivityForResult(intent, 1);
        }
    
        public void click2(View view) {
            Intent intent = new Intent(this, ContactActivity.class);
            // startActivity(intent);
            startActivityForResult(intent, 2);
        }
    
        /**
         * 当被激活的activity 使用完毕 销毁的时候 就会执行onActivityresult的方法
         */
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            // TODO Auto-generated method stub
            
            System.out.println("结果码为"+ resultCode);
            super.onActivityResult(requestCode, resultCode, data);
    
            if (data != null) {
                String name = data.getStringExtra("name");
                if (requestCode == 1) {
    
                    et.setText(name);
                }else if(requestCode == 2){
                    et2.setText(name);
                }
                
            }
        }
    
    }

    联系人操作类

    public class ContactActivity extends Activity {
        ListView lv;
        List<String> names;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.contact);
            lv = (ListView) this.findViewById(R.id.lv);
            names = getContacts();    //获取联系人的姓名
            lv.setAdapter(new MyAdapter());
            
            lv.setOnItemClickListener(new OnItemClickListener() {
    
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    TextView tv = (TextView) view;
                    String name = tv.getText().toString();
                    Intent data = new Intent();
                    data.putExtra("name", name);
                    setResult(3, data);
                    // 可以把当前的activity给结束掉 
                    finish();
                }
            });
            
        }
        private class MyAdapter extends BaseAdapter{
    
            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return names.size();
            }
    
            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return position;
            }
    
            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return position;
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                TextView tv = new TextView(ContactActivity.this);
                tv.setTextSize(28);
                tv.setTextColor(Color.RED);
                tv.setText(names.get(position));
                return tv;
            }
            
        }
    
        private List<String> getContacts (){
            String rawbase = "content://com.android.contacts/raw_contacts";
            String database = "content://com.android.contacts/data";
            Cursor cursor = this.getContentResolver().query(Uri.parse(rawbase), null, null, null, null);
            List<String> names = new ArrayList<String>();
            while (cursor.moveToNext()) {
                
                String id  = cursor.getString( cursor.getColumnIndex("_id"));
                System.out.println("id ="+ id);
                
                Cursor datacursor = this.getContentResolver().query(Uri.parse(database), null, "raw_contact_id=?", new String[]{id}, null);
                while (datacursor.moveToNext()) {
                    
    
    
                    String type = datacursor.getString(datacursor.getColumnIndex("mimetype"));
    
                    
                    if("vnd.android.cursor.item/name".equals(type)){
                        String data1 = datacursor.getString( datacursor.getColumnIndex("data1"));
                        names.add(data1);
                        
                    }
                    
                }
                datacursor.close();
            
            }
            cursor.close();
            return names;
        }
        
        
    }

  • 相关阅读:
    Auth模块使用方法大全
    正则模块
    Django实现Rbac权限管理
    Django models中关于blank与null的补充说明
    Laravel日常使用总结
    关闭IIS开启自启
    phpstorm常用快捷键
    将程序sublime添加到右键菜单中
    form表单利用iframe高仿ajax
    PHP生成二维码,PHPQRCode
  • 原文地址:https://www.cnblogs.com/android-zcq/p/3388962.html
Copyright © 2020-2023  润新知