简体   繁体   English

Android:如何为扩展MapActivity的类调用ListActivity方法?

[英]Android: How to call ListActivity methods for class that extends MapActivity?

I am trying to create a class that uses both a ListView and MapView. 我正在尝试创建一个同时使用ListView和MapView的类。 I originally wrote the class to extend ListActivity. 我最初编写该类来扩展ListActivity。 However, I later found out that I would need to extend MapView and then reference the ListView if I wanted to combine the two. 但是,后来我发现,如果我想将两者结合起来,就需要扩展MapView,然后引用ListView。

So, this is how is is now declared: 因此,这就是现在的声明方式:

public class MapListActivity extends MapActivity {

Next I figured I needed a way to get at the ListView so I wrote this: 接下来,我想我需要一种获取ListView的方法,所以我这样写:

//instance vars
ListView listView;

//get ref to listview
listView = (ListView) findViewById(android.R.id.list);

In line with the old code I added: 与旧代码一致,我添加了:

    listView.setListAdapter(adapter);

    // selecting single ListView item
    listView.getListView();

However, the error I get is that the method setListAdapter(ListAdapter) is undefined for the type MapListActivity or ListView. 但是,我得到的错误是未为MapListActivity或ListView类型定义方法setListAdapter(ListAdapter)。 This makes sense for the MapView because the class extends MapActivity but I am not sure how to get it to work now that the class no longer extends ListActivity? 这对于MapView很有意义,因为该类扩展了MapActivity,但是由于该类不再扩展ListActivity,现在我不确定如何使它工作?

Any help appreciated. 任何帮助表示赞赏。

Read the ListView documentation 阅读ListView文档

listView.setListAdapter(adapter);

should be 应该

listView.setAdapter(adapter);

Also, 也,

listView.getListView();

doesn't exist for a ListView . ListView不存在。 It is unclear what you want to achieve. 尚不清楚您要实现什么。 If you want to get the item at a position, you can use ListView#getItemAtPosition (int pos) 如果要在某个位置获取项目,则可以使用ListView#getItemAtPosition (int pos)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM