我正在使用Jsoup和aSyncTask开发应用程序。我试图在列表视图中获取html表,但是在列表视图中获取数据时遇到了麻烦。我知道我应该使用onPostExecute方法。错误是未调用该方法,因此您应使用@Override以便将调用该方法,但随后我在调用列表视图并在列表视图中设置列表时遇到问题。

如果有人有替代解决方案,也将不胜感激。

这是我的代码:

public class Cluka2 extends AsyncTask<Void, Void, String> {

    Document document = null;
    public List<String> list = new ArrayList<>();
    ListView listView = null;
    Context context = null;


    public Cluka2(ArrayList<String> list,Context mContext) {
        this.list = list;
        this.context= mContext;
    }

    public List<String> getList() {
        return list;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

    @Override
    protected String doInBackground(Void... params) {

        try {
            document = Jsoup.connect("https://tennisnaarden.planmysport.com/portal/page/pmsportal30/TVNaarden/Toernooien/Clubtoernooi").get();

            Elements elements = document.select("#pcnt1383_8158836_1383_4326089_4326089 td:first-child");

            for (int i = 0; i < elements.size(); i++) {

                list.add(elements.get(i).text());
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return list.toString();
    }

    @Override
    protected void onPostExecute(String result) {
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, list);
        Toernooien.setAdapter(arrayAdapter);
    }

}


当应用程序运行时,此代码没有错误,但未显示包含数据的列表。

这是我的活动课:

public class ClubkampioenschappenSingleenDubbel extends AppCompatActivity {

    ArrayList<String> list = new ArrayList<>();
    Context context;
    Document doc = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clubkampioenschappen_singleen_dubbel);
        this.setTitle("Clubkampioenschappen Single en Dubbel");

        new Cluka2(list, context).execute();

        ListView Toernooien = (ListView) findViewById(R.id.Toernooien);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_clubkampioenschappen_singleen_dubbel, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

最佳答案

您已经在doInBackground()中设置了列表,因此,只需将结果字符串作为“成功”或“失败”传递即可。然后您就可以在onPostExecute()中执行相关代码

根据您的AsynsTask结构,您的onPostExecute()方法应如下所示:

@override
protected void onPostExecute(String result) {
            ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, list);
            Onderdelen.setAdapter(arrayAdapter);
        }


除此之外,您还必须在构造函数中初始化上下文。

public Cluka2(ArrayList<String> list,Context mContext) {
        this.list = list;
        this.context=mContext;
    }


另外,您还需要在活动的Listview上添加onCreate()。因此,从onPostExecute()删除此行并将其放在onCreate()中。

 ListView Onderdelen = (ListView) listView.findViewById(R.id.Toernooien);


编辑:

在活动的onCreate()中:

更改

 new Cluka2(list, context).execute();

        ListView Toernooien = (ListView) findViewById(R.id.Toernooien);




 ListView Toernooien = (ListView) findViewById(R.id.Toernooien);
 new Cluka2(list, ClubkampioenschappenSingleenDubbel.this).execute();

10-07 19:27
查看更多