- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我制作了一个应用程序,其中列出了我使用 jsoup
从 Html 页面解析的一些链接。
该列表显示链接的标题和应该有链接的箭头。但是当我选择其中的任何一项时,我只会得到最后一个链接。
这是我的 Controller
:
public class Controller extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_view);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
toolbar.setTitle("");
toolbar.setSubtitle("");
linkList = new ArrayList<>();
http = this.getString(R.string.http);
url = this.getString(R.string.path1);
lv = (ListView) findViewById(R.id.list);
new GetLinks().execute();
}
private class GetLinks extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... arg0) {
Document doc;
Elements links;
try {
doc = Jsoup.connect(url).get();
links = doc.getElementsByClass("processlink");
linkList = ParseHTML(links);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
ListAdapter adapter = new SimpleAdapter(
Controller.this, linkList,
R.layout.list_item, new String[]{TITLE, LINK},
new int[]{R.id.title});
//Log.i(TAG, "onPostExecute: "+ getString(R.id.link));
lv.setAdapter(adapter);
}
}
private ArrayList<HashMap<String, String>> ParseHTML(Elements links) throws IOException {
if (links != null) {
ArrayList<HashMap<String, String>> linkList = new ArrayList<>();
for (Element link : links) {
final String linkhref = http + link.select("a").attr("href");
String linktext = link.text();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent,
View view, int position, long id) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse(linkhref));
startActivity(browserIntent);
}
});
HashMap<String, String> student = new HashMap<>();
student.put(TITLE, linktext);
student.put(LINK, linkhref);
linkList.add(student);
}
return linkList;
}
else {
Log.e(TAG, "Couldn't get html from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get html from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
}
这是 main_view.xml
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2" />
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@color/colorPrimary"
android:text="@string/app_name"
android:gravity="center"
android:id="@+id/textView2"
android:layout_below="@+id/toolbar"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
最后但同样重要的是,list_item.xml
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:paddingTop="6dip"
android:textColor="@color/colorPrimaryDark"
android:textSize="16sp"
android:textStyle="bold"
android:text="Title"
android:layout_toLeftOf="@+id/link"
android:layout_toStartOf="@+id/link" />
<ImageView
android:id="@+id/link"
android:layout_width="35dp"
android:layout_height="wrap_content"
android:gravity="end"
android:src="@drawable/arrow"
android:autoLink="web"
android:padding="1dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignBottom="@+id/title" />
如果我记录标题和链接,我会得到所有链接和标题,但不在应用程序中。
所以我的问题是如何让链接作为列表工作,而不仅仅是最后一个链接?
最佳答案
我猜 linkList
已经在全局中声明了。所以你可以接受这个。
无论如何,我很乐意建议您对代码进行一些修改。您可以在 onCreate
函数中完全初始化您的 ListView
。将您的 Adapter
声明为全局变量。
您也可以考虑在 onCreate
函数中设置 ListView
项目的 onClickListener
。
// Declare the adapter globally.
private ListAdapter adapter;
// Declare the linkList as an ArrayList of Student object
private ArrayList<Student> linkList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_view);
// ... Setup toolbar
// ... Setup http and url string
// Setup ListView
linkList = new ArrayList<Student>();
lv = (ListView) findViewById(R.id.list);
// Initialize the adapter
adapter = new SimpleAdapter(
Controller.this, linkList,
R.layout.list_item, new String[]{TITLE, LINK},
new int[]{R.id.title});
// Set the adapter here
lv.setAdapter(adapter);
// Setup the ListView item click
setupListViewItemClick();
// Now execute the AsyncTask
new GetLinks().execute();
}
这是 setupListViewItemClick
函数,它可能看起来像这样。
private void setupListViewItemClick() {
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent,
View view, int position, long id) {
// Get the student by the position of the adapter.
Student student = linkList.get(position);
String linkhref = student.getLink();
Intent browserIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse(linkhref));
startActivity(browserIntent);
}
});
}
因此,您需要修改您的ParseHTML
函数。
private ArrayList<Student> ParseHTML(Elements links) throws IOException {
if (links != null) {
// Removed the initialization of HashMap
for (Element link : links) {
String linktext = link.text();
// ** Removed the onClickListener **
Student student = new Student();
student.setTitle(linktext);
student.setLink(link);
// Just add the items here
linkList.add(student);
}
return linkList;
} else {
// The else part goes here
}
return null;
}
最后,在您的 AsyncTask
中,您需要像这样修改您的 onPostExecute
以通知您的 linkList
已填充数据,因此适配器刷新它。
private class GetLinks extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... arg0) {
Document doc;
Elements links;
try {
doc = Jsoup.connect(url).get();
links = doc.getElementsByClass("processlink");
linkList = ParseHTML(links);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Just call the notifyDatasetChanged to notify the adapter
// to reload the data from the linkList
adapter.notifyDatasetChanged();
}
}
更新
@PanveetSingh 表示,student
不是一个对象,而是一个HashMap
。对不起,我之前错过了那部分。您可能会考虑像这样创建一个名为 Student
的类。
public class Student {
public String title;
public String linkhref;
public Student() {}
public void setTitle(String title) {
this.title = title;
}
public void setLink(Element link) {
this.linkhref = "http://" + link.select("a").attr("href");
}
public String getTitle() {
return this.title;
}
public String getLink() {
return this.linkhref;
}
}
您的ParseHTML
函数和其他函数已相应修改。
关于Android ListView 项目点击不会转到正确的屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41245742/
我有几个系统使用 docker-compose 并且没有问题。 但是,我在这里有一个“向下”根本不做任何事情的地方。 'up'虽然完美。这是在 MacOS 上。 该项目的昵称是“ Storm ”,脚本
解释起来确实很奇怪,所以就这样...... 我正在从 phpmyadmin 获取包含未转义单引号的数据。我正在尝试转换'至'通过使用Content-Type: text/html;在 php
伙计们?在这里需要一些帮助。我使用委托(delegate)协议(protocol)将一些字符串从“第二个 View Controller ”传回给它的前一个。 我的数组附加了我在委托(delegate
我有以下 eval() 东西: c = Customer() eval("c.name = row.value('customer', '{c}')".format(c=column_name), {
我写了这个测试类: @ContextConfiguration(locations = { "classpath:/test/BeanConfig.xml" }) public class Candi
我这样写代码: @ContextConfiguration(locations = { "classpath:/test/BeanConfig.xml" }) @RunWith(SpringJUnit
假设我更改了文件,然后进行 pull 。 Git 会报错,因为本地仓库还没有保存,将被覆盖。如果我然后删除该添加并使文件与以前相同(与远程 repo 相同),那么会发生 pull 吗? 最佳答案 是的
我正在阅读《Java for Dummies》一书,但遇到了问题。我不明白为什么 @Override 不起作用。我确信这与我的代码有关,因为我之前已经获得了一个多态数组来使用覆盖,但它对我来说太简单了
我从我的项目中提取了这段代码,因为我试图找到我犯的一个错误,该错误使我的 BeginStoryboard 无法自行停止。我尽可能地简化了代码,但仍然没有发现问题。你认为它可能是什么?
这个问题在这里已经有了答案: Difference between char[] and char * in C [duplicate] (3 个答案) 关闭 7 年前。 我想我知道自己问题的答案,
我一直在使用 java 的 Scanner 类时遇到问题。我可以让它很好地读取我的输入,但问题是当我想要输出一些东西时。给定多行输入,我想在完全读取所有输入后只打印一行。这是我用来读取输入的代码:
对于这个问题,我已经用最简单的术语表达了这一点。 如果元素被点击,'active'类被添加到元素,'active'类从其他元素中移除。 但是,如果该元素是“事件的”并且它被第二次单击,则“事件”类不应
这会在桌面上创建一个新文件夹,但不会将文件夹 .pfrom 的内容 move 到文件夹 .pTo。 int main() { SHFILEOPSTRUCT sf = {0}; TCHA
我有一个关于多线程调试 DLL (/MDd) 和多线程调试 (/MTd) 设置的问题。它们之间的区别很明显:一个是使用动态库,一个是使用静态库。当我使用/MDd 编译我的程序时,一切都进行得很好。但是
我的问题是,如果我在页面加载时创建一个克隆变量,jQuery 只会 append 它一次。奇怪! Click to copy This is an element! $(document)
所以...我是一个开发 django 应用程序的新手,但是当我尝试通过 virtualbox heroku 运行 heroku run python manage.py syncdb 时,它一直在下面
我在 Spring Boot 初始化时遇到了问题。我在一个简单的 Spring Boot 项目中有这个结构。 com.project.name |----App.java (Annoted with
我在 www.7hermanosmx.com/menu.php 页面上有以下代码 - 一切正常,除了黄色框(类 menuholder)应该每行三个相互 float 。他们坚决拒绝这样做!我知道我做错了
我正在尝试在我正在构建的小型网站上添加一个下拉菜单。出于某种原因,我可以获得我想要向下滑动到 fadeOut() 的 div 并执行其他类似的操作,但我无法将它获取到 slideDown()。我不知道
我有一个不能正确 float 的 div。当您切换可见性时,它会覆盖一些当前文本,但我可以稍后移动它。只是好奇为什么它不能正确 float ! Simple Tabs with CSS &am
我是一名优秀的程序员,十分优秀!