使用onPostExecute在列表视图中列出

时间:2015-09-28 12:47:14

标签: java android jsoup

我正在使用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);
    }
}

2 个答案:

答案 0 :(得分:0)

您已经在doInBackground()中设置了列表因此,您只需将结果字符串作为&#34;成功&#34;或&#34;失败&#34;。并在此基础上,您可以在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;
    } 

此外,您需要在活动ListviewonCreate()开始。因此,请从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();

答案 1 :(得分:0)

问题是你的AsyncTask的结果是String,但是在你的onPostExecute方法中你需要一个List。这是正确的代码

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

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


public Cluka2(ArrayList<String> list) {
    this.list = list;
}

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

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

@Override
protected List<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;
}


protected void onPostExecute(List<String> list) {
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, list);
    ListView Onderdelen = (ListView) listView.findViewById(R.id.Toernooien);
    Onderdelen.setAdapter(arrayAdapter);
}

}