アンドロイドのfragmentにアイテムを表示したいが上手くいかない
http://gacken.com/wp/program/android/1927/
を参考にアンドロイドのフラグメントにアダプターを入れて表示させたのですが
1 2 3は表示されるのですがグループは表示されません。
どのようにすれば解決するのでしょうか。
ItemListFragment.java
public class ItemListFragment extends ListFragment {
private Callbacks mCallbacks = sDummyCallbacks;
public class GroupSpinnerItem {
public boolean isGroup; // グループフラグ
public String itemName; // アイテム名
public GroupSpinnerItem(boolean isGroup, String itemName) {
this.isGroup = isGroup;
this.itemName = itemName;
}
}
public interface Callbacks {
void onItemSelected(String id);
}
private static Callbacks sDummyCallbacks = new Callbacks() {
@Override
public void onItemSelected(String id) {
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GroupSpinnerAdapter adapter = new GroupSpinnerAdapter(getContext());
adapter.addItem(new GroupSpinnerItem(true, "グループ"));
adapter.addItem(new GroupSpinnerItem(false, "1"));
adapter.addItem(new GroupSpinnerItem(false, "2"));
adapter.addItem(new GroupSpinnerItem(false, "3"));
setListAdapter(adapter);
}
GroupSpinnerAdapter.java
public class GroupSpinnerAdapter extends BaseAdapter {
public static ArrayList<ItemListFragment.GroupSpinnerItem> itemList = new ArrayList<ItemListFragment.GroupSpinnerItem>();
public static LayoutInflater inflater;
public GroupSpinnerAdapter(Context context) {
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public static void addItem(ItemListFragment.GroupSpinnerItem item) {
itemList.add(item);
}
public int getCount() {
return itemList.size();
}
public Object getItem(int position) {
return itemList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
return createView(position, convertView, parent, false);
}
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
return createView(position, convertView, parent, true);
}
public View createView(int position, View convertView, ViewGroup parent, boolean isDropDown) {
TextView tvName = null;
if(convertView instanceof TextView) {
tvName = (TextView)convertView;
}
if(tvName == null) {
tvName = (TextView)inflater.inflate(android.R.layout.simple_spinner_item, parent, false);
}
ItemListFragment.GroupSpinnerItem item = itemList.get(position);
if(item.isGroup) {
if(isDropDown) {
tvName.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
tvName.setText(item.itemName);
tvName.setBackgroundColor(Color.BLUE);
}
}
else {
String text = " " + item.itemName;
tvName.setText(text);
tvName.setBackgroundColor(Color.TRANSPARENT);
}
return tvName;
}
@Override
public boolean isEnabled(int position) {
return (itemList.get(position).isGroup == false);
}
}