简体   繁体   English

如何使用JSOUP将标题显示为链接

[英]How to show titles as links using JSOUP

Recently I was trying to build my own android app using JSOUP. 最近,我试图使用JSOUP构建自己的android应用。 I faced with the problem of showing titles from news as links. 我面临着将新闻标题显示为链接的问题。 I mean I did created small application which shows the headlines of news portal, but it is only text. 我的意思是我确实创建了一个小型应用程序,用于显示新闻门户的标题,但这只是文本。 Is it possible to show this text as clickable links, so that in will navigate to the web view page of an article? 是否可以将这些文本显示为可单击的链接,以便将其导航到文章的Web视图页面?

Here is the extract from the code: 以下是代码摘录:

`public class MainActivity extends Activity { 公共类MainActivity扩展了Activity {

public Elements title;

public ArrayList<String> titleList = new ArrayList<String>();
private ArrayAdapter<String> adapter;
private ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lv = (ListView) findViewById(R.id.listView1);
    new NewThread().execute();
    adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, titleList);
}


public class NewThread extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... arg) {


        Document doc;
        try {
            doc = Jsoup.connect("http://tech.onliner.by/").get();
            title = doc.select(".b-posts-1-item__title");
            titleList.clear();
            for (Element titles : title) {
                titleList.add(titles.text());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        lv.setAdapter(adapter);
    }
}`

Thanks in advance 提前致谢

Set an OnItemClickListener to handle click events in your listview, then create an Intent to launch your webview activity passing the url you want view as data. 设置一个OnItemClickListener来处理列表视图中的单击事件,然后创建一个Intent以启动您的Webview活动,并将您想要查看的url作为数据传递。

  lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String url = ...; //The url
        Intent i= new Intent(...); //The activity, flags...
        i.setData(Uri.parse(url));

        startActivity(i);
      }
    });

Hope this helps. 希望这可以帮助。

I agree with eltabo's approach however it requires another activity to launch the url 我同意eltabo的方法,但是它还需要另一个活动来启动url

A different approach would be to launch the url on clicking the listview from the same activity without passing it to another one for webview launch 一种不同的方法是在单击同一活动中的listview时启动url,而不将其传递给另一个进行webview启动的URL

This technique can be implemented using HashMap 可以使用HashMap实现此技术

1) Add the title and the link to HashMap 1)将标题和链接添加到HashMap

hashMap.put(title,url);

2) Next in the listview onClickListener we add the code to launch the corresponding url 2)接下来,在listview onClickListener中,我们添加代码以启动相应的url

lv.setOnItemClickListener(new OnItemClickListener() {}

we can make use of the position of the listview item clicked and use it to retrieve the corresponding url from the HashMap 我们可以利用单击的listview项的位置,并使用它从HashMap中检索相应的url。

@Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
                        value=(new ArrayList<String>(hashMap.values())).get((int)arg3); // based on position clicked retrieve the value ( the url for the title) from hashMap
                        Uri uri=Uri.parse(value); // here we parse the url
                        startActivity(new Intent("android.intent.action.VIEW", uri));
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM