Unable to display my parsed data in listview

I’m trying parse url for div classes, but i’m unable to display all of them. I was able to pull the first one using jsuop.

<div class="k_block">Text 1 is here</div>
<div class="k_block">Text 2 is here</div>
<div class="k_block">Text 3 is here</div>
<div class="k_block">Text 4 is here</div>
<div class="k_block">Text 5 is here</div>

Here is my code:

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        new Look().execute();

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    class Look extends AsyncTask<Void, Void, Void> {

        String title;
        Document doc;
        String[] array;

        @Override
        protected void onPreExecute() {    }

        @Override
        protected Void doInBackground(Void... params) {
            try {
                doc = Jsoup.connect("url here").get();
                title = doc.select("div.k_block").text();
                array = title.split("\.\.\. ");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {

            ArrayAdapter<String> adptr =
                    new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, array);
            ListView listView = (ListView) findViewById(R.id.mylil);
            listView.setAdapter(adptr);

        }
    }

I know there is an error here -> ArrayAdapter adptr = new ArrayAdapter(this, android.R.layout.simple_list_item_1, array); but i don’t know what to do.

Thank you for taking your time. I appreciate it.

Answer

Use MainActivity.this instead of this. Try this:

ArrayAdapter<String> adptr =
                    new ArrayAdapter<String>(MainActivity.this, 
                                 android.R.layout.simple_list_item_1,
                                 array);

Leave a Reply

Your email address will not be published. Required fields are marked *