Java Arraylist不工作,项不堆叠

Java Arraylist不工作,项不堆叠,java,android,arraylist,android-asynctask,Java,Android,Arraylist,Android Asynctask,因此,我试图从数据库中获取一些信息,并将其保存到数组列表中,以便稍后我可以根据该列表中的长度和项添加友好的按钮 代码: private List<String> probleme = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.la

因此,我试图从
数据库中获取一些信息
,并将其保存到
数组列表中
,以便稍后我可以根据该列表中的
长度和
项添加友好的
按钮

代码:

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 List probleme=new ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
if(SharedPrefManager.getInstance(this.isLoggedIn()){
尾问题();

for(int i=0;iCautaProbleme在后台运行,同时继续执行
for
循环。 您应该将带有
onReady()
方法的接口发送到
ASyncTask
并从
onPostExecute
调用它

然后,在实现(活动)
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);
    }

for(int i=0;i
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);
    }
}

当你用大写字母开始一个方法名称时,对于有经验的开发人员来说,这会使阅读变得更加困难。请遵循推荐的命名约定,现在就开始工作。事实上,我没有想过要等到
AsyncTask
完成。
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);
    }
}
class CautaProbleme extends AsyncTask<Void, Void, String> {

    ....

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);

        ....

        createButtonDynamically();
    }
}