在android中的活动之间传递值

时间:2015-08-26 06:27:26

标签: android android-intent android-activity

我有以下一项活动:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.academic_lay);
        Exp_list = (ExpandableListView) findViewById(R.id.exp_list);
        Academic_category = DataProvider.getInfo();
        Academic_list = new ArrayList<String>(Academic_category.keySet());
        adapter = new AcademicAdapter(this, Academic_category, Academic_list);
        Exp_list.setAdapter(adapter);
        Exp_list.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                                        int groupPosition, int childPosition, long id) {

                if(Academic_category.get(
                        Academic_list.get(groupPosition)).get(
                        childPosition)=="Department of Economics"){
                    String dpmt="Economics";
                    Intent intent = new Intent(Academic_Structure.this, TabLayoutActivity.class);
                    intent.putExtra("Dpmt", dpmt);
                    System.out.println(dpmt);
                    startActivity(intent);

我在第一项活动中收到键#34; Dpmt&#34;的另一项活动如下:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pdf_layout);
        System.out.println(getIntent().getStringExtra("Dpmt"));

但它将输出设为null。 请允许任何人帮助我如何获得价值&#34;经济学&#34;关键&#34; Dpmt&#34;在第二项活动中。

3 个答案:

答案 0 :(得分:0)

也许您应该将==Department of Economics更改为equals("Department of Economics");

答案 1 :(得分:0)

而不是

intent.putExtra("Dpmt", dpmt);

尝试

intent.putExtra("Dpmt", dpmt.toString());

这是第二类的代码

public class Academic_Structure extends Activity {
    HashMap<String, List<String>> Academic_category;
    List<String> Academic_list;
    ExpandableListView Exp_list;
    AcademicAdapter adapter;





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.academic_lay);
        Exp_list = (ExpandableListView) findViewById(R.id.exp_list);
        Academic_category = DataProvider.getInfo();
        Academic_list = new ArrayList<String>(Academic_category.keySet());
        adapter = new AcademicAdapter(this, Academic_category, Academic_list);
        Exp_list.setAdapter(adapter);
        Exp_list.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                                        int groupPosition, int childPosition, long id) {

                if(Academic_category.get(
                        Academic_list.get(groupPosition)).get(
                        childPosition).equals("Department of Economics")){
                    String dpmt="Economics";
                    Intent intent = new Intent(Academic_Structure.this, TabLayoutActivity.class);
                    intent.putExtra("Dpmt", dpmt.toString());
                    System.out.println(dpmt);
                    startActivity(intent);
                    //startActivity(new Intent("com.bbau.ankit.test_splash.TabLayoutActivity"));
                }
                if(Academic_category.get(
                        Academic_list.get(groupPosition)).get(
                        childPosition).equals("Department of History")){
                    startActivity(new Intent("com.bbau.ankit.test_splash.Contactus"));
                }
                if(Academic_category.get(
                        Academic_list.get(groupPosition)).get(
                        childPosition).equals("Department of Political Science")){
                    startActivity(new Intent("com.bbau.ankit.test_splash.Contactus"));
                }

                return false;
            }
        });

    }

}

在TabLayoutActivity内部,请尝试使用此代码段

Bundle extras = getIntent().getExtras();
if (extras != null) {
   String dmpt= extras.getString("Dmpt");                
   System.out.println("Dmpt"+dmpt);
} else {
   System.out.println(bundle is null);
}

答案 2 :(得分:0)

获得价值使用时:

String dpmt = getIntent().getExtras().getString("keyName","defaultKey");