English 中文(简体)
无法加固具有新服务价值的ArrayList
原标题:Unable to increment ArrayList with new values in Service
  • 时间:2011-11-23 15:01:05
  •  标签:
  • java
  • android

I am trying to populate an ArrayList called items in a service, however everything I call service from my activity only the first element is populated into the array. I am not sure what is the best way to do this. I heard about using bind service methods but from what I read this is not adviced.

• 如何在服务中断之前将体积储存在顽固的阵列中

Thanks in advance,

PROBLEM CODE

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

Graham

ACTIVITY

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

SERVICE

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

}
最佳回答

你们正在根据每次呼吁,在这种方法内建立一个新的ArrayList。 你在任何时候都只有一个要素。

替换

ArrayList<String> items = null;

iii

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

并删除印本的瞬时状态。

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签