1

我已经扩展了 SherlockMapActivity,这是一个兼容库,允许在更高版本的 android 中找到类似选项卡的界面。问题是我有两个选项卡。Mapview 选项卡和 ListView 选项卡。MapView 工作正常,但列表视图没有。我想要一种方法来实现可扩展列表视图而不扩展可扩展列表活动。扩展列表视图应出现在列表视图选项卡下。现在的问题是 setListAdapter 方法在我的 Code..pls 帮助中未定义。

public class MainActivity extends SherlockMapActivity implements LocationListener,ActionBar.TabListener {

private MapController mapController;
private MapView mapView;    
private LocationManager locationManager;
private GeoPoint currentPoint;
private Location currentLocation = null;
private PlaceOverlay currPos;
private ViewSwitcher tswitcher;

public boolean showMiniHelp = true;

TableLayout tlayout;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.activity_main);

    tswitcher = (ViewSwitcher)findViewById(R.id.viewSwitcher1);


    getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    ActionBar.Tab tab1 = getSupportActionBar().newTab();
    tab1.setText("Map View");
    tab1.setTabListener(this);
    getSupportActionBar().addTab(tab1);

    ActionBar.Tab tab2 = getSupportActionBar().newTab();
    tab2.setText("List View");
    tab2.setTabListener(this);
    getSupportActionBar().addTab(tab2);


    mapView = (MapView)findViewById(R.id.mapView);
    mapView.setBuiltInZoomControls(true);
    mapView.setSatellite(false);      
    mapController = mapView.getController();
    mapController.setZoom(17);
    getLastLocation();
    drawCurrPositionOverlay();
    drawStores();
    drawSportLocations();
    animateToCurrentLocation();

    //ListViewSetup

     // Construct Expandable List
    final String NAME = "name";
    final String IMAGE = "image";
    final String DATA = "data";
    final LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final ArrayList<HashMap<String, String>> headerData = new ArrayList<HashMap<String, String>>();

    final HashMap<String, String> group1 = new HashMap<String, String>();
    group1.put(NAME, "Group 1");
    headerData.add( group1 );

    final HashMap<String, String> group2 = new HashMap<String, String>();
    group2.put(NAME, "Group 2");
    headerData.add( group2);

    final HashMap<String, String> group3 = new HashMap<String, String>();
    group3.put(NAME, "Group 3");
    headerData.add( group3);

    final ArrayList<ArrayList<HashMap<String, Object>>> childData = new ArrayList<ArrayList<HashMap<String, Object>>>();

    final ArrayList<HashMap<String, Object>> group1data = new ArrayList<HashMap<String, Object>>();
    childData.add(group1data);

    final ArrayList<HashMap<String, Object>> group2data = new ArrayList<HashMap<String, Object>>();
    childData.add(group2data);

    final ArrayList<HashMap<String, Object>> group3data = new ArrayList<HashMap<String, Object>>();
    childData.add(group3data);

    // Set up some sample data in both groups
    for( int i=0; i<10; ++i) {
        final HashMap<String, Object> map = new HashMap<String,Object>();
        map.put(NAME, "Child " + i );
        map.put(DATA, "Data " + i);
        map.put(IMAGE, getResources().getDrawable((i%3==0? R.drawable.basketball : R.drawable.supermarket)));
        ( i%2==0 ? group1data : group2data ).add(map);
    }
    final HashMap<String, Object> map = new HashMap<String,Object>();
    map.put(NAME, "Child x");
    map.put(DATA, "Data x");
    map.put(IMAGE, getResources().getDrawable(R.drawable.tennis));
    group3data.add(map);

    setListAdapter( new SimpleExpandableListAdapter(
            this,
            headerData,
            R.layout.group_row,
            new String[] { NAME, DATA },    // the names of the data
            new int[] { R.id.groupname },   // the text field to populate with the field data
            childData,
            0,
            null,
            new int[] {}
        ) {
            @Override
            public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
                final View v = super.getChildView(groupPosition, childPosition, isLastChild, convertView, parent);

                // Populate your custom view here
                ((TextView)v.findViewById(R.id.name)).setText( (String) ((Map<String,Object>)getChild(groupPosition, childPosition)).get(NAME) );
                ((TextView)v.findViewById(R.id.data)).setText( (String) ((Map<String,Object>)getChild(groupPosition, childPosition)).get(DATA) );
                ((ImageView)v.findViewById(R.id.image)).setImageDrawable( (Drawable) ((Map<String,Object>)getChild(groupPosition, childPosition)).get(IMAGE) );

                return v;
            }

            @Override
            public View newChildView(boolean isLastChild, ViewGroup parent) {
                 return layoutInflater.inflate(R.layout.expandable_list_item_with_image, null, false);
            }
        }
    );
    ExpandableListView list = (ExpandableListView) findViewById(android.R.id.list);
    list.setOnChildClickListener(new OnChildClickListener(){
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
            System.out.println("Group:"+groupPosition+", Child: "+childPosition);
            return true;
        }
    });
4

2 回答 2

1

绝对是!甚至我也有类似的问题。

扩展 ExpandableListActivity 只是通过直接为您提供适配器和东西来帮助您。

但是您也可以在布局中定义 ExpandableListView 并手动声明它的所有适配器。

教程将为您提供帮助。

祝你好运!

于 2012-08-31T18:27:45.387 回答
0

是的,我猜它可能!您将不得不使用自定义适配器。您必须创建一个自定义类来扩展 baseadapter 或任何其他适配器类。我不确定你能不能试一试。

如果我在这方面找到一些东西,我会更新你。

于 2012-08-31T18:20:31.517 回答