Pass the value from 1st activity to 2nd activity in android

。_饼干妹妹 提交于 2020-01-06 04:30:07

问题


As am getting article title on textview on first activity.how can i pass these textview to next activity...

I have used below code:

    for ( j = 0; j <Appscontent.Sub_arraylisttwo.size(); j++) 
      {
         LinearLayout ly = new LinearLayout(this);
         ly.setOrientation(LinearLayout.VERTICAL);
            ly.setOnClickListener(mArticleClick);
         TextView tv = new TextView(this);
         tv.setText(Appscontent.Sub_arraylisttwo.get(j));   

        ly.addView(tv);
        lLayout.addView(ly);
     }

       int num=Integer.parseInt(number);
       number=String.valueOf(num=num+1);
        System.out.println("the Number Value Is"+number);
          Appscontent.Sub_arraylisttwo.clear();

       hSroll.addView(lLayout);
       viewLayout.addView(headerText);
      viewLayout.addView(hSroll);
       verticalLayout.addView(viewLayout);

      Log.i("12", "" + lLayout.getChildCount());}
         }
      private OnClickListener   mArticleClick   = new OnClickListener() {

                                @Override
                                public void onClick(View v) {

                                Intent in = new Intent(MainActivity.this, SubCate.class);

                               startActivity(in); 

                                }
                            };

Here i have to click one article means that article name only pass to next activity and display that article title.. how can i do ??? please give me solution for these ???


回答1:


if you want to use intents:

while going to the ListActivity pass data by..

intent.putExtra("Title", yourstring);
intent.putExtra("Content", yourstring);
startActivity(intent);

and to recover it in second activity use:

title= getIntent().getExtras().getString("Title");

...and so on..




回答2:


//to pass :
 Intent in = new Intent(MainActivity.this, SubCate.class);
in.putExtra("name", "Artical Name");  
 startActivity(in);


// to retrieve object in second Activity
getIntent().getSerializableExtra("name");



回答3:


 public void onClick(View view)
 {
    public void run()
    {
            Intent i=new Intent(activity1.this,activity2.class);
            i.putExtra("somename", variable1);
            i.putExtra("somename1", variable2);         

    }
 }

In the second activity

     Bundle extras = getIntent().getExtras();
    if (extras != null) {
        one= extras.getDouble("somename");
        two = extras.getDouble("somename2");

    }


来源:https://stackoverflow.com/questions/15173134/pass-the-value-from-1st-activity-to-2nd-activity-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!