我的主要活动类是这个..我想要的列表项的位置是知道如何针对每个列表项添加浏览器活动。因此,当单击列表中的项目时,它将导航到该网站。如果列表中的Google已点击,则应导航至google.com
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class Main extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
setListAdapter(new MyAdapter(this,android.R.layout.simple_list_item_1,R.id.textView1, getResources().getStringArray(R.array.countries)));
}
private class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, int resource,
int textViewResourceId, String[] strings) {
super(context, resource, textViewResourceId, strings);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.list_item, parent, false);
String[] items = getResources().getStringArray(R.array.countries);
ImageView iv = (ImageView) row.findViewById(R.id.imageView1);
TextView tv = (TextView) row.findViewById(R.id.textView1);
tv.setText(items[position]);
if(items[position].equals("Pakistan")){
iv.setImageResource(R.drawable.apk);
}
else if(items[position].equals("Canada")){
iv.setImageResource(R.drawable.canada);
}
else if(items[position].equals("India")){
iv.setImageResource(R.drawable.india);
}
else if(items[position].equals("UAE")){
iv.setImageResource(R.drawable.uae);
}
return row;
}
}
}
答案 0 :(得分:0)
此代码适用于您的案例,在项目点击时,转到您想要的特定网址
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.google.com"));
startActivity(browserIntent);
}
});
答案 1 :(得分:0)
由于它是ListActivity。覆盖onListItemClick
protected void onListItemClick (ListView l, View v, int position, long id)
Added in API level 1
This method will be called when an item in the list is selected. Subclasses should override. Subclasses can call getListView().getItemAtPosition(position) if they need to access the data associated with the selected item.
Parameters
l The ListView where the click happened
v The view that was clicked within the ListView
position The position of the view in the list
id The row id of the item that was clicked
由于您的布局中有ImageView,因此单击“列表”项时可能会对焦。
然后将此android:descendantFocusability="blocksDescendants"
添加到list_item.xml