Android ListView项目点击不';不要转到右屏幕

Android ListView项目点击不';不要转到右屏幕,android,jsoup,Android,Jsoup,我制作了一个应用程序,其中列出了我使用jsoup从Html页面解析的一些链接 该列表显示链接的标题和应包含链接的箭头。但当我选择其中任何一项时,我只得到最后一个链接 这是我的控制器: public class Controller extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstan

我制作了一个应用程序,其中列出了我使用
jsoup
从Html页面解析的一些链接

该列表显示链接的标题和应包含链接的箭头。但当我选择其中任何一项时,我只得到最后一个链接

这是我的
控制器

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;
    }
}
最后但并非最不重要的是,
list\u item.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" />
<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" />

如果我记录标题和链接,我会得到所有的链接和标题,但不会在应用程序中

因此,我的问题是如何让链接作为列表而不仅仅是最后一个链接工作?

ParseHTML(Elements-links)
方法中

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent,
                                    View view, int position, long id) {
                String theRealLink = linkList.get(position);
                Intent browserIntent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse(theRealLink));
                startActivity(browserIntent);
            }
        });
lv.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
公共无效MClick(AdapterView父级、,
视图、整型位置、长id){
String theRealLink=linkList.get(位置);
Intent browserIntent=新的意图(Intent.ACTION\u视图,
parse(theRealLink));
startActivity(浏览器内容);
}
});
链接列表还需要是
final


(还要记住,在Java中,
methodnameshouldbeinthisformat()

我想,
链接列表已经在global中声明了。所以你可以接受这个

无论如何,我很乐意建议您对代码进行一些编辑。您可以在
onCreate
函数中完全初始化
列表视图。将
适配器
声明为全局变量

您也可以考虑设置< <代码> OnCLListListNo/<代码> >代码> >代码> > OnCudio函数。

// 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();
}
因此,您需要修改
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;
}
更新

@PanveetSingh说,
student
不是一个对象,而是一个
HashMap
。对不起,我刚才错过了那部分。您可以考虑创建一个名为“代码>学生<代码>的类。p>
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
函数和其他函数将相应地修改

student
是一个hashmap,
student.getLink()
不起作用,你需要在hashmap中迭代两次才能得到链接字符串,代码需要大量的修复,之前没有看到。谢谢你的帮助。希望这是好的去了。你现在可以看一看,再告诉我这里有什么问题。:)非常感谢,伙计:)很好,但是更改ArrayList后适配器不工作我只是收到一个错误,说它需要java…java.util.Map>,并且在适配器中将链接列表更改为(List>)linkList没有帮助,并且没有notifyDatasetChanged(),在学生类中getTitle()和getLink()应该是字符串而不是空的。但是我不知道如何修复适配器,您可能需要编写自定义适配器。互联网上有很多关于这方面的资源。你的代码需要大量修复,遵循最佳实践,使用POJO类或维护字符串列表,我的建议是首先遵循一些教程
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();
    }
}
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;
    }
}