0

I am trying to add values to array list for ploting xAxis value in mpandroid chart but while debugging array list returns Null

While adding in forloop it shows value but after the end of forloop array list is empty Help me

Code

private ArrayList<String> getXAxisValues() {
final ArrayList<String> xAxis = new ArrayList<>();
final Intent intent = getIntent();
final String getUserId =  intent.getStringExtra("UserId");
new Thread(new Runnable() {
    public void run() {
        HttpRequest req = null;
        try {
            req = new HttpRequest("http://abc.sc.in/abc.php");
            final String response = req.preparePost().withData("answerUserId="+getUserId).sendAndReadString();


            JSONArray myObjects = null;
            try {
                myObjects = new JSONArray(response);

                for (int ji = 1; ji < myObjects.length(); ji++) {
                    JSONObject jsonobject = myObjects.getJSONObject(ji);
                    final String Date = jsonobject.getString("date(answerDate)");
                    System.out.println("Date Response =====>: " + Date);
                   // The Values of Date (2 Dates [21,23]) here are shown while adding
                    xAxis.add(Date); 

                }

            } catch (JSONException e) {
                e.printStackTrace();
            }






        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}).start();


System.out.println("Here It shows [] after adding values ==============================>" + xAxis);

return xAxis;

}

10
  • 2
    actually your thread is not executed and before that you are returning xAxis Commented Nov 24, 2015 at 6:45
  • @user3676184 Is there any possibility of adding return inside thread or after thread execution Commented Nov 24, 2015 at 6:47
  • its your separate method or its in your main class Commented Nov 24, 2015 at 6:49
  • @user3676184 it is a seperate method Commented Nov 24, 2015 at 6:50
  • write your code outside the thread and it will work... Commented Nov 24, 2015 at 6:51

3 Answers 3

2

create AsynchTask task that will return arraylist like

public class DemoAsynch extends AsyncTask<Context, Integer, ArrayList<String>>{

}

After that

ArrayList<String> abc = new DemoAsynch().execute().get();

in abc you will get all list and also pass your id whatever you required to that asynch

Sign up to request clarification or add additional context in comments.

1 Comment

Calling get() method will hang Main Ui Thread which will cause "ANR" Dialog. so call execute() method without get() method and use onPostExecute
2

While adding in forloop it shows value but after the end of forloop array list is empty

Because getXAxisValues() method return value after calling Thread.start() instead of wait until run method execution finish.

Either use Callable and Future classes to get status of running Thread before getting value or in Android, best way is do it using AsyncTask by implementing doInBackground and onPostExecute methods

1 Comment

Upvote for listing possible ways ..I'll replace it with async thanks
2

I am not an Android guru but you missed the concept.

You really need to understand how an asynchronous call works. You are printing values and then the response coming back .

Try printing after the response came

for (int ji = 1; ji < myObjects.length(); ji++) {
                    JSONObject jsonobject = myObjects.getJSONObject(ji);
                    final String Date = jsonobject.getString("date(answerDate)");
                    System.out.println("Date Response =====>: " + Date);
                   // The Values of Date (2 Dates [21,23]) here are shown while adding
                    xAxis.add(Date); 
    System.out.println("Here It shows [] after adding values ==============================>" + xAxis);


                }

2 Comments

How can I do that ? can u modify and show how it will work ?
@vinod I already did. Look at the line System.out.println("Here It shows [] after adding values ==============================>" + xAxis);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.