Java 如何使用JSOUP将标题显示为链接
最近,我尝试使用JSOUP构建自己的android应用程序。我面临着将新闻标题显示为链接的问题。我的意思是,我确实创建了一个显示新闻门户标题的小应用程序,但它只是文本。是否可以将此文本显示为可单击的链接,以便在中导航到文章的web视图页面 以下是代码摘录: `公共类MainActivity扩展了活动{Java 如何使用JSOUP将标题显示为链接,java,android,oracle,parsing,jsoup,Java,Android,Oracle,Parsing,Jsoup,最近,我尝试使用JSOUP构建自己的android应用程序。我面临着将新闻标题显示为链接的问题。我的意思是,我确实创建了一个显示新闻门户标题的小应用程序,但它只是文本。是否可以将此文本显示为可单击的链接,以便在中导航到文章的web视图页面 以下是代码摘录: `公共类MainActivity扩展了活动{ public Elements title; public ArrayList<String> titleList = new ArrayList<String>();
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);
}
}`
公共元素标题;
public ArrayList titleList=新的ArrayList();
专用阵列适配器;
私有ListView lv;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView)findViewById(R.id.listView1);
新建NewThread().execute();
适配器=新阵列适配器(此,R.layout.list_项,R.id.product_名称,标题列表);
}
公共类NewThread扩展异步任务{
@凌驾
受保护的字符串doInBackground(字符串…arg){
文件文件;
试一试{
doc=Jsoup.connect(“http://tech.onliner.by/).get();
title=单据选择(“.b-posts-1-item_uuutitle”);
标题列表。清除();
用于(元素标题:标题){
titleList.add(titles.text());
}
}捕获(IOE异常){
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(字符串结果){
低压设置适配器(适配器);
}
}`
提前感谢设置一个侦听器来处理listview中的单击事件,然后创建一个启动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);
}
});
lv.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
字符串url=…;//url
意图i=新意图(…);//活动、标志。。。
i、 setData(Uri.parse(url));
星触觉(i);
}
});
希望这能有所帮助。我同意eltabo的方法,但是它需要另一个活动来启动url 另一种方法是,在同一活动中单击listview时启动url,而不将其传递给另一个活动以启动webview 这种技术可以使用HashMap实现 1) 将标题和链接添加到HashMap
hashMap.put(title,url);
2) 接下来在listview onClickListener中,我们添加代码以启动相应的url
lv.setOnItemClickListener(new OnItemClickListener() {}
我们可以利用单击的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));
}
@覆盖
公共视图单击(AdapterView arg0、视图arg1、整型arg2、长型arg3){
value=(新建ArrayList(hashMap.values()).get((int)arg3);//根据单击的位置从hashMap检索值(标题的url)
Uri=Uri.parse(value);//这里我们分析url
startActivity(新意图(“android.Intent.action.VIEW”,uri));
}
大家好,欢迎继续!虽然您已经很好地解释了您的问题,但如果您能提供一些代码,解释您尝试过的内容以及遇到的问题,这将非常有帮助。这将有助于其他人为您的问题提供相关且更具体的答案。感谢您的通知。添加了代码示例您能否提供准确的导入方法为了执行这样的代码提取,我应该设置什么&