如何从每个ListView项获取URL

时间:2018-03-16 22:52:33

标签: android listview scope jsoup

我有一个ListView,可生成约20篇文章标题。除了每个ListView项都将我带到同一个URL之外,一切正常。我试图找出如何访问每个URL,因为它是在嵌套类中实例化的。 URI在我的setOnItemClickListener

中解析

首先,文章类:

public class Article {

    private String link;

    public Article() {

    }
    public Article(String link) {

        this.link = link;
    }
    public String getLink() {
        return link;
    }
    public void setLink(String link) {
        this.link = link;
    }
    @Override
    public String toString() {
        return this.link;
    }
}

然后是活动:

public class NewsActivity extends AppCompatActivity {

    ListView mListView;
    Document document;
    Elements newsFeed;
    Elements links;
    String href;

    private static final String TAG = "NewsActivity";
    private ArrayList<Article> mArticleList = new ArrayList<>();

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

        HtmlParser htmlThread = new HtmlParser();
        htmlThread.execute();

        mListView = findViewById(R.id.articleListView);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //THE PROBLEM IS THAT EVERY ITEM GOES TO THE SAME URL
                Uri uri = Uri.parse(href);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
            }
        });
    }

    public class HtmlParser extends AsyncTask<Void, Integer, ArrayList<Article>> {
        private static final int NETWORK_NO_ERROR = -1;
        private static final int NETWORK_HOST_UNREACHABLE = 1;
        private static final int NETWORK_NO_ACCESS_TO_INTERNET = 2;
        private static final int NETWORK_TIME_OUT = 3;

        Integer serverError = NETWORK_NO_ERROR;

        ProgressDialog dialog;

        protected void onPreExecute() {
            dialog = new ProgressDialog(NewsActivity.this);
            dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            dialog.setMessage("Retrieving News Feed");
            dialog.show();
        }
        @Override
        protected ArrayList<Article> doInBackground(Void... params) {
            try {

                document = Jsoup.connect("my_url_here").get();

                //get news and titles
                newsFeed = document.getElementsByClass("the_element_class");

                //get links
                links = newsFeed.select("a[href]");

                for(Element link : links) {
                    mArticleList.add(new Article(link.text()));
                    href = links.attr("href"); //tried to get all of the links here
                }
                return mArticleList;
            } catch(ConnectException e) {
                serverError = NETWORK_NO_ACCESS_TO_INTERNET;
                return null;
            } catch (UnknownHostException e) {
                serverError = NETWORK_HOST_UNREACHABLE;
                return null;
            } catch(SocketTimeoutException e) {
                serverError = NETWORK_TIME_OUT;
                return null;
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        } //end doInBackground
        protected void onProgressUpdate(Integer... progress) {

        } // end onProgressUpdate
        protected void onPostExecute(ArrayList<Article> result) {
            if(result != null) {
                ListView listView = findViewById(R.id.articleListView);
                listView.setAdapter(new ArrayAdapter<Article>(NewsActivity.this, R.layout.custom_textview, mArticleList));

                if(dialog.isShowing()) {
                    dialog.dismiss();
                }
            } else {
                switch(serverError) {
                    case NETWORK_NO_ERROR:
                        Toast.makeText(NewsActivity.this, "Oops. Invalid response from server.", Toast.LENGTH_LONG).show();
                        break;
                    case NETWORK_NO_ACCESS_TO_INTERNET:
                        Toast.makeText(NewsActivity.this, "No Internet connection found.", Toast.LENGTH_SHORT).show();
                        break;
                    case NETWORK_TIME_OUT:
                    case NETWORK_HOST_UNREACHABLE:
                        Toast.makeText(NewsActivity.this, "Error in connection.", Toast.LENGTH_LONG).show();
                        break;
                }
            }
        }
    }
} 

0 个答案:

没有答案