如何在列表视图中获取列表项的位置?
我的代码将包名从.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();
}
}
设置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))));
}
});
@joey从你的代码中,行是你存储包名的ArrayList。所以试着把ArrayList
没有必要让这个全局变量适配器可以用于这个目的 –
这个答案可能会在浏览器中打开您的应用程序链接。要直接打开Google Play商店,请使用'Uri.parse(“market:// details?id =”+ lines.get(position))'代替整个网址。 –
试试下面的代码
try{
InputStream inputreader = getAssets().open("vers.txt");
BufferedReader buffreader = new BufferedReader(new InputStreamReader(inputreader));
/* Enter your code here */
}
使用'OnItemClickListener'特定位置的项目并没有解答问题 – Redman
这里是你的问题代码。我使用此代码实现了相同的功能:
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);
希望这有助于! :)
尽管这个问题已经回答,但我的回答有适当的办法让
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
}
});
也许你应该在你的'ListView' – Karakuri