如何在列表视图中获取列表项位置?

1 android listview

我的代码将.txt文件中的包名称填充到 ListView 中,我想让每个列表项在单击时在 Google Play 商店中自行打开。如何获取列表项(包名称)?

startActivity(new Intent(
            Intent.ACTION_VIEW,
            Uri.parse("http://play.google.com/store/apps/details?id="
                    + "com.utorrent.client")));
Run Code Online (Sandbox Code Playgroud)

我的列表视图:

    try {
        String filePath = Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/vers.txt";
        BufferedReader br = new BufferedReader(new InputStreamReader(
                new FileInputStream(filePath), "Cp1252"), 100);

        String line;
        ArrayList<String> lines = new ArrayList<String>();
        while ((line = br.readLine()) != null) {
            lines.add(line);
        }

        br.close();

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, lines);

        listView1.setAdapter(adapter);

    } catch (Exception e) {
        e.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)

pho*_*nix 5

在您的列表视图上设置 onItemClickListener,然后从单击项目的位置从您的数据列表中获取该项目。

 listView.setOnItemClickListener(new OnItemClickListener() {
     @Override
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
         startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id="
                + lines.get(position))));
     }
 });
Run Code Online (Sandbox Code Playgroud)