我正在尝试将值添加到数组列表中,以便在 mpandroid 图表中绘制 xAxis 值,但在调试数组列表时返回 Null
在 forloop 中添加时,它显示值,但在 forloop 数组列表结束后为空帮助我
代码
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;
}
最佳答案
创建将返回arraylist
的AsynchTask
任务
喜欢
public class DemoAsynch extends AsyncTask<Context, Integer, ArrayList<String>>{
}
之后
ArrayList<String> abc = new DemoAsynch().execute().get();
在 abc 中,您将获得所有列表,并将您的 id 传递给该异步所需的任何内容
关于java - Android 数组列表添加值后返回 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33887058/