我是 android 编程的新手。我正在开发一个应用程序,当用户点击任何 ListView 项目时,它应该转到 Google map 应用程序并在 map 上显示该地址的图钉。但是当我点击任何项目时,什么也没有发生。 以下是我的显示 Activity 。
public class DisplayActivity extends Activity implements OnItemClickListener{
ListView listView;
private String tag_name;
public List<NameAddress> nameAddressList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
listView = (ListView) findViewById(R.id.list);
Intent intent = getIntent();
if(intent!= null)
{
tag_name = intent.getStringExtra("DashItemName");
setTitle("List of " +tag_name+ " addresses");
}
nameAddressList = null;
try {
XMLDOMParserHandler parser = new XMLDOMParserHandler(tag_name);
nameAddressList = parser.parseXML(getAssets().open("data.xml"));
ArrayAdapter<NameAddress> adapter =
new ArrayAdapter<NameAddress>(this,R.layout.list_item, nameAddressList);
listView.setAdapter(adapter);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display, menu);
return true;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
// Build the intent
String address = nameAddressList.get(position).toString();
address = "geo:0,0?q=" + address;
String query = URLEncoder.encode(address, "utf-8");
Uri location = Uri.parse(query);
Intent mapIntent = new Intent(Intent.ACTION_VIEW, location;
mapIntent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
// Verify it resolves
PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(mapIntent, 0);
boolean isIntentSafe = activities.size() > 0;
// Start an activity if it's safe
if (isIntentSafe) {
startActivity(mapIntent);
}
else
{
Toast.makeText(this, "Please install google maps app", Toast.LENGTH_LONG).show();
}
}
}
请给我一个解决这个问题的方法。
最佳答案
Please suggest me a way to solve this problem.
它对你不起作用,因为你忘记为 ListView 分配监听器:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
listView = (ListView) findViewById(R.id.list);
listView.setOnItemClickListener(this);
...
}
现在,它会为你工作。
关于Android OnItemCLickListener 在 ListView 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23055276/