0

我有一个带有上下文菜单的 ExpandableListView。不管我有多少组项目,它总是返回 0。

我的代码:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);

    ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;

    int group = ExpandableListView.getPackedPositionGroup((int) info.packedPosition);
    int type = ExpandableListView.getPackedPositionType((int) info.packedPosition);
    int child = ExpandableListView.getPackedPositionChild((int) info.packedPosition);

    Log.d("Value ConTEXT: " + String.valueOf(group), String.valueOf(child) + " GROUP COUNT " + getExpandableListAdapter().getGroupCount());
    menu.setHeaderTitle(((Person) dAdapter.getGroup(group)).getName());
    // menu.add(0, 0, 0, "Geschiedenis verwijderen");
    menu.add(1, 1, 1, "Verwijderen uit lijst");
}

还有我的自定义基本列表适配器:

public class DrinkActionAdapter extends BaseExpandableListAdapter {

    Activity context;
    ArrayList<Person> personsGroup;
    ArrayList<Person> personsChild;

    public DrinkActionAdapter(Activity context, ArrayList<Person> personsGroup,
            ArrayList<Person> personsChild) {
        this.context = context;
        this.personsGroup = personsGroup;
        this.personsChild = personsChild;
    }

    public void addPerson(Person p) {
        personsGroup.add(p);
        personsChild.add(p);
        for (Person pp : personsGroup) {
            Log.d(pp.getName(), pp.getName());
        }
    }

    public Object getChild(int groupPosition, int childPosition) {
        Log.d("GetChild" + String.valueOf(groupPosition), String.valueOf(childPosition));
        return personsGroup.get(childPosition);
    }

    public long getChildId(int groupPosition, int childPosition) {
        Log.d("GetChildID" + String.valueOf(groupPosition), String.valueOf(childPosition));
        return childPosition;
    }

    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        Person p = personsChild.get(groupPosition);
        View v = convertView;
        Log.d(p.getName(), p.getName() + "CHILD");
        if (v == null) {

            LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = mInflater.inflate(R.layout.drink_action_child, null);
        }
        if (personsChild.size() != 0) {
            TextView lbFavoriteDrink = (TextView) v.findViewById(R.id.drink_action_child_lb_favorite_drink);
            if (p.getFavoriteDrink() != null) {
                lbFavoriteDrink.setText(p.getFavoriteDrink().getName());
            } else {
                lbFavoriteDrink.setText("Nog niks gedronken.");
            }
            TextView lbCost = (TextView) v.findViewById(R.id.drink_action_child_lb_cost);
            lbCost.setText(String.valueOf(p.getTotalMoneySpent()));

            TextView lbAmount = (TextView) v.findViewById(R.id.drink_action_child_lb_amount);
            lbAmount.setText(String.valueOf(p.getTotalAmountOfDrinks()));

            TextView lbKcal = (TextView) v.findViewById(R.id.drink_action_child_lb_kcal);
            lbKcal.setText(String.valueOf(p.getTotalKcal()));

            TextView lbPunishment = (TextView) v.findViewById(R.id.drink_action_child_lb_boete);
            lbPunishment.setText(p.getPunishment());

            TextView lbWeightPutOn = (TextView) v.findViewById(R.id.drink_action_child_lb_weight);
            lbWeightPutOn.setText(String.valueOf(p.getWeightPutOn()));

            TextView lbLiters = (TextView) v.findViewById(R.id.drink_action_child_lb_liter);
            lbLiters.setText(String.valueOf(p.getAmountOfLiters()));

            TextView lbTimeSinceLastDrink = (TextView) v.findViewById(R.id.drink_action_child_lb_time_since_last_drink);
            lbTimeSinceLastDrink.setText(String.valueOf(p.getTimeSinceLast()[2]) + ":" + String.valueOf(p.getTimeSinceLast()[1]) + ":" + String.valueOf(p.getTimeSinceLast()[0]));

        } else {
            // WHAT TO DO IF NO PROFILES
        }

        return v;
    }

    public int getChildrenCount(int groupPosition) {
        Log.d("GetChildrenCount" + String.valueOf(groupPosition), String.valueOf(groupPosition));
        return 1;
        // return persons.size();
    }

    public Object getGroup(int groupPosition) {
        Log.d(String.valueOf(groupPosition), String.valueOf(groupPosition) + personsGroup.get(groupPosition).getName());
        return personsGroup.get(groupPosition);
    }

    public int getGroupCount() {

        return personsGroup.size();
    }

    public long getGroupId(int groupPosition) {
        Log.d(String.valueOf(groupPosition) + "GetGroupId", String.valueOf(groupPosition));
        return groupPosition;
    }

    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        Person p = personsGroup.get(groupPosition);
        View v = convertView;
        Log.d(groupPosition + " " + p.getName(), p.getName() + "GROUP");
        if (v == null) {

            LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = mInflater.inflate(R.layout.drink_action_item, null);

        }
        if (personsGroup.size() != 0) {

            TextView lbName = (TextView) v.findViewById(R.id.drink_action_lb_name);
            TextView lbAlcohol = (TextView) v.findViewById(R.id.drink_action_info_current_alcohol);

            ImageView btnDrink = (ImageView) v.findViewById(R.id.drink_action_item_btn_drink);
            btnDrink.setTag(p);

            lbName.setText(p.getName());
            lbAlcohol.setText(String.valueOf(p.getCurrentAlcoholLevel()));

        } else {
            // WHAT TO DO IF NO PROFILES
        }

        return v;
    }

    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return true;
    }

    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return true;
    }

}

每当我长按一个项目时,它总是选择第一个。

4

1 回答 1

0

return v;尝试使用此代码之前在 getChildView() 方法中注册到 contextMenu((Activity) context).registerForContextMenu(v);

于 2012-12-11T16:12:02.697 回答