Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6869423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:33:21+00:00 2026-05-27T03:33:21+00:00

I am trying to populate an ArrayList called items in a service, however everything

  • 0

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.

How can store strings in a persistant arraylist until the service isDestroyed()

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

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:33:22+00:00Added an answer on May 27, 2026 at 3:33 am

    You are creating a new instance of ArrayList inside that method on every call. You will only have one element at any instant.

    Replace

    ArrayList<String> items = null;
    

    with

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

    and remove the instantiation from print_result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having a problem in trying to pass a ArrayList Object from Activity
I'm trying to populate a List of beans from a form: public class Foo
I am trying to populate a variable with an XML response from an ajax
I'm trying to populate a JSP dropdown from a database table. Here's the code
I'm trying to populate an NSArray with a collection of images in Resources. However,
Hello only have a few hours with Java. (from Python) I am trying to
Trying to populate a uitable from xml, xml is already parsed. for (Row* fighter
I am trying to populate a text box based on the values from a
I'm trying to populate a mx:tree component with values that I'm getting from BlazeDS.
I am trying to populate a string with a double value using a sprintf

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.