Agustin Scalisi
Agustin Scalisi

Reputation: 551

How parse li class in a ListView Using Jsoup

I am parsing a web page http://abcsur.info/clasificados/inmuebles/casas, the page is refreshed and change every week. I want to display the ads on [li class#li.list-group-items]. enter image description here

My idea is to add this li classes to a List View. After search in several sites, i write this code, but the app crash (NullPointerException)

public class Casas extends Activity {

public Casas(){}
// URL Address
String url = "http://abcsur.info/clasificados/inmuebles/casas";
ProgressDialog mProgressDialog;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.casas);

            new Title().execute();


}

// Title AsyncTask
private class Title extends AsyncTask<Void, Void, Void> {


    List<String> items;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog = new ProgressDialog(Casas.this);
        mProgressDialog.setTitle("ABC sur");
        mProgressDialog.setMessage("Cargando...");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        try {
            Document document = Jsoup.connect(url).get();
            Elements li = document.select("ul.list-group");
            List<String> items = new ArrayList<String>();
            for (Element item : li) {
                items.add(item.text());
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        ListView lv = (ListView) findViewById(R.id.listView);
        ArrayAdapter adaptador = new ArrayAdapter<String>(Casas.this,
                android.R.layout.simple_list_item_1, items);
        lv.setAdapter(adaptador);

        mProgressDialog.dismiss();
    }
}

}

Any idea?

Logcat

   Process: info.androidhive.abcsur, PID: 2923
   java.lang.NullPointerException: Attempt to invoke interface method 'int   java.util.List.size()' on a null object reference
        at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
        at android.widget.ListView.setAdapter(ListView.java:487)
        at info.androidhive.abcsur.InmueblesFolder.Casas$Title.onPostExecute(Casas.java:92)
        at info.androidhive.abcsur.InmueblesFolder.Casas$Title.onPostExecute(Casas.java:49)
        at android.os.AsyncTask.finish(AsyncTask.java:636)
        at android.os.AsyncTask.access$500(AsyncTask.java:177)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5257)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

After Update (Works!)

 public class Casas extends Activity {

public Casas(){}
// URL Address
String url = "http://abcsur.info/clasificados/inmuebles/casas";
ProgressDialog mProgressDialog;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.casas);

            new Title().execute();


}

// Title AsyncTask
private class Title extends AsyncTask<Void, Void, Void> {

    String lii;
    List<String> items;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog = new ProgressDialog(Casas.this);
        mProgressDialog.setTitle("ABC sur");
        mProgressDialog.setMessage("Cargando...");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.show();
    }

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

        try {
            Document document = Jsoup.connect(url).get();
            Elements li = document.select("li.list-group-items");
            items = new ArrayList<String>();
            for (Element item : li) {
                items.add(item.text());
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        ListView lv = (ListView) findViewById(R.id.listView);
        ArrayAdapter adaptador = new ArrayAdapter<String>(Casas.this,
                android.R.layout.simple_list_item_1, items);
        lv.setAdapter(adaptador);
        adaptador.notifyDataSetChanged();
        mProgressDialog.dismiss();
    }
}

}

And the XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/listView" />
</RelativeLayout>

Upvotes: 0

Views: 276

Answers (1)

Yuva Raj
Yuva Raj

Reputation: 3881

You declared List<String> items; already before onPreExecute. Also you declared and initiated the same ArrayList in doInBackground.

List<String> items = new ArrayList<String>();

When you add items to ArrayAdapter on onPostExecute(), it refers to the top ArrayList items NOT the ArrayList items in doInBackground.

Since it refers the top ArrayList which you haven't initiated, that gives you the error java.util.List.size() on a null object reference.

So, just change,

List<String> items = new ArrayList<String>();

to,

items = new ArrayList<String>();

in doInBackground.

Upvotes: 2

Related Questions