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

  • SEARCH
  • Home
  • 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 8278769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:07:05+00:00 2026-06-08T09:07:05+00:00

In a Java Class, I have three List as fields in a class. So

  • 0

In a Java Class, I have three List as fields in a class.
So I believe I made them available to the whole class? and all inner classes?

However, this does not work:
(For the example, I will only show one list here, not all three)

class Items extends ListActivity {

List<String> items = null;  // field

Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

            // stuff

    items = new ArrayList<String>();

    new Task().execute();

    Log.d("Size", String.valueOf(items.size()));



 }

 class Task extends AsyncTask<String, String, Void> {

    // in this subclass, I parse items from JSON and add to List "items".

     items = add(item);

 }

Once I exit the task, I seem to lose the items in the List. It Logs “size 0”; if I do it inside the task, it shows the proper amount of items. Am I missing a very basic point about the scope of List variables here?

EDIT: Below is the complete Task class (slightly cleaned up for posting)

class Task extends AsyncTask<String, String, Void> {
    private InputStream is = null;
    private String result = "";


    @Override
    protected Void doInBackground(String... params) {
        String url_select = "items.php";

        param = new ArrayList<NameValuePair>();
        param.add(new BasicNameValuePair("category", Category));

        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url_select);

        try {
            httpPost.setEntity(new UrlEncodedFormEntity(param));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();

            // read content
            is = httpEntity.getContent();

        } catch (Exception e) {

            Log.e("log_tag", "Error in http connection " + e.toString());
        }
        try {
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
            String line = "";
            while ((line = br.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();

        } catch (Exception e) {
            // TODO: handle exception
            Log.e("log_tag", "Error converting result " + e.toString());
        }

        return null;

    }

    protected void onPostExecute(Void v) {

        try {

            JSONArray jArray = new JSONArray(result);
            JSONObject json_data = null;
            for (int i = 0; i < jArray.length(); i++) {
                json_data = jArray.getJSONObject(i);
                item = json_data.getString("item");

                items.add(item);

                Log.d("Items", item);
            }
        } catch (JSONException e1) {
            Toast.makeText(getBaseContext(), "No items!",
                    Toast.LENGTH_SHORT).show();
        } catch (ParseException e1) {
            e1.printStackTrace();
        }


    }
}
  • 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-06-08T09:07:06+00:00Added an answer on June 8, 2026 at 9:07 am

    I am not Android developer but isn’t AsyncTask something like new Thread? If yes then you just see “size 0” because Log.d("Size", String.valueOf(items.size())); was executed before new task().execute(); updated your list.

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

Sidebar

Related Questions

I have a List at java. My class has fields and one of them
I have a base class, which is normal Java class. Three subclasses extend it
I have made a Java class where I have defined a constructor and some
Suppose you have a Java class hierarchy of about 30 classes, with a base
I have three java types as defined below: Main.java: import java.util.Arrays; import java.util.List; public
Have there been incompatibilities between Java releases where Java source code/Java class files targeting
Is there a way for a Java class to have an awareness of its
Is there a reason why java does not have a class which allows both
Referring here A is a precompiled Java class (I also have the source file)
If I have a java class which is package-private (declared with class, not public

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.