Reputation: 409
Currently I have two activity's in my android up. The MainActivity page displays toast notifications and I want my HistoryActivity page to display all the previous toasts. Before I send a toast from my MainActivity page I save into arrayList. Then, I want to display all the strings in this arrayList on the HistoryActivity page. I am unsure how to build a textview and how to integrate the textview with the strings in the arrayList. Please Help.
I also think I need to add something to my xml files, but I'm unsure of that too. Thanks.
The current code looks like this for the MainActivity page.
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
private ContextCoreConnector contextCoreConnector;
private ContextPlaceConnector contextPlaceConnector;
private ArrayList<String> locations;
//Initializing contextPlaceConnector here, I initialize PlaceEventListener below and the logic for when a geofence is broken is below.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contextCoreConnector = ContextCoreConnectorFactory.get(this);
contextPlaceConnector = ContextPlaceConnectorFactory.get(this);
checkContextConnectorStatus();
}
@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;
}
private PlaceEventListener placeEventListener = new PlaceEventListener() {
@Override
public void placeEvent(PlaceEvent event) {
String placeNameAndIdCurrent = " You are now at " + event.getPlace().getPlaceName() + ". The ID number of this location is " + event.getPlace().getId();
String placeNameId = "Location is "+event.getPlace().getPlaceName() +". The ID number of this location is " + event.getPlace().getId();
Toast toast = Toast.makeText(getApplicationContext(), placeNameAndIdCurrent, Toast.LENGTH_LONG);//changed from Toast.LENGTH_LONG
toast.show();
Log.i("found place", placeNameAndIdCurrent);
locations= new ArrayList<String>();
locations.add(placeNameId);
}
};
private void checkContextConnectorStatus() {
if (contextCoreConnector.isPermissionEnabled()) {
// Gimbal is already enabled
startListeningForGeofences();
}
else {
contextCoreConnector.enable(this, new Callback<Void>() {
@Override
public void success(Void arg0) {
startListeningForGeofences();
// Gimbal is ready
}
@Override
public void failure(int arg0, String arg1) {
Log.e("failed to enable", arg1);
}
});
}
}
private void startListeningForGeofences() {
contextPlaceConnector.addPlaceEventListener(placeEventListener);
}
@Override
protected void onResume() {
super.onResume();
contextCoreConnector.setCurrentActivity(this);
}
@Override
protected void onPause() {
super.onPause(); contextCoreConnector.setCurrentActivity(null);
}
public void sendHistory(View view){
Intent intent = new Intent(this, HistoryActivity.class);
//EditText editText = (EditText) findViewById(R.id.edit_message);
intent.putStringArrayListExtra("key", locations);
startActivity(intent);
}
public void sendLogin(View view){
Intent intent = new Intent(this, LoginActivity.class);
//EditText editText = (EditText) findViewById(R.id.edit_message);
startActivity(intent);
}
// return arrayList so that it can be used in other classes
public ArrayList<String> getList(){
return locations;
}
Then the HistoryActivity page looks like this.
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.TextView;
public class HistoryActivity extends Activity {
//private MainActivity Mac;
private TextView myText = null;
ArrayList<String> list = getIntent().getStringArrayListExtra("key");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
LinearLayout lView = new LinearLayout(this);
myText= new TextView(this);
lView.addView(myText);
for (int i=0; i<list.size();i++){
myText.append(list.get(i));
myText.append("\n");
}
setContentView(lView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.history, menu);
return true;
}
Now I am getting this error.
01-31 01:28:25.675: E/AndroidRuntime(14623): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{concertpass.app/concertpass.app.HistoryActivity}: java.lang.NullPointerException
01-31 01:28:25.675: E/AndroidRuntime(14623): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
01-31 01:28:25.675: E/AndroidRuntime(14623): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-31 01:28:25.675: E/AndroidRuntime(14623): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-31 01:28:25.675: E/AndroidRuntime(14623): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-31 01:28:25.675: E/AndroidRuntime(14623): at android.os.Handler.dispatchMessage(Handler.java:99)
01-31 01:28:25.675: E/AndroidRuntime(14623): at android.os.Looper.loop(Looper.java:123)
01-31 01:28:25.675: E/AndroidRuntime(14623): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-31 01:28:25.675: E/AndroidRuntime(14623): at java.lang.reflect.Method.invokeNative(Native Method)
01-31 01:28:25.675: E/AndroidRuntime(14623): at java.lang.reflect.Method.invoke(Method.java:521)
01-31 01:28:25.675: E/AndroidRuntime(14623): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
01-31 01:28:25.675: E/AndroidRuntime(14623): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-31 01:28:25.675: E/AndroidRuntime(14623): at dalvik.system.NativeStart.main(Native Method)
01-31 01:28:25.675: E/AndroidRuntime(14623): Caused by: java.lang.NullPointerException
01-31 01:28:25.675: E/AndroidRuntime(14623): at concertpass.app.HistoryActivity.<init>(HistoryActivity.java:15)
01-31 01:28:25.675: E/AndroidRuntime(14623): at java.lang.Class.newInstanceImpl(Native Method)
01-31 01:28:25.675: E/AndroidRuntime(14623): at java.lang.Class.newInstance(Class.java:1429)
01-31 01:28:25.675: E/AndroidRuntime(14623): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
01-31 01:28:25.675: E/AndroidRuntime(14623): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
<activity
android:name="concertpass.app.HistoryActivity"
android:label="@string/title_activity_history" >
</activity>
Thanks again.
Upvotes: 1
Views: 16566
Reputation: 133560
First you need to pass the arraylist to HistoryPage
using intent.
Intent intent = new Intent(this, HistoryActivity.class);
intent.putStringArrayListExtra("key",locations);
startActivity(intent);
Then in HistoryPage
ArrayList<String> list = getIntent().getStringArrayListExtra("key");
Then loop through the list and display it in textview.
LinearLayout lView = new LinearLayout(this);
myText= new TextView(this);
lView.addView(myText);
for (int i=0; i<list.size();i++){
myText.append(list.get(i));
myText.append("\n");
}
setContentView(lView);
You can remove this
setContentView(R.layout.activity_history);
You can also use a ListView
which i think is appropriate i this case
Upvotes: 1