-1

I am very new to android development and I am facing problem while writing code for a contact book. My question is :

I have Two activity class (main_activity and addcontact_activity) ,In main_activity ----one button,listview and One EditText, if I click on button then it redirects to second activity and In second activity ---three editText(for name,phone number and email) and two buttons(save and clear)

In first activity everything fine but when I clicked on save button from second activity then it links to Main_activity but the data sent from second activity to first activity it doesn't show in main_activity so please help!

Code of Main_activity:

EditText t;
Button b;
ListView lv;
ArrayList <String> al;
ArrayAdapter<String> ad;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    t=(EditText)findViewById(R.id.editText1);
    b=(Button)findViewById(R.id.button1);
    lv=(ListView)findViewById(R.id.listView1);
    b.setOnClickListener(this);



    al=new ArrayList<String>();
    ad=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,al);
    lv.setAdapter(ad);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    if(v.getId()==R.id.button1){
        Intent link = new Intent(getApplicationContext(),addcontact.class);
        startActivity(link);
    }
}


@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();

    Intent Info = getIntent();
    al.add(Info.getStringExtra("name").toString());
    ad.notifyDataSetChanged();

}

}

code for addContact_activity:

TextView name,phone,email;
Button save, reset;
EditText n,p,e;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addcon);

    name=(TextView)findViewById(R.id.textView1);
    phone=(TextView)findViewById(R.id.textView2);
    email=(TextView)findViewById(R.id.textView3);

    save=(Button)findViewById(R.id.save);
    save.setOnClickListener(this);
    reset=(Button)findViewById(R.id.button2);

    n=(EditText)findViewById(R.id.editText1);
    p=(EditText)findViewById(R.id.editText2);
    e=(EditText)findViewById(R.id.editText3);


}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
        if(v.getId()==R.id.save){
        Intent send = new Intent(getApplicationContext(),MainActivity.class);
        send.putExtra("name", n.getText().toString());
        startActivity(send);

    }
}

}

AndyN
  • 1,742
  • 1
  • 15
  • 30
Ravindra Yadav
  • 619
  • 2
  • 6
  • 15

4 Answers4

0

Try this..

Do below codes inside OnCreate()

Intent Info = getIntent();
al.add(Info.getStringExtra("name").toString());
ad.notifyDataSetChanged();

Code

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

    t=(EditText)findViewById(R.id.editText1);
    b=(Button)findViewById(R.id.button1);
    lv=(ListView)findViewById(R.id.listView1);
    b.setOnClickListener(this);



    al=new ArrayList<String>();
    ad=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,al);
    lv.setAdapter(ad);
    Intent Info = getIntent();
    if(Info.hasExtra("name")){
      al.add(Info.getStringExtra("name").toString());
      ad.notifyDataSetChanged();
    }
}
Hariharan
  • 24,741
  • 6
  • 50
  • 54
  • thanks for your answer ..but when I write your code into the OnCreate() in Main activity it gives an error because at first time when main activity runs it does not get any Intent information so that why this code not run..!!!!! – Ravindra Yadav Mar 26 '14 at 05:04
  • @user3462608 see my edited code. I used `if(Info.hasExtra("name"))` condition try it. – Hariharan Mar 26 '14 at 05:06
0

use getInent().getExtras() to get the bundle. or getIntent().getExtra();

Shriram
  • 4,343
  • 8
  • 37
  • 64
0

add this code on onResume() instead of on pause

Intent Info = getIntent();
    al.add(Info.getStringExtra("name").toString());
    ad.notifyDataSetChanged();
jyomin
  • 1,957
  • 2
  • 11
  • 27
0

In your case, you are sending the data to another activity using Intent, and correctly so by:

 Intent send = new Intent(getApplicationContext(),MainActivity.class);
            send.putExtra("name", n.getText().toString());
            startActivity(send);  

Now, in order to retrieve the data, you need to get it from Intent in MainActivity, by:

getIntent().getStringExtra("name");  

put that in onCreate() of MainActivity.

Insert a null check before reading your data...for the Intent so it doesn't throw a NPE.
Something like:

if(getIntent()!=null || getIntent().getExtras()!=null)

Read more for Intents on:

http://developer.android.com/reference/android/content/Intent.html
What is an Intent in Android?

Community
  • 1
  • 1
Pararth
  • 8,114
  • 4
  • 34
  • 51