Listview 如何通过android studio使用列表视图中的链接打开web浏览器?
我正在尝试创建一个列表视图,其中列出了三个链接到网站的项目。当我从listview中选择一个项目时,它会打开一个web浏览器并将我带到代码中链接的网站。我将三个链接放入一个数组中,并尝试执行一个单击事件来触发浏览器 以下是我的Java代码:Listview 如何通过android studio使用列表视图中的链接打开web浏览器?,listview,android-studio,hyperlink,itemselector,Listview,Android Studio,Hyperlink,Itemselector,我正在尝试创建一个列表视图,其中列出了三个链接到网站的项目。当我从listview中选择一个项目时,它会打开一个web浏览器并将我带到代码中链接的网站。我将三个链接放入一个数组中,并尝试执行一个单击事件来触发浏览器 以下是我的Java代码: package edu.dtcc.bwharto9.intentslab; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import an
package edu.dtcc.bwharto9.intentslab;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
private String[] monthsArray = { "StackOverflow", "Developer.Android", "Javatechig", };
private ListView ListView;
private ArrayAdapter arrayAdapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView = (ListView) findViewById(R.id.months_list);
// this-The current activity context.
// Second param is the resource Id for list layout row item
// Third param is input array
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, monthsArray);
ListView.setAdapter(arrayAdapter);
ListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView parentView, View childView,
int position, long id) {
Intent i = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://stackoverflow.com/"));
startActivity(i);
Intent j = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://developer.android.com/"));
startActivity(j);
Intent k = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://Javatechig.com/"));
startActivity(k);
}
public void onNothingSelected(AdapterView parentView) {
}
});
}
}
如果需要,这里是我的XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/months_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:choiceMode="singleChoice">
</ListView>
</LinearLayout>
提前感谢所有帮助过你的人 在你的听众面前,你应该这样做
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
您应该使用setOnItemClickListener而不是setOnItemSelectedListener