0

I am able to extract the href elements from a page and store the results into a string array. Then display it inside a TextView. The problem comes if I try to display to a ListView. I don't know how to work with ArrayAdapters in this case. This is my working code that displays into a TextView.

package com.example.jsouptestarray;

import java.io.IOException;
import java.util.ArrayList;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.example.jsouptestarray.R;


import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.text.method.ScrollingMovementMethod;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

    TextView text;
    ListView list; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new MyTask().execute();  
    }

    private class MyTask extends AsyncTask<Void, Void, ArrayList<String>> {

        ArrayList<String> arr_linkText=new ArrayList<String>();

        @Override
        protected ArrayList<String> doInBackground(Void... params) {

             Document doc;
             String linkText = ""; 

             try {
                 doc = Jsoup.connect("https://www.google.com/").get();
                 Elements links = doc.getElementsByTag("a");
                 for (Element el : links) { 
                     linkText = el.attr("href");
                     arr_linkText.add(linkText); // add value to ArrayList
                 }
             } catch (IOException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
             }

             return arr_linkText;     //<< retrun ArrayList from here
        } 


        @Override
        protected void onPostExecute(ArrayList<String> result) {        

            // get all value from result to display in TextView
            for (String temp_result : result) {
                 System.out.println("links :: "+temp_result);
                 text = (TextView) findViewById (R.id.textView2);
                 text.append(temp_result + "\n" + "\n");
                 text.setMovementMethod(new ScrollingMovementMethod());
            }
        }

    }
}

This is my attempt with the ListView, but I get an error and I don't understand how to fix it.

list = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
          android.R.layout.simple_list_item_1, android.R.id.text1, temp_result);

// Assign adapter to ListView
list.setAdapter(adapter); 

Here is a screenshot of the error

enter image description here

I hope someone can help me fix it, and I appreciate your time!

2 Answers 2

2

Fourth element of the constructor is excess in your case. It can be used to set data at once, you should see API. Try this:

list = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> adapter = 
    new ArrayAdapter<String>(MainActivity.this, 
                             android.R.layout.simple_list_item_1, 
                             android.R.id.text1);

for (String temp_result : result)
{
   adapter.add(temp_result);
}

// Assign adapter to ListView
list.setAdapter(adapter); 
Sign up to request clarification or add additional context in comments.

1 Comment

updated the answer, I forgot about MainActivity.this as mentioned in another comment
2

Replace this with MainActivity.this. When you're inside the AsyncTask, this refers to the AsyncTask instance, which can't be passed to the constructor (different, unrelated, incompatible classes)

So you need to call an Activity as the argument and since your AsyncTask is part of an Activity, you can explictly reference the Activity instance with ClassName.this.

1 Comment

Combine our answers, take out one argument and use MainActivity.this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.