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

尝试在Android中显示子视图时,ExpandableListView的groupPosition错误,第1张

概述我有一个问题,我已经有一段时间了.我正在使用带有ViewHolder的ExpandableListView.我知道ExpandableListView重用了Views,这就是我认为我遇到的问题. 我在列表中有3个视图,如果我选择1然后选择2然后选择3,那么一切都很好,它会显示正确的子视图.虽然当我首先选择3时,所有其他子视图对于相同的groupPosition是相同的.如果我先选择2,那么3是正确 我有一个问题,我已经有一段时间了.我正在使用带有VIEwHolder的ExpandableListVIEw.我知道ExpandableListVIEw重用了VIEws,这就是我认为我遇到的问题.

我在列表中有3个视图,如果我选择1然后选择2然后选择3,那么一切都很好,它会显示正确的子视图.虽然当我首先选择3时,所有其他子视图对于相同的groupposition是相同的.如果我先选择2,那么3是正确的,但是1显示第三个子视图.

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

这是我的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;}@OverrIDepublic int getGroupCount() {    return ListData.size();}@OverrIDepublic int getChildrenCount(int groupposition) {    return 1; // There will only ever be 1 child in the group.}@OverrIDepublic Object getGroup(int groupposition) {    return groupposition;}@OverrIDepublic Object getChild(int groupposition,int childposition) {    return null;}@OverrIDepublic long getGroupID(int groupposition) {    return groupposition;}@OverrIDepublic long getChildID(int groupposition,int childposition) {    return childposition;}@OverrIDepublic boolean hasStableIDs() {    return true; // Stops child vIEws from resetting}@OverrIDepublic 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;}@OverrIDepublic VIEw getChildVIEw(int groupposition,int childposition,boolean isLastChild,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,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;}@OverrIDepublic boolean isChildSelectable(int groupposition,int childposition) {    return false;}}

请有人帮我解决这个问题.

解决方法 问题是视图回收,你只是在第一次膨胀视图时更新视图试试这个

if(convertVIEw == null){    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);    convertVIEw = inflater.inflate(R.layout.ListvIEw_desc_item,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);
总结

以上是内存溢出为你收集整理的尝试在Android中显示子视图时,ExpandableListView的groupPosition错误全部内容,希望文章能够帮你解决尝试在Android中显示子视图时,ExpandableListView的groupPosition错误所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1127810.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存