从android中的arrayList显示字符串

时间:2014-01-28 04:16:19

标签: android arraylist textview

目前我在我的机器人中有两个活动。 MainActivity页面显示Toast通知,我希望HistoryActivity页面显示所有以前的Toast。在从MainActivity页面发送Toast之前,我保存到arrayList中。然后,我想在HistoryActivity页面上显示此arrayList中的所有字符串。我不确定如何构建textview以及如何将textview与arrayList中的字符串集成。请帮助。

我还认为我需要在我的xml文件中添加一些东西,但我也不确定。感谢。

MainActivity页面的当前代码如下所示。

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

然后,HistoryActivity页面看起来像这样。

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

现在我收到了这个错误。

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>

再次感谢。

1 个答案:

答案 0 :(得分:1)

首先,您需要使用intent将arraylist传递给HistoryPage

    Intent intent = new Intent(this, HistoryActivity.class);
    intent.putStringArrayListExtra("key",locations);
    startActivity(intent); 

然后在HistoryPage

    ArrayList<String> list = getIntent().getStringArrayListExtra("key");

然后遍历列表并在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);

您可以删除此

  setContentView(R.layout.activity_history);

你也可以使用我认为合适的ListView