D.Bronder
D.Bronder

Reputation: 156

List in list view with onPostExecute

I am working on an application with Jsoup and aSyncTask. I am trying to get a html table in a list view, but I am having trouble with getting the data in the list view. I know I should use the onPostExecute method. The error is that the method is not called so you should use @Override so method will be called, but then I am having problem with calling the list view and setting the list in the list view.

If someone has an alternative solution that will also be appreciated.

This my code:

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

}

This code has no errors when the application is running, but it is not displaying a list with data.

This is my activity class:

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

Upvotes: 0

Views: 1432

Answers (2)

Bhavesh Patadiya
Bhavesh Patadiya

Reputation: 25830

you already setting up your list in your doInBackground() So, you can just passing result string as "Success" or "Failure". and based on that you can do relevant code in your onPostExecute()

According to your structure of AsynsTask your onPostExecute() method should be look like below :

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

In Addition to that, you will have to initiate your context as well in constructor.

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

Also you will need to intiate your Listview on onCreate() of your activity. So, remove this line from onPostExecute() and place it in onCreate().

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

Edit :

inside your activity's onCreate() :

Change

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

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

To

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

Upvotes: 1

Eric B.
Eric B.

Reputation: 4702

The problem is your AsyncTask's result is String, but in your onPostExecute method you are expecting a List. This is the correct code

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

} 

Upvotes: 0

Related Questions