Arraylist 不工作,项目不堆叠
Arraylist not working, items do not stack up
所以,我试图从 database
中获取一些信息并将其保存到 ArrayList
中,因此稍后我可以根据 length
动态添加 buttons
和该列表中的 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);
}
}`
Cauta问题方法:
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
添加到列表中,所以用于创建 buttons
的 for
没有做任何事情,有一个空的 list
。我已在全球范围内声明 private List probleme = new ArrayList<>();
。
CautaProbleme 运行 在您继续 for
循环时在后台出现。
您应该将带有 onReady()
方法的接口发送到 ASyncTask
并从 onPostExecute
.
调用它
然后,在你实施 (Activity) onReady()
的地方,你可以做
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);
}
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();
}
}
所以,我试图从 database
中获取一些信息并将其保存到 ArrayList
中,因此稍后我可以根据 length
动态添加 buttons
和该列表中的 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);
}
}`
Cauta问题方法:
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
添加到列表中,所以用于创建 buttons
的 for
没有做任何事情,有一个空的 list
。我已在全球范围内声明 private List probleme = new ArrayList<>();
。
CautaProbleme 运行 在您继续 for
循环时在后台出现。
您应该将带有 onReady()
方法的接口发送到 ASyncTask
并从 onPostExecute
.
然后,在你实施 (Activity) onReady()
的地方,你可以做
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);
}
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();
}
}