无法使用Service中的新值增加ArrayList

时间:2011-11-23 15:01:06

标签: java android

我正在尝试在服务中填充一个名为items的ArrayList,但是我从我的活动中调用服务的所有内容只有第一个元素填充到数组中。我不确定最好的方法是什么。我听说过使用绑定服务方法,但从我读到的内容中我并未提出建议。

如何在服务isDestroyed()

之前将字符串存储在持久性arraylist中

提前致谢,

问题代码

    public void print_result(String orig) {
        Log.d(TAG, "HELLO WORLD:" + orig);
        int i = 0;
        items = new ArrayList<String>();
        if (items != null) {
            if (items.contains(orig)) {
                Log.d(TAG, "Element already exists exiting");
            } else {

                items.add(orig);

                Log.d(TAG, "Adding Element" + items);

            }

        } else {
            Log.d(TAG, "IS NULL");
        }
    }  
格雷厄姆

活性

 package com.example;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class ServicesDemo extends Activity implements OnClickListener {
    private static final String TAG = "ServicesDemo";
    Button buttonStart, buttonStop, buttonAdd;
    EditText Input;
    String x = "";// test pass to my service

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        buttonStart = (Button) findViewById(R.id.buttonStart);
        buttonStop = (Button) findViewById(R.id.buttonStop);
        buttonAdd = (Button) findViewById(R.id.buttonAdd);
        Input = (EditText) findViewById(R.id.INPUT);
        buttonStart.setOnClickListener(this);
        buttonStop.setOnClickListener(this);

    }

    public void onClick(View src) {
        switch (src.getId()) {
        case R.id.buttonStart:
            Log.d(TAG, "onClick: starting srvice");
            Intent dataIntent = new Intent(ServicesDemo.this, MyService.class);
            x = Input.getText().toString();
            dataIntent.putExtra("originator", x);
            startService(dataIntent);

            break;

        case R.id.buttonStop:

            Log.d(TAG, "onClick: stopping srvice");
            // implicit starting
            stopService(new Intent(this, MyService.class));
            break;

        case R.id.buttonAdd:

            Log.d(TAG, "ADDING VALUES");
            break;
        }

    }

    public static String getTag() {
        return TAG;
    }
}

服务

package com.example;

import java.util.ArrayList;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service {

    ArrayList<String> items = null;

    public String ORIG = "";
    private static final String TAG = "MyService";
    public Bundle data = new Bundle();

    @Override
    public IBinder onBind(Intent intent) {

        return null;

    }

    public static String getTag() {
        return TAG;
    }

    @Override
    public void onCreate() {
        Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();

    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onDestroy");

    }

    @Override
    public void onStart(Intent intent, int startid) {
        Log.d(TAG, "onStart");
        data = intent.getExtras();
        ORIG = data.getString("originator");
        Log.d(TAG, "onCreate");
        Thread initBkgdThread = new Thread(new Runnable() {
            public void run() {
                print_result(ORIG);
            }
        });
        initBkgdThread.start();
    }

    public void print_result(String orig) {
        Log.d(TAG, "HELLO WORLD:" + orig);
        int i = 0;
        items = new ArrayList<String>();
        if (items != null) {
            if (items.contains(orig)) {
                Log.d(TAG, "Element already exists exiting");
            } else {

                items.add(orig);

                Log.d(TAG, "Adding Element" + items);

            }

        } else {
            Log.d(TAG, "IS NULL");
        }
    }

}

1 个答案:

答案 0 :(得分:1)

您正在每次调用时在该方法中创建一个新的ArrayList实例。你任何时刻都只有一个元素。

替换

ArrayList<String> items = null;

ArrayList<String> items = new ArrayList<String>();

并从print_result中删除实例化。