2017-02-18 116 views
0

我的代码将包名从.txt文件填充到ListView中,并且我希望单击时在Google Play商店中打开每个列表项。如何获取列表项(包名)?如何在列表视图中获取列表项的位置?

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

我的列表视图:

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(); 
    } 
} 
+1

也许你应该在你的'ListView' – Karakuri

回答

3

设置onItemClickListener你的列表视图,然后从点击项目的位置从你的DataList该项目。

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)))); 
    } 
}); 
+0

@joey从你的代码中,行是你存储包名的ArrayList。所以试着把ArrayList lines = new ArrayList <>();作为一个全球变量。 –

+0

没有必要让这个全局变量适配器可以用于这个目的 –

+0

这个答案可能会在浏览器中打开您的应用程序链接。要直接打开Goog​​le Play商店,请使用'Uri.parse(“market:// details?id =”+ lines.get(position))'代替整个网址。 –

0

试试下面的代码

try{ 
    InputStream inputreader = getAssets().open("vers.txt"); 
    BufferedReader buffreader = new BufferedReader(new InputStreamReader(inputreader)); 

    /* Enter your code here */ 
} 
+0

使用'OnItemClickListener'特定位置的项目并没有解答问题 – Redman

1

这里是你的问题代码。我使用此代码实现了相同的功能:

listView.setOnItemClickListener(
      new AdapterView.OnItemClickListener(){ 
       @Override 
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
        String recipes = String.valueOf(parent.getItemAtPosition(position)); 
        Toast.makeText(Main22Activity.this, recipes, Toast.LENGTH_LONG).show(); 
        Intent intent = new Intent(android.content.Intent.ACTION_VIEW); 
        intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=my packagename ")); 
        startActivity(intent); 

希望这有助于! :)

0

尽管这个问题已经回答,但我的回答有适当的办法让

mListView.setOnItemClickListener(new OnItemClickListener() { 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     Object obj = mListView.getAdapter().getItem(position); 
     //Now this object has everything you need to get 

    } 
}); 
相关问题