使用ExpandableListView而不扩展ExpandableListActivity

时间:2012-08-31 17:57:05

标签: android android-activity expandablelistview

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

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;
        }
    });

2 个答案:

答案 0 :(得分:1)

绝对是!即使我有一个similar problem.

扩展ExpandableListActivity可以直接为您提供适配器和内容。

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

tutorial会对您有所帮助。

祝你好运!

答案 1 :(得分:0)

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

如果我发现了一些问题,我会更新你。