2016-06-10 37 views
1

我試圖創建多個不同的XML視圖/班消息傳遞應用程序數據:如何存儲在一個對象,而不是一類

一個主菜單(MainActivity);

一個用於發送消息的屏幕(SendMsg);

一個用於存儲發送的消息(日誌)。

SENDMSG,我想我的信息存儲到一個名爲MSGLOG的對象,這是我在MainActivity創造。僅用於測試,我已經設置SendMsg在按下SEND按鈕時顯示日誌類,並且發送的消息確實出現在那裏。

但是,它不將它存儲到我創建的,所以我不能從MainActivity再次訪問MSGLOG變量。

如何將它存儲到一個變量中,以便在訪問它時它總是在那裏?任何幫助將非常感激。

============================================== ===========

public class MainActivity extends AppCompatActivity { 

    Logs msgLog = new Logs(); 

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

    public void sendOnClick (View view){ 
     Intent intent = new Intent("com.example.android.attone.SendMsg"); 
     startActivity(intent); 
    } 

    public void logsOnClick (View view){ 
     Intent intent = new Intent(this, Logs.class); 
     startActivity(intent); 
    } 
} 

================================ =========================

public class SendMsg extends AppCompatActivity { 

    EditText txtPhoneNo; 
    EditText txtMessage; 
    Button btnSend; 

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

     txtPhoneNo = (EditText) this.findViewById(R.id.txtPhoneNo); 
     txtMessage = (EditText) this.findViewById(R.id.txtMessage); 
     btnSend = (Button) this.findViewById(R.id.btnSend); 

     btnSend.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View view) 
      { 
       // Get phone number and message sms 
       String phoneNo = txtPhoneNo.getText().toString(); 
       String message = txtMessage.getText().toString(); 

       Intent writeLog = new Intent(getApplicationContext(), Logs.class); 
       writeLog.putExtra("link", message); 
       startActivity(writeLog); 

       // If phone number and message is not empty 
       if (phoneNo.length() > 0 && message.length() > 0) 
       { 
        sendMessage(phoneNo, message); 
       } 
       else 
       { 
        Toast.makeText(getBaseContext(), "Please enter both number and message", Toast.LENGTH_LONG).show(); 
       } 
      } 
     }); 
    } 

    // Function send message sms 
    private void sendMessage(String phoneNo, String message) 
    { 
     try 
     { 
      SmsManager smsManager = SmsManager.getDefault(); 
      smsManager.sendTextMessage(phoneNo, null, message, null, null); 
      Toast.makeText(getApplicationContext(), "SMS sent", Toast.LENGTH_LONG).show(); 
     } 
     catch (Exception e) 
     { 
      Toast.makeText(getApplicationContext(), "SMS failed. Please try again!", Toast.LENGTH_LONG).show(); 
      e.printStackTrace(); 
     } 
    } 
} 

================== =======================================

public class Logs extends AppCompatActivity { 

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

     String logging = getIntent().getStringExtra("link"); 
     TextView textView = (TextView) findViewById(R.id.txtLog); 
     textView.setText(logging); 

     Button log2menu = (Button)findViewById(R.id.logToMenu); 
     log2menu.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(getApplicationContext(), MainActivity.class); 
       startActivity(intent); 
      } 
     }); 
    } 
} 
+0

'Logs msgLog = new Logs();'不要這樣做。 **從不**自己實例化一個活動 –

+0

*如何將它存儲到一個變量中,以便它在訪問它時總是存在?*如果「always」非常重要,那麼您將不得不使用數據庫。存儲在一個對象中的數據將最多可用,直到您關閉該應用程序,然後它將會消失 –

+0

我明白了 - 非常感謝!我對應用程序開發很陌生,所以我的知識仍然非常有限,而這對我來說並不明顯。我現在就放棄它:-) –

回答

0

您可以使用SharedPreferences存儲這些數據。 如果您不希望在應用程序關閉後保存數據,則可以嘗試另外一件事,那就是將數據存儲在單獨類的靜態變量中。

相關問題