我正在尝试创建一个列表视图,其中列出了三个作为网站链接的项目。当我从列表视图中选择一个项目时,它应该打开一个 Web 浏览器并将我带到代码中链接的网站。我已将三个链接放入一个数组中,并尝试执行单击事件来触发浏览器。
这是我的Java代码:
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>
提前感谢所有帮助过的人!