无法实例化ExpandableListAdapter,因为它是抽象的

时间:2018-09-25 14:40:55

标签: java android abstract expandablelistadapter

我正在尝试使用互联网上的代码,但是将所述代码插入我的个人项目时,出现以下错误:

  

“ ExpandabelListAdapter”是抽象的;无法实例化

我试图做自己的研究来解决我的问题,但我仍然无法解决上述问题...

这是我的代码:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MainActivity extends AppCompatActivity {

private ExpandableListView listView;
private ExpandableListAdapter AdapterMedia;
private List<String> listDataHeader;
private HashMap<String,List<String>> listHash;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView = (ExpandableListView)findViewById(R.id.);
    initData();


    AdapterMedia = new ExpandableListAdapter(this, listDataHeader, listHash);


    listView.setAdapter(AdapterMedia);
}

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:0)

您需要创建一个自定义的Adapter类,例如,名为“ MyExpandableListAdapter”,然后在该类中实现ExpandableListAdapter。您还需要实现接口中声明的所有方法。

public class MyExpandableListAdapter implements ExpandableListAdapter {
   @Override
   public void registerDataSetObserver(DataSetObserver observer) {...}
}

在Internet上查看教程:https://www.javacodegeeks.com/2013/06/android-expandablelistview-with-custom-adapter-baseexpandablelistadapter.html

答案 1 :(得分:0)

@deHaar是正确的,ExpandableListAdapter一个界面,不能直接实例化。我将检查此tutorial以获得更多信息。

此外,作为一般注释,findViewById(R.id.<id>;)需要从相应的<id>布局:XML

中定义的关联的setContentView(R.layout.<layout>);

希望有帮助!

相关问题