首页 > 解决方案 > 如何将字符串从一个活动传递到另一个活动并将其用于拨号盘和电子邮件

问题描述

我有两个一个 Adapter 和另一个 Activity 。Adapter 根据 Firebase 数据结构中的位置将 String Extra 发送到 Next 活动中,在该活动中显示从 Adapter 传递的数据。它工作得很好。我无法在 Textview 中显示数据。但是当我用户打算拨打电话或发送电子邮件时,我无法使用我收到的附加信息,但是当我在 Textview 中设置文本时......它们显示了确切的数据。请帮助

这是适配器中的方法

@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 或发送电子邮件时......我无法这样做。

请帮忙

标签: javaandroid

解决方案


试试这个:

全局声明变量

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);
    }
}


推荐阅读