可扩展列表视图中的递增和递减

时间:2016-07-04 11:33:33

标签: android

我创建了expandablelisview。如果我点击子项中的plus imageview,count的值增加1.最初所有子项的计数值为0.如果我点击第1行中的加号,则计数值从0增加到1.之后,我点击第2行意味着计数值从1增加到2.我想将计数值增加为0到1而不是第2行中的1到2

public class ExpandListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private ArrayList<Group> groups;
    ArrayList<Child> ch_list=new ArrayList<Child>();

    Integer count=0;

    public class ViewHolder {

        TextView tv ;
        ImageView food_image;
        ImageView minus,plus ;
         TextView item_count;

    }


    public ExpandListAdapter(Context context, ArrayList<Group> groups) {
        this.context = context;
        this.groups = groups;

    }


    @Override
    public Object getChild(int groupPosition, int childPosition) {
        ArrayList<Child> chList = groups.get(groupPosition).getItems();
        return chList.get(childPosition);
    }

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

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

        final Child child = (Child) getChild(groupPosition, childPosition);



        ViewHolder viewHolder = null;
        if (convertView == null) {
            LayoutInflater inflator = LayoutInflater.from(parent.getContext());
            convertView = inflator.inflate(R.layout.detail_list, null);
            viewHolder = new ViewHolder();

            viewHolder.tv = (TextView) convertView.findViewById(R.id.type);
            viewHolder.food_image = (ImageView) convertView.findViewById(R.id.food_image);
            viewHolder.minus = (ImageView) convertView.findViewById(R.id.minus);
            viewHolder.plus = (ImageView) convertView.findViewById(R.id.plus);
            viewHolder.item_count = (TextView) convertView.findViewById(R.id.count);


            convertView.setTag(viewHolder);


        }
        else {
            viewHolder = (ViewHolder) convertView.getTag();
        }


        viewHolder.tv.setText(child.getName().toString());


        final ViewHolder finalViewHolder = viewHolder;
        viewHolder.plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                count=count+1;
                finalViewHolder.item_count.setText(count.toString());
            }
        });




        final ViewHolder finalViewHolder1 = viewHolder;
        viewHolder.minus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                count=count-1;
                finalViewHolder1.item_count.setText(count.toString());   //Key -> String.valueOf(position) and Value -> int count
            }
        });

        return convertView;
    }

    @Override
    public  int getChildrenCount(int groupPosition) {
        ArrayList<Child> chList = groups.get(groupPosition).getItems();
        return chList.size();
    }

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

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        Group group = (Group) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater inf = (LayoutInflater) context
                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = inf.inflate(R.layout.group_item, null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.group_name);
        tv.setText(group.getName());
        ExpandableListView eLV = (ExpandableListView) parent;
        int count = getGroupCount();
                          if(count<1){

                             eLV.expandGroup(groupPosition);
                             // eLV.setGroupIndicator(null);
                          }


        return convertView;
    }



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

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

}

Logcat错误`

FATAL EXCEPTION: main
                                                                       Process: abservetech.com.foodapp, PID: 1807
                                                                       android.content.res.Resources$NotFoundException: Unable to find resource ID #0x1
                                                                           at android.content.res.Resources.getResourcePackageName(Resources.java:1871)
                                                                           at android.content.res.SPRDResources.getThemeResources(SPRDResources.java:94)
                                                                           at android.content.res.SPRDResources.getText(SPRDResources.java:155)
                                                                           at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
                                                                           at android.widget.TextView.setText(TextView.java:3927)
                                                                           at abservetech.com.foodapp.ExpandListAdapter$1.onClick(ExpandListAdapter.java:99)
                                                                           at android.view.View.performClick(View.java:4446)
                                                                           at android.view.View$PerformClick.run(View.java:18437)
                                                                           at android.os.Handler.handleCallback(Handler.java:733)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                           at android.os.Looper.loop(Looper.java:136)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5372)
                                                                           at java.lang.reflect.Method.invokeNative(Native Method)
                                                                           at java.lang.reflect.Method.invoke(Method.java:515)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:970)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:786)
                                                                           at dalvik.system.NativeStart.main(Native Method)

`

1 个答案:

答案 0 :(得分:0)

将count变量定义为不是类中的全局变量,而是定义为getChildView中的本地变量。您可以在每行或其他内容中显示它。但是这样,它对每一行都是唯一的,而不是适配器的全局。

编辑:

在适配器中创建一个Integer Array全局

int[] myIntegerArray = new int[size];
默认情况下,

整数数组按照here的规定启动为0。 您现在只需要在getChildView内部执行onClickListener:

myIntegerArray[childPosition] += 1 
finalViewHolder.item_count.setText(Integer.toString(myIntegerArray[childPosition]));