java - Arraylist 不工作,项目不堆叠
问题描述
因此,我试图从中获取一些信息database
并将其保存到 中ArrayList
,以便稍后我可以buttons
根据该列表中的length
and动态添加。items
代码:
private List<String> probleme = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
if (SharedPrefManager.getInstance(this).isLoggedIn()) {
CautaProbleme();
for(int i=0; i<probleme.size(); i++) {
Button myButton = new Button(this);
myButton.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
myButton.setText(probleme.get(i));
LinearLayout layout = (LinearLayout) findViewById(R.id.linear_home);
layout.addView(myButton);
}
}`
考塔问题方法:
private void CautaProbleme(){
class CautaProbleme extends AsyncTask<Void, Void, String> {
User user = SharedPrefManager.getInstance(getApplicationContext()).getUser();
final String departament = String.valueOf(user.getDepartament());
@Override
protected String doInBackground(Void... voids) {
//creating request handler object
RequestHandler requestHandler = new RequestHandler();
//creating request parameters
HashMap<String, String> params = new HashMap<>();
params.put("departament", departament);
//returing the response
return requestHandler.sendPostRequest(URLs.URL_PROBLEME, params);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
//displaying the progress bar while user registers on the server
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//hiding the progressbar after completion
try {
//converting response to json object
JSONObject obj = new JSONObject(s);
//if no error in response
if (!obj.getBoolean("error")) {
JSONArray problemeArray = obj.getJSONArray("probleme");
for (int i = 0; i < problemeArray.length(); i++) {
JSONObject problema = problemeArray.getJSONObject(i);
// Verificare daca s-au primit probleme trimise de server
if (!problema.isNull("Problema")) {
String situatie = problema.getString("Problema");
probleme.add(situatie);
}
}
} else {
Toast.makeText(getApplicationContext(), "Some error occurred", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
CautaProbleme ru =new CautaProbleme();
ru.execute();
}
JSON
响应没问题,problemeArray
看起来不错,但问题是.add()
命令没有将 添加strings
到列表中,所以for
for 创建buttons
没有做任何事情,有一个空的list
. 我已经private List probleme = new ArrayList<>();
在全球范围内宣布了。
解决方案
AsyncTask
异步执行,因此您必须等到完成执行才能获取数据。更好的选择是在操作完成时使用回调来调用。
步骤 - 1:删除按钮创建代码onCreate
并将其添加到回调中
@Override
protected void onCreate(Bundle savedInstanceState) {
....
if (SharedPrefManager.getInstance(this).isLoggedIn()) {
CautaProbleme();
}
}
在此处添加回调函数和更新逻辑
private void createButtonDynamically() {
LinearLayout layout = (LinearLayout) findViewById(R.id.linear_home);
for (int i = 0; i < probleme.size(); i++) {
Button myButton = new Button(this);
myButton.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
myButton.setText(probleme.get(i));
layout.addView(myButton);
}
}
步骤 - 2:onPostExecute
从操作完成时调用回调
class CautaProbleme extends AsyncTask<Void, Void, String> {
....
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
....
createButtonDynamically();
}
}
推荐阅读
- python - 完成脚本中的断言?
- docker - 卷值'不匹配任何正则表达式:u'^[a-zA-Z0-9._-]+$' - elasticsearch docker-compose中的错误
- c# - 使用实体框架(C#),有没有办法加密字段,以便只有在用户登录时才能解密它们?
- linux - LVM Linux。我的任务中的 LE 是什么意思?可能是错字
- c++ - 基于 C++ QMap 在 QML 中动态创建元素的最佳方法
- excel - chrome 中的检查元素在网站上没有显示任何 id 框,我如何找到它?包含代码
- python - 如何使用 Selenium 和 Python 更改用户代理
- oauth-2.0 - 是否可以集成 2 个具有不同 OAuth2 授权类型的应用程序?
- php - 访问 WordPress index.php 响应网站目录内容文件
- python - python相当于r的??搜索功能