Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 可展开列表视图组位置变量导致索引超出范围_Java_Android_Expandablelistview - Fatal编程技术网

Java 可展开列表视图组位置变量导致索引超出范围

Java 可展开列表视图组位置变量导致索引超出范围,java,android,expandablelistview,Java,Android,Expandablelistview,错误指向行字符串groceryName=recipeNames.get(parentPosition)我理解错误的含义,但我不完全理解导致错误的原因。奇怪的是,只有当一些群体扩大时,这种情况才会发生。当我按下删除按钮时,它没有删除正确的项目,这意味着它读取了错误的索引。如果所有的组都被折叠,它就可以删除。非常感谢您的帮助 package groceryproject.jacob.com.recipelist; import android.content.Context; import and

错误指向行
字符串groceryName=recipeNames.get(parentPosition)我理解错误的含义,但我不完全理解导致错误的原因。奇怪的是,只有当一些群体扩大时,这种情况才会发生。当我按下删除按钮时,它没有删除正确的项目,这意味着它读取了错误的索引。如果所有的组都被折叠,它就可以删除。非常感谢您的帮助

package groceryproject.jacob.com.recipelist;

import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Typeface;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.TextView;
import java.util.List;

public class ExpandableIngredientListAdapter extends BaseExpandableListAdapter{
private Context mContext;
private List<String> recipeNames; // header titles
private HashMap<String, List<String>> recipeIngredients;
private Button mDeleteButton;

public ExpandableIngredientListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listChildData) {
    this.mContext = context;
    this.recipeNames = listDataHeader;
    this.recipeIngredients = listChildData;
}

@Override
public Object getChild(int groupPosition, int childPosititon) {
    return this.recipeIngredients.get(this.recipeNames.get(groupPosition)).get(childPosititon);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public View getChildView(int groupPosition, final int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {
    //Changed from groupPosition to groupPosition - 1
    final String childText = (String) getChild(groupPosition, childPosition);

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.expandable_list_view_item, null);
    }

    TextView txtListChild = (TextView) convertView.findViewById(R.id.expandable_list_recipe_ingredient_item);
    txtListChild.setText(childText);
    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    return this.recipeIngredients.get(this.recipeNames.get(groupPosition)).size();
}

@Override
public Object getGroup(int groupPosition) {
    return this.recipeNames.get(groupPosition);
}

@Override
public int getGroupCount() {
    return this.recipeNames.size();
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, final ViewGroup parent) {
    String headerTitle = (String) getGroup(groupPosition);
    if (convertView == null) {
        final LayoutInflater infalInflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.expandable_list_view_group, null);

        mDeleteButton = (Button) convertView.findViewById(R.id.delete_recipe_from_grocery_list_button);
        //TODO: Add a dialog to ensure user wants to delete the recipe
        mDeleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int parentPosition = groupPosition;
                //This is where the error is
                String groceryName = recipeNames.get(parentPosition);
                RecipeDB dbHelper = new RecipeDB(parent.getContext());
                recipeNames.remove(parentPosition);
                recipeIngredients.remove(parentPosition);
                dbHelper.deleteGrocery(groceryName);
                notifyDataSetChanged();

            }
        });
    }

    TextView lblListHeader = (TextView) convertView.findViewById(R.id.expandable_list_recipe_header);
    lblListHeader.setTypeface(null, Typeface.BOLD);
    lblListHeader.setText(headerTitle);



    return convertView;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}
}

所以我似乎终于找到了解决办法。无论出于何种原因,删除该行:

if (convertView == null){

从getGroupView和getChildView修复了它。我不确定从getChildView中删除它是否真的有用,但我这样做是为了更好的衡量。我不知道为什么这是一个修复,但在我试图复制这个问题时,它还没有崩溃

添加您的错误logshare logcat/error添加了logcat错误。我不确定它会做什么,但我将重写构造函数,这样我就可以直接从数据库中提取,而不是将信息作为参数传递。我还想通过直接访问我的自定义对象,消除对带有arraylist的hashmap的需要。我知道这不会解决任何问题,但它会让我更容易找到它的故障点。我会更新进度。
if (convertView == null){