1

I have created the expandablelisview.If i click the plus imageview in child items the value of count is increased by 1. Initially the count value is 0 for all child item.If i click the plus in 1st row,the count value increased from 0 to 1.After that,i clicked the 2nd row means the count value increased from 1 to 2.I want to increase the count value as 0 to 1 in not 1 to 2 in 2nd row`

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 error`

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)

`

Abserve Tech
  • 117
  • 2
  • 11

1 Answers1

0

Define the count variable not as a global variable in the class but as a local one in getChildView. You could display it inside each row or whatever. But that way it will be unique to each row and not global to the adapter.

EDIT:

Create a Integer Array global inside your adapter

int[] myIntegerArray = new int[size];

integers array are by default initiated to 0 as stated here. You now just need to do inside getChildView onClickListener:

myIntegerArray[childPosition] += 1 
finalViewHolder.item_count.setText(Integer.toString(myIntegerArray[childPosition]));
Community
  • 1
  • 1
  • I just told you, create the variable inside the getChildView function. With something like Integer count = 0; It should do the trick. – Loic Reyreaud Jul 04 '16 at 12:24
  • its work but if scroll the list the value get recycle – Abserve Tech Jul 04 '16 at 12:26
  • Indeed I forgot about recycle. Try to create a global array with the size of the max item in your list. Then inside getChildView access the array at the view position. That should workaround the problem of the recycle – Loic Reyreaud Jul 04 '16 at 12:28
  • can u explain me with simple codes.I can't understand what ur telling – Abserve Tech Jul 04 '16 at 12:34
  • If i do like that means it shows error.i have posted my logcat error – Abserve Tech Jul 04 '16 at 12:51
  • Yes silly mistake on my side. But let's interpret the logcat to find the mistake: at android.widget.TextView.setText(TextView.java:3927). Means problem on setText line. Then can't find ressource id means that compiler is looking for an ID, but it should not cause it should be a string. I forgot to add Integer.toString(myIntegerArray[childPosition]) which transform into string. I edit with it – Loic Reyreaud Jul 04 '16 at 12:56
  • new int[size]; which size? – Abserve Tech Jul 04 '16 at 13:04
  • The size you want for your list. Could be 10 100 1000. What you want. You can change it for a constant. – Loic Reyreaud Jul 04 '16 at 13:07
  • Accept the answer if the problem is fixed, otherwise comment your problem :) – Loic Reyreaud Jul 04 '16 at 14:06
  • i have tried this now its working but when i increased the row value in group1(1st child)means that same value will increased in group2(1st child) – Abserve Tech Jul 05 '16 at 04:34
  • Then use groupPosition and not childPosition – Loic Reyreaud Jul 05 '16 at 07:09