expandDataview android的notifyDataSetChanged

时间:2016-09-14 08:39:27

标签: android android-studio expandablelistview android-adapter notifydatasetchanged

我尝试将新数据添加到数组中,如此示例

enter image description here

这是我的代码

public class MainActivity extends AppCompatActivity {

ExpandableListView expandableListView;
ExpandableListAdapter expandableListAdapter;
List<String> expandableListTitle;
HashMap<String, List<String>> expandableListDetail;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
    expandableListDetail = ExpandableListDataPump.getData();
    expandableListTitle = new ArrayList<String>(expandableListDetail.keySet());
    expandableListAdapter = new CustomExpandableListAdapter(this, expandableListTitle, expandableListDetail);
    expandableListView.setAdapter(expandableListAdapter);
    expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

        @Override
        public void onGroupExpand(int groupPosition) {
            Toast.makeText(getApplicationContext(),
                    expandableListTitle.get(groupPosition) + " List Expanded.",
                    Toast.LENGTH_SHORT).show();
        }
    });

    expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {

        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText edit = (EditText) findViewById(R.id.editText);
                expandableListTitle.add(edit.getText().toString());
                edit.setText("");
                expandableListAdapter.notifyDataSetChanged();
            }
        };

但是,在notifyDataSetChanged();它说&#34;无法解决方法&#39; notifyDataSetChanged();上面有一个红色荧光笔

任何人都可以告诉我我的代码错误以及我应该如何修复它? 谢谢你的帮助

修改

以下是我的customexpandablelistadapter类

public class CustomExpandableListAdapter extends BaseExpandableListAdapter {

private Context context;
private List<String> expandableListTitle;
private HashMap<String, List<String>> expandableListDetail;

public CustomExpandableListAdapter(Context context, List<String> expandableListTitle,
                                   HashMap<String, List<String>> expandableListDetail) {
    this.context = context;
    this.expandableListTitle = expandableListTitle;
    this.expandableListDetail = expandableListDetail;
}

@Override
public Object getChild(int listPosition, int expandedListPosition) {
    return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
            .get(expandedListPosition);
}

@Override
public long getChildId(int listPosition, int expandedListPosition) {
    return expandedListPosition;
}

@Override
public View getChildView(int listPosition, final int expandedListPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {
    final String expandedListText = (String) getChild(listPosition, expandedListPosition);
    if (convertView == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.list_item, null);
    }
    TextView expandedListTextView = (TextView) convertView
            .findViewById(R.id.expandedListItem);
    expandedListTextView.setText(expandedListText);
    return convertView;
}

@Override
public int getChildrenCount(int listPosition) {
    return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
            .size();
}

@Override
public Object getGroup(int listPosition) {
    return this.expandableListTitle.get(listPosition);
}

@Override
public int getGroupCount() {
    return this.expandableListTitle.size();
}

@Override
public long getGroupId(int listPosition) {
    return listPosition;
}

@Override
public View getGroupView(int listPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    String listTitle = (String) getGroup(listPosition);
    if (convertView == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.list_group, null);
    }
    TextView listTitleTextView = (TextView) convertView
            .findViewById(R.id.listTitle);
    listTitleTextView.setTypeface(null, Typeface.BOLD);
    listTitleTextView.setText(listTitle);
    return convertView;
}

@Override
public boolean hasStableIds() {
    return false;
}

   @Override
   public boolean isChildSelectable(int listPosition, int expandedListPosition) {
       return true;
   }
}

这是我的ExpandableListDataPump类,其中我放置了所有数组数据

public class ExpandableListDataPump {
public static HashMap<String, List<String>> getData() {
    HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>();

    List<String> student1 = new ArrayList<String>();
    student1.add("test");
    student1.add("test1");
    student1.add("test2");
    student1.add("test3");
    student1.add("test4");
    student1.add("test");

    List<String> student2 = new ArrayList<String>();
    student2.add("Brazil");
    student2.add("Spain");
    student2.add("Germany");
    student2.add("Netherlands");
    student2.add("Italy");

    List<String> student3 = new ArrayList<String>();
    student3.add("United States");
    student3.add("Spain");
    student3.add("Argentina");
    student3.add("France");
    student3.add("Russia");

    expandableListDetail.put("111", student1);
    expandableListDetail.put("222", student2);
    expandableListDetail.put("333", student3);
    return expandableListDetail;
}

}

2 个答案:

答案 0 :(得分:1)

更改

ExpandableListAdapter expandableListAdapter;

CustomExpandableListAdapter expandableListAdapter;

答案 1 :(得分:0)

您的CustomExpandableListAdapter需要延长BaseExpandableListAdapter