Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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
尝试在Android中显示子视图时,ExpandableListView的组位置错误_Android_Expandablelistview_Android Viewholder - Fatal编程技术网

尝试在Android中显示子视图时,ExpandableListView的组位置错误

尝试在Android中显示子视图时,ExpandableListView的组位置错误,android,expandablelistview,android-viewholder,Android,Expandablelistview,Android Viewholder,我有一个问题,我已经工作了一段时间。我使用的是一个可扩展的ListView和一个ViewHolder。我知道ExpandableListView重用视图,我认为这就是我遇到的问题 我在列表中有3个视图,如果我选择1,然后选择2,然后选择3,则一切正常,并显示正确的子视图。虽然当我首先选择3时,所有其他子视图都是相同的groupPosition。如果我先选择2,则3是正确的,但1显示第三个子视图 我希望它们都固定在正确的位置,这样它们就不会改变 这是我的ExpandableListView适配器:

我有一个问题,我已经工作了一段时间。我使用的是一个可扩展的ListView和一个ViewHolder。我知道ExpandableListView重用视图,我认为这就是我遇到的问题

我在列表中有3个视图,如果我选择1,然后选择2,然后选择3,则一切正常,并显示正确的子视图。虽然当我首先选择3时,所有其他子视图都是相同的groupPosition。如果我先选择2,则3是正确的,但1显示第三个子视图

我希望它们都固定在正确的位置,这样它们就不会改变

这是我的ExpandableListView适配器:

public class MenuListAdapter extends BaseExpandableListAdapter 
{
private Context context;
private ArrayList<ListData> listData = new ArrayList<ListData>();

/**
 * Constructor for the class.
 * 
 * @param refContext This is a reference to the context of the ExpandableListAdapter being used in.
 * @param refListData This is a reference to the ArrayList of ListData that will display the item data of the menu in the list.
 */
public MenuListAdapter(Context refContext, ArrayList<ListData> refListData)
{
    context = refContext;
    listData = refListData;
}

private static class ViewHolder
{
    TextView title;
    TextView description;
    ImageView image;
    ImageButton addItem;
}

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

@Override
public int getChildrenCount(int groupPosition) 
{
    return 1; // There will only ever be 1 child in the group.
}

@Override
public Object getGroup(int groupPosition) 
{
    return groupPosition;
}

@Override
public Object getChild(int groupPosition, int childPosition) 
{
    return null;
}

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

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

@Override
public boolean hasStableIds() 
{
    return true; // Stops child views from resetting
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) 
{
    final ViewHolder holder;

    if(convertView == null)
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.listview_item, parent, false);

        holder = new ViewHolder();
        holder.title = (TextView) convertView.findViewById(R.id.titleTextView);
        holder.image = (ImageView) convertView.findViewById(R.id.imageImageView);
        holder.addItem = (ImageButton) convertView.findViewById(R.id.addItemImageButton);

        /* The contents of the item View is set for every View in the list. */
        holder.title.setText(listData.get(groupPosition).getTitle());
        holder.image.setImageBitmap(listData.get(groupPosition).getImageBitmap());
        holder.image.setTag(listData.get(groupPosition).getImageRes()); // Sets tag for image resource.

        holder.image.setFocusable(false);// Enables the group view to be in focus in order to still be able to select the image and group.

        holder.image.setOnClickListener(new OnClickListener() 
        {       
            @Override
            public void onClick(View v) 
            {
                Integer imageResource = (Integer) holder.image.getTag(); // Get image resource from tag.
                int resource = imageResource.intValue(); // Creates primitive image resource from object Integer.

                Intent imageIntent = new Intent(context, ImageActivity.class);
                imageIntent.putExtra("image_resource", resource); // Adds image resource as Extra to Intent.
                context.startActivity(imageIntent);
            }
        });

        holder.addItem.setFocusable(false); // Enables the group view to be in focus in order to still be able to select the button and group.

        holder.addItem.setOnClickListener(new OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {   
                /* AlertDialog that allows the user to select how many items they want of the item they selected. */
                AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
                AlertDialog alertDialog;
                LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View customView = layoutInflater.inflate(R.layout.item_quantity, null);

                final NumberPicker quantityPicker = (NumberPicker) customView.findViewById(R.id.quantityPicker);
                quantityPicker.setMinValue(1);
                quantityPicker.setMaxValue(10);
                quantityPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS); // Stops keyboard from appearing.

                alertBuilder.setTitle("Select quantity");
                alertBuilder.setView(customView);
                alertBuilder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() 
                {
                    public void onClick(DialogInterface dialog, int id) 
                    {
                        int quantityAmount = quantityPicker.getValue(); // Gets value from NumberPicker.

                        /* Update checkout data. */
                        MainActivity mainActivity = (MainActivity) context;
                        mainActivity.updateCheckout(holder.title.getText().toString(), quantityAmount); // Updates checkout with added order.

                        dialog.cancel(); // Cancels AlertDialog.

                        Toast.makeText(context, "Order added to checkout!", Toast.LENGTH_SHORT).show();
                    }
                });

                alertBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() 
                {

                    @Override
                    public void onClick(DialogInterface dialog, int which) 
                    {
                        dialog.cancel(); // Cancels AlertDialog.
                    }
                });

                alertDialog = alertBuilder.create(); // Creates the AlertDialog View.
                alertDialog.show(); // Shows the AlertDialog View.
            }

        });

        convertView.setTag(holder);
    }
    else
    {
        holder = (ViewHolder) convertView.getTag();
    }

    return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) 
{
    final ViewHolder holder;

    if(convertView == null)
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.listview_desc_item, parent, false);

        holder = new ViewHolder();
        holder.description = (TextView) convertView.findViewById(R.id.descriptionTextView);

        holder.description.setText(listData.get(groupPosition).getDescription()); // Sets description of item View.

        System.out.println("Group position: " + groupPosition);
        System.out.println("Child position: " + childPosition);

        convertView.setTag(holder);
    }
    else
    {
        holder = (ViewHolder) convertView.getTag();
    }

    return convertView;
}

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

}
公共类MenuListAdapter扩展了BaseExpandableListAdapter
{
私人语境;
私有ArrayList listData=新ArrayList();
/**
*类的构造函数。
* 
*@param refContext这是对中使用的ExpandableListAdapter的上下文的引用。
*@param refListData这是对列表数据的ArrayList的引用,该列表将显示列表中菜单的项目数据。
*/
public MenuListAdapter(Context refContext,ArrayList refListData)
{
context=refContext;
listData=refListData;
}
私有静态类视图持有者
{
文本视图标题;
文本视图描述;
图像视图图像;
图像按钮附加项;
}
@凌驾
public int getGroupCount()
{
返回listData.size();
}
@凌驾
公共整数getChildrenCount(整数组位置)
{
return 1;//组中只有一个子项。
}
@凌驾
公共对象getGroup(int-groupPosition)
{
返回组位置;
}
@凌驾
公共对象getChild(int-groupPosition,int-childPosition)
{
返回null;
}
@凌驾
公共长getGroupId(int-groupPosition)
{
返回组位置;
}
@凌驾
公共长getChildId(int-groupPosition,int-childPosition)
{
返回子位置;
}
@凌驾
公共布尔表ID()
{
return true;//停止重置子视图
}
@凌驾
公共视图getGroupView(int groupPosition、布尔isExpanded、视图convertView、视图组父级)
{
最终持票人;
if(convertView==null)
{
LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
convertView=充气机。充气(R.layout.listview_项,父项,false);
holder=新的ViewHolder();
holder.title=(TextView)convertView.findViewById(R.id.titleTextView);
holder.image=(ImageView)convertView.findViewById(R.id.ImageView);
holder.addItem=(ImageButton)convertView.findViewById(R.id.addItemImageButton);
/*为列表中的每个视图设置项目视图的内容*/
holder.title.setText(listData.get(groupPosition.getTitle());
holder.image.setImageBitmap(listData.get(groupPosition.getImageBitmap());
holder.image.setTag(listData.get(groupPosition.getImageRes());//设置图像资源的标记。
holder.image.setFocusable(false);//使组视图处于焦点位置,以便仍然能够选择图像和组。
holder.image.setOnClickListener(新的OnClickListener()
{       
@凌驾
公共void onClick(视图v)
{
整数imageResource=(整数)holder.image.getTag();//从标记获取图像资源。
int resource=imageResource.intValue();//从对象整数创建基本图像资源。
Intent-imageIntent=新的意图(上下文,ImageActivity.class);
imageIntent.putExtra(“图像资源”,resource);//将图像资源作为额外资源添加到Intent。
背景。起始触觉(意象意图);
}
});
holder.addItem.setFocusable(false);//使组视图处于焦点位置,以便仍然能够选择按钮和组。
holder.addItem.setOnClickListener(新的OnClickListener()
{
@凌驾
公共void onClick(视图v)
{   
/*AlertDialog,允许用户从所选项目中选择需要的项目数量*/
AlertDialog.Builder alertBuilder=新建AlertDialog.Builder(上下文);
警报对话框警报对话框;
LayoutInflater LayoutInflater=(LayoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
视图customView=layoutInflater.flate(R.layout.item\u数量,空);
final NumberPicker quantityPicker=(NumberPicker)customView.findviewbyd(R.id.quantityPicker);
quantityPicker.setMinValue(1);
quantityPicker.setMaxValue(10);
quantityPicker.SetDegenantFocusability(NumberPicker.FOCUS_BLOCK_subgends);//停止显示键盘。
alertBuilder.setTitle(“选择数量”);
alertBuilder.setView(自定义视图);
alertBuilder.setPositiveButton(“确认”,新的DialogInterface.OnClickListener()
{
public void onClick(DialogInterface对话框,int-id)
{
int quantityAmount=quantityPicker.getValue();//从NumberPicker获取值。
/*更新签出数据*/
MainActivity MainActivity=(MainActivity)上下文;
mainActivity.updateCheckout(holder.title.getText().toString(),quantityAmount);//使用添加的订单更新签出。
dialog.cancel();//取消警报对话框。
Toast.makeText(上下文,“订单已添加到签出!”,Toast.LENGTH_SHORT.show();
}
});
alertBuilder.setNegativeButton(“取消”,新的DialogInterface.OnClickListener()
{
@O
if(convertView == null)
{
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.listview_desc_item, parent, false);

    holder = new ViewHolder();


    convertView.setTag(holder);
}


    holder = (ViewHolder) convertView.getTag();
    holder.description = (TextView) convertView.findViewById(R.id.descriptionTextView);

    holder.description.setText(listData.get(groupPosition).getDescription());
    System.out.println("Group position: " + groupPosition);
    System.out.println("Child position: " + childPosition);