Grace D Grace D - 5 months ago 26
Android Question

I'm trying to populate an ExpandableListView, why am I getting an error trying to set my custom adapter?

I have been attempting to create an ExpandableListView and give it groups and children using a HashMap I created in the MyDataProvider class. I'm almost successful, but I can't figure out why I am getting this error on setting an adapter.

Here is my Class that will show the list:

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


Here is my Adapter:

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;


}


Here is the error:

Error:(30, 13) error: cannot find symbol method setAdapter(MyCustomAdapter)


I'm sure it's a basic mistake, but I've done some fairly extensive googling and found nothing, so I'd appreciate any and all help. Thanks!

EDIT:

Here is the xml for this activity. The view I am trying to reference with the view variable is the ExpandableListView here, with the 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>

Answer

The View that you try to use like a ListView does not have a method called setAdapter. The View is the super class of views you has in your android projects. You need to use a ListView instead of View.

Just if you are curious like me, the setAdapter method is added in android.widget.AdapterView That is son of ViewGroup, and ViewGroup is son of View.

enter image description here

First thing, change the like

View view;

to

ListView listView;

Remove this

setContentView(view);

and set the adapter into your listView ref.