我一直在尝试创建一个ExpandableListView,并使用我在MyDataProvider类中创建的HashMap为其提供组和子组。我几乎成功了,但我无法弄清楚为什么我在设置适配器时遇到此错误。
这是我的班级,将显示列表:
package com.gmd.referenceapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import com.gmd.referenceapplication.R;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class CommonConstants extends AppCompatActivity {
View view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_common_constants);
MyDataProvider dp = new MyDataProvider();
view= findViewById(R.id.expandableList);
HashMap constantsHashMap;
constantsHashMap = dp.getCommonMap();
ArrayList constantsHashMapKeys = new ArrayList<String>(constantsHashMap.keySet());
MyCustomAdapter adapter = new MyCustomAdapter(this, constantsHashMap, constantsHashMapKeys);
view.setAdapter(adapter); //error here
setContentView(view);
}
}
这是我的适配器:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by gmd on 6/13/2016.
*/
public class MyCustomAdapter extends BaseExpandableListAdapter {
private Context context;
private HashMap<String,ListViewItem> constantsHashMap;
private List<String> constantList;
public MyCustomAdapter(Context context, HashMap<String, ListViewItem> hashMap, List<String> list){
constantsHashMap = hashMap;
this.context = context;
this.constantList = list;
}
public View getChildView(int groupPosition,
int childPosition,
boolean isLastChild,View convertView, ViewGroup parent) {
String childTitle = (String) getChild(groupPosition, childPosition);
if(convertView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.child_layout, parent, false);
}
TextView childTextView = (TextView) convertView.findViewById(R.id.textViewChild);
childTextView.setText(childTitle);
return convertView;
}
这是错误:
Error:(30, 13) error: cannot find symbol method setAdapter(MyCustomAdapter)
我确定这是一个基本的错误,但我做了一些相当广泛的谷歌搜索并没有找到任何东西,所以我会感激任何和所有的帮助。谢谢!
编辑:
以下是此活动的xml。我试图用视图变量引用的视图是这里的ExpandableListView,其id为“expandableList”
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.gmd.referenceapplication.CommonConstants">
<ExpandableListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:dividerHeight="@dimen/activity_vertical_margin"
android:indicatorLeft="@dimen/activity_vertical_margin"
android:id="@+id/expandableList" />
</LinearLayout>