gpt4 book ai didi

java - Arraylist 不起作用,项目不堆叠

转载 作者:行者123 更新时间:2023-12-02 09:02:19 26 4
gpt4 key购买 nike

所以,我试图从 database 获取一些信息并将其保存到 ArrayList ,所以稍后我可以动态添加 buttons根据lengthitems从该列表中。

代码:

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

CautaProbleme方法:

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用于创建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();
}
}

关于java - Arraylist 不起作用,项目不堆叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60074754/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com