如何启动与ListView关联的特定URL [英] How to launch specific URL associated with ListView
问题描述
我在弄清楚如何从特定列表视图项启动特定网站时遇到了麻烦.现在,我可以按预期运行它,但只能在google.com上启动.我将如何进行编码以启动3个站点的特定URL?任何帮助表示赞赏.谢谢.麻生注释掉了一个祝酒词,当单击一个特定项目时会显示该祝酒词.
I am having trouble figuring out how to launch a specific website from a particular listview item. Right now i have it working as intended but only launching to google.com. How would i go about coding this to launch the 3 sites particular urls? any help appreciated. Thanks. Aso commented out a toast that would display when a specific item is clicked which worked fine.
MainActivity:
MainActivity:
public class MainActivity extends AppCompatActivity {
//lets declare an array of icons that matches the order of descriptions
public static int[] images = {R.drawable.bbcnews, R.drawable.reddit, R.drawable.sciencemag};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//lets get an array of descriptions from xml (string array resource)
String[] descriptions = getResources().getStringArray(R.array.description_array);
//in order to set up or listview we also need an ArrayAdapter
//this Adaptor needs to use our custom_list layout.
//Therefore we need to create a custom Arrayadaptor class
//get a reference to our ListView so we can associate it with our custom ArrayAdapter
ListView listView = (ListView) findViewById(R.id.listView);
//create a new MyCustomAdapter
MyCustomAdapter myCustomAdapter = new MyCustomAdapter(this, descriptions, images);
//connect the ListView with myCustomAdapter
listView.setAdapter(myCustomAdapter);
}
}
适配器:
public class MyCustomAdapter extends BaseAdapter {
//lets declare some instance variables to hold essential data
String[] descriptions;
int [] images;
Context context;
//constructor to set up our instance variables
public MyCustomAdapter(MainActivity c, String[] d, int[] i){
context = c;
descriptions = d;
images = i;
}
@Override
public int getCount() {
return descriptions.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
//the android framework will call getView everytime it needs to render your listview
// int position indicates which row the framework is trying to draw
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row; // a reference to refer to each row
//layoutInflator is a class that creates a java obj from the xml layout
//we get a layoutINflator from the framework by calling getSystemService
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
//use the layoutInflater to create a new View of the correct type (custome_list)
row = inflater.inflate(R.layout.custom_list, null);
//get the textview and set its text
TextView textView = (TextView) row.findViewById(R.id.textView);
textView.setText(descriptions[position]);
//get the img view and set its img icon
ImageView imageView = (ImageView) row.findViewById(R.id.imageView);
imageView.setImageResource(images[position]);
//we need to add an OnClickListener to respond to user clicks!
row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(context, descriptions[position], Toast.LENGTH_LONG).show();
Uri uri = Uri.parse("http://www.google.com"); // missing 'http://' will cause crashed
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
context.startActivity(intent);
}
});
return row;
}
}
Strings.xml:
Strings.xml:
<resources>
<string name="app_name">My Fave Sites</string>
<string-array name="description_array">
<item>Get your world news here</item>
<item>Reddit is a sweet forum</item>
<item>Cool science stuff!</item>
</string-array>
<string-array name="links">
<item>http://www.bbc.com/</item>
<item>https://www.reddit.com/</item>
<item>http://www.bbc.com/</item>
</string-array>
</resources>
推荐答案
这可以做到.
@Override
public void onClick(View v) {
String[] links = context.getResources().getStringArray(R.array.links);
String url = links[position];
Uri uri = Uri.parse(url); // missing 'http://' will cause crashed
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
context.startActivity(intent);
}
不过有两个建议:
-
您可能会考虑将
String [] links = context.getResources().getStringArray(R.array.links);
部分移到适配器构造函数中,因为它不会在每个适配器上都改变点击.
You may consider moving
String[] links = context.getResources().getStringArray(R.array.links);
part to your adapter constructor as it's not going to change on every click.
您可以改用 ListView.setOnItemClickListener
并处理片段或活动中的click事件,因为适配器类旨在仅处理数据部分而不是逻辑.
You can use ListView.setOnItemClickListener
instead and handle the click event in your fragment or activity as the adapter class is designed to handle only the data part not the logic.
这篇关于如何启动与ListView关联的特定URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!