Reputation: 1201
I'm trying to create an expandable listview. The initial view with the groups shows up fine, the only problem is... nothing happens when I click on them. No expansion or anything. I've followed most of the tutorials I've seen and nothing has fixed it! Thanks for any help, here is the code I'm working with:
FeaturesAdapter:
package com.me.app;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
public class FeaturesAdapter extends BaseExpandableListAdapter
{
private Context mContext;
private HashMap<String, List<String>> mFeatures = new HashMap<String, List<String>>();
private HashMap<Integer, String> mList = new HashMap<Integer, String>();
public FeaturesAdapter (Context con)
{
mContext = con;
}
public boolean AddGroup(final String groupName, final List<String> features)
{
final List<Feature> prev = mFeatures.put(groupName, features);
if (prev != null)
return false;
mList.put(mFeatures.size() - 1, groupName);
notifyDataSetChanged();
return true;
}
@Override
public Object getChild(int groupPos, int childPos)
{
if (mList.containsKey(groupPos))
{
final String str = mList.get(groupPos);
final List<Feature> data = mFeatures.get(str);
return data.get(childPos);
}
return null;
}
@Override
public long getChildId(int groupPos, int childPos)
{
return 0;
}
@Override
public View getChildView(int groupPos, int childPos, boolean isLastChild, View convertView, ViewGroup parent)
{
View rowView = convertView;
FeatureView fView = null;
int gPos = groupPos;
int cPos = childPos;
if (rowView == null)
{
LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.feature_row, null);
fView = new FeatureView();
fView.feature_name = (TextView) rowView.findViewById(R.id.feature_name);
fView.feature_checked = (CheckBox) rowView.findViewById(R.id.feature_checked);
rowView.setTag(fView);
}
else
fView = (FeatureView) rowView.getTag();
String str = mList.get(gPos);
List<Feature> data = mFeatures.get(str);
Feature feature = data.get(cPos);
fView.feature_name.setText(feature.getName());
fView.feature_checked.setChecked(feature.getChecked());
int [] values = {gPos,cPos};
fView.feature_checked.setTag(values);
fView.feature_checked.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
doThings();
}
});
return rowView;
}
protected static class FeatureView {
protected TextView feature_name;
protected CheckBox feature_checked;
}
@Override
public int getChildrenCount(int groupPos)
{
if (mList.containsKey(groupPos))
return mFeatures.get(mList.get(groupPos)).size();
return 0;
}
@Override
public Object getGroup(int groupPos)
{
if (mList.containsKey(groupPos))
return mFeatures.get(mList.get(groupPos));
return null;
}
@Override
public int getGroupCount()
{
return mFeatures.size();
}
@Override
public long getGroupId(int groupPos)
{
return 0;
}
@Override
public View getGroupView(int groupPos, boolean isExpanded, View convertView, ViewGroup parent)
{
View rowView = convertView;
FeatureView fView = null;
int gPos = groupPos;
if (rowView == null)
{
LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.group_row, null);
fView = new FeatureView();
fView.feature_name = (TextView) rowView.findViewById(R.id.group_name);
fView.feature_checked = (CheckBox) rowView.findViewById(R.id.group_checked);
rowView.setTag(fView);
}
else
fView = (FeatureView) rowView.getTag();
String str = mList.get(groupPos);
List<Feature> data = mFeatures.get(str);
fView.feature_name.setText(mList.get(groupPos));
boolean flag = false;
for (Feature feat : data)
{
if (feat.getChecked())
flag = true;
}
fView.feature_checked.setChecked(flag);
int[] values = {gPos};
fView.feature_checked.setTag(values);
fView.feature_checked.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
doOtherThings();
});
return rowView;
}
@Override
public boolean hasStableIds()
{
return false;
}
@Override
public boolean isChildSelectable(int groupPos, int childPos)
{
return false;
}
}
And here is my code in the actual file:
super.onCreate(savedInstanceState);
this.setContentView(R.layout.feature_list);
FeaturesAdapter adapter = new FeaturesAdapter(this);
mBye.add("Cya");
mBye.add("Bye");
mBye.add("Adios");
mBye.add("Goodbye");
adapter.AddGroup("Bye", mBye);
mStuff.add("Hi"));
mStuff.add("Hey"));
mStuff.add("Hello"));
adapter.AddGroup("Salutations", mStuff);
mSlang.add(new Feature("Yo"));
adapter.AddGroup("Slang", mSlang);
setListAdapter(adapter);
ExpandableListView elv = getExpandableListView();
elv.setTextFilterEnabled(true);
Upvotes: 3
Views: 1498
Reputation: 1201
And we have a winner. If you have focusable elements within the ExpandableListView, you need to set android:focusable="false" within their xml settings. Wohoo.
Upvotes: 5