簡體   English   中英

如何從其他活動的活動中獲取數據..在android..?

[英]How to get Data from an activity from other activity ..in android..?

我對 android 開發很陌生,在為通訊錄編寫代碼時遇到了問題。 我的問題是:

我有兩個activity類( main_activityaddcontact_activity ),在main_activity ----一個buttonlistview和一個EditText ,如果我點擊button然后它重定向到第二個活動和在第二個活動中 ---三個editText (對於姓名,電話號碼和電子郵件)和兩個button (保存並清除)

在第一個活動中一切正常,但是當我單擊第二個活動的保存button ,它鏈接到Main_activity但從第二個活動發送到第一個活動的數據沒有顯示在main_activity所以請幫助!

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

}

}

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

    }
}

}

嘗試這個..

OnCreate()執行以下代碼

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

代碼

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

使用 getInent().getExtras() 獲取包。 或 getIntent().getExtra();

將此代碼添加到 onResume() 而不是暫停

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

在您的情況下,您正在使用Intent將數據發送到另一個activity ,並且通過以下方式正確發送:

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

現在,為了檢索數據,您需要通過以下方式從MainActivity Intent中獲取它:

getIntent().getStringExtra("name");  

把它放在MainActivity onCreate()中。

在讀取數據之前插入空檢查......為了Intent所以它不會拋出 NPE。
就像是:

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

閱讀更多關於Intents信息:

http://developer.android.com/reference/android/content/Intent.html
Android 中的 Intent 是什么?

Intent i = new Intent(this, ActivityTwo.class);//this your current class
startActivity(i); 
to pass a value in class 1

Intent i = new Intent(this, ActivityTwo.class);
i.putExtra("Value1", "This value one for ActivityTwo ");
i.putExtra("Value2", "This value two ActivityTwo"); 

    // to get values from ActivityTwo.class

Bundle extras = getIntent().getExtras();
if (extras == null) {
  return;
}
// get data via the key
String value1 = extras.getString(Intent.EXTRA_TEXT);
if (value1 != null) {
  // do something with the data
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM