我有一个类别和项目的ExpandableListView(每个项目属于类别)。现在我只想获取列表中单击项目的ID或名称,并在Toast消息中显示它。但是,当我点击某个项目时,没有任何事情发生,没有Toast。
这是我的ExpandableListView适配器(相关部分):
public class Expandable_ListView_Adapter extends BaseExpandableListAdapter {
LayoutInflater inflater;
ArrayList<Category> arr_categories;
Context context;
public Expandable_ListView_Adapter(Context context, ArrayList<Category> categories)
{
inflater = LayoutInflater.from(context);
arr_categories = categories;
this.context=context;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return arr_categories.get(groupPosition).getCategory_items().get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
Category category = arr_categories.get(groupPosition);
int category_id = category.getCategory_id();
return category_id;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
Category category = arr_categories.get(groupPosition);
Base_Item base_item = category.getCategory_items().get(childPosition);
int id = base_item.getItem_id();
Toast.makeText(context, "Category: "+category.getCategory_name() + ", \nItem: "+base_item.getItem_name()
+ "\nID: "+id,
Toast.LENGTH_SHORT).show();
return id;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
final Category category = arr_categories.get(groupPosition);
convertView = inflater.inflate(R.layout.row_category, parent, false);
((TextView)convertView.findViewById(R.id.tv_category_name)).setText(category.getCategory_name());
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final Category category = arr_categories.get(groupPosition);
final Base_Item base_item = category.getCategory_items().get(childPosition);
convertView = inflater.inflate(R.layout.row_base_item, parent, false);
((TextView)convertView.findViewById(R.id.tv_base_item_name)).setText(base_item.getItem_name());
return convertView;
}
这是我片段中的代码:
public class Fragment_Master_List extends Fragment {
Task_DB_Manager manager;
Expandable_ListView_Adapter adapter;
ArrayList<Category> arr_all_categories;
ExpandableListView master_lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_master_list, container, false);
master_lv = (ExpandableListView) view.findViewById(R.id.elv_master_list);
manager = new Task_DB_Manager(getActivity());
arr_all_categories = manager.get_all_categories();
adapter = new Expandable_ListView_Adapter(getActivity(), arr_all_categories);
master_lv.setAdapter(adapter);
master_lv.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(getActivity(), "Item: "+arr_all_categories.get(groupPosition).getCategory_items().get(childPosition).getItem_name().toString(), Toast.LENGTH_SHORT).show();
//Toast.makeText(getActivity(), "Item id: "+adapter.getChildId(groupPosition, childPosition), Toast.LENGTH_SHORT).show();
return false;
}
});
return view;
}
}
当我点击一个项目(孩子)以及如何修复它时,我怎么不敬酒呢?谢谢!
答案 0 :(得分:0)
经过一番研究后,我找到了解决方案!我不得不改变这个自动创建的方法,我认为这是无关紧要的,返回true而不是false:
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}