如何将字符串从一个活动传递到另一个活动,并用于拨号盘和电子邮件目的

时间:2018-09-17 09:13:44

标签: java android

我有两个,一个Adapter和另一个Activity。适配器将Firebase数据结构中每个位置的额外字符串发送到下一个活动中,在该活动中显示从适配器传递来的数据。 效果很好。我无法在Textview中显示数据。但是,当我打算拨打电话或发送电子邮件时,我将无法使用接收的Extras,但是当我在Textview中使用setText时,它们会显示确切的数据。请帮助

这是适配器中的方法

@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {

        database = FirebaseDatabase.getInstance();
        dbreference = database.getReference("gender");

        g = bookslist.get(position);
 holder.teacher_quali.setText(g.getBqualifications());

        holder.profile_details.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(v.getContext(), gender_details.class);

                intent.putExtra(NEAR_LOCATION, g.getBlocation());
                intent.putExtra(AVAILAIBILITY, g.getBavailaile());

                intent.putExtra(MOBILE, g.getSellermobile());
                intent.putExtra(EMAIL, g.getSelleremail());


                v.getContext().startActivity(intent);
            }
});

我将MOBILE和EMAIL定义为

public static final String MOBILE = "other_mobile";
public static final String EMAIL= "other_email";

在同一适配器视图中,我的活动是

public class gender_details extends AppCompatActivity {

    private TextView tutor_email,tutor_mobile;
    private ImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.profile_details);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_details); // get the reference of Toolbar
        toolbar.setTitle(getIntent().getStringExtra(KEY_NAME));
        toolbar.setLogo(R.drawable.ic_person_black_24dp);
        setSupportActionBar(toolbar);




        String tutor_email_txt = "";
        String tutor_mobile_txt = "";


        tutor_email_txt = intent.getStringExtra(EMAIL);
        tutor_mobile_txt = intent.getStringExtra(MOBILE);
        // Setting values

        TextView Email_Txt = (TextView) findViewById(R.id.tutor_email);
        Email_Txt.setText(tutor_email_txt);

         TextView Contact_Txt = (TextView) findViewById(R.id.tutor_contact);
        Contact_Txt.setText(String tutor_mobile_txt);
    }
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.toolbar_menu, menu);
            return true;
        }
// Activity's overrided method used to perform click events on menu items
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
//noinspection SimplifiableIfStatement
// Display menu item's title by using a Toast.
            if (id == R.id.action_call) {


                Intent intent = new Intent(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:"+tutor_mobile_txt));
                startActivity(intent);


                return true;
            } else if (id == R.id.action_email) {

                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("plain/text");
                intent.putExtra(Intent.EXTRA_EMAIL, tutor_email_txt);
                intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
                intent.putExtra(Intent.EXTRA_TEXT, "mail body");
                startActivity(Intent.createChooser(intent, ""));

                return true;
            }
            return super.onOptionsItemSelected(item);
        }
    @Override
    public void onBackPressed() {
        Intent intent = new Intent(gender_details.this, MainActivity.class);
        startActivity(intent);
    }
}

正如您在Textview中看到的那样,信息可以正确显示,但是当我习惯使用Intent Action Dail或发送电子邮件时……我无法这样做。

请帮助

3 个答案:

答案 0 :(得分:3)

尝试一下:

全局声明变量

public class gender_details extends AppCompatActivity {

    private TextView tutor_email,tutor_mobile;
    private ImageView img;
     String tutor_email_txt;
        String tutor_mobile_txt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.profile_details);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_details); // get the reference of Toolbar
        toolbar.setTitle(getIntent().getStringExtra(KEY_NAME));
        toolbar.setLogo(R.drawable.ic_person_black_24dp);
        setSupportActionBar(toolbar);




       


        tutor_email_txt = intent.getStringExtra(EMAIL);
        tutor_mobilee_txt = intent.getStringExtra(MOBILE);
        // Setting values

        TextView Email_Txt = (TextView) findViewById(R.id.tutor_email);
        Email_Txt.setText(tutor_email_txt);

         TextView Contact_Txt = (TextView) findViewById(R.id.tutor_contact);
        Contact_Txt.setText(String tutor_mobile_txt);
    }
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.toolbar_menu, menu);
            return true;
        }
// Activity's overrided method used to perform click events on menu items
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
//noinspection SimplifiableIfStatement
// Display menu item's title by using a Toast.
            if (id == R.id.action_call) {


                Intent intent = new Intent(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:"+tutor_mobile_txt));
                startActivity(intent);


                return true;
            } else if (id == R.id.action_email) {

                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("plain/text");
                intent.putExtra(Intent.EXTRA_EMAIL, tutor_email_txt);
                intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
                intent.putExtra(Intent.EXTRA_TEXT, "mail body");
                startActivity(Intent.createChooser(intent, ""));

                return true;
            }
            return super.onOptionsItemSelected(item);
        }
    @Override
    public void onBackPressed() {
        Intent intent = new Intent(gender_details.this, MainActivity.class);
        startActivity(intent);
    }
}

答案 1 :(得分:0)

是否在清单中声明许可,否则请声明:

uses-permission android:name =“ android.permission.CALL_PHONE”

答案 2 :(得分:0)

您的代码中存在一些严重的命名问题。首先,您需要将tutor_mobile_txt变量设置为全局变量。其次,在onCreate内部,您正在初始化tutor_mobilee_txt而不是tutor_mobile_txt。更正拼写,然后检查。