3

Please look into this screen:

please look into accept button image

And when I click on accept changed to another button:

i clicked on accept chnaged to another button image

if I click on one getChildView List item in expandable list-view,it is also effecting to other child item while scrolling the screen. Here I changed button text when I click the getChildview item

Here I am clicking on accept button of one child ,then it should change to another button but while scrolling all child items buttons also changing

public class ExpandableListAdapter extends BaseExpandableListAdapter {

        private final Context context;

        private final List<String> headings;

        private List<DriverOrders> order;

        GroupHolder holder;

        public ExpandableListAdapter(final FragmentActivity fragmentActivity,
                                      final List<String> lidtDataHeaders, final List<DriverOrders> orders
        ) {
            this.context = fragmentActivity;
            this.headings = lidtDataHeaders;
            this.order = orders;
        }

        @Override
        public Object getChild(final int groupPosition, final int childPosition) {

            return this.headings.get(childPosition);
        }

        @Override
        public long getChildId(final int groupPosition, final int childPosition) {

            return childPosition;
        }

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

            final String childName = (String) getChild(groupPosition, childPosition);



            // GroupHolder holder;
            holder = null;

            if (convertView == null) {
                final LayoutInflater inflater = (LayoutInflater) this.context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.driver_child_layout, null);
                holder = new GroupHolder();

                holder.orderNumber = (TextView) convertView.findViewById(R.id.orderNumber);
                holder.taskType = (TextView) convertView.findViewById(R.id.taskType);
                holder.detaisl = (TextView) convertView.findViewById(R.id.detaisl);
                holder.accept = (Button) convertView.findViewById(R.id.accpet);
                holder.decline = (Button) convertView.findViewById(R.id.decline);
                holder.started = (Button) convertView.findViewById(R.id.started);
                convertView.setTag(holder);

            } else {
                holder = (GroupHolder) convertView.getTag();
            }

           /* TextView orderNumber = (TextView) convertView.findViewById(R.id.orderNumber);
            TextView taskType = (TextView) convertView.findViewById(R.id.taskType);
            TextView detaisl = (TextView) convertView.findViewById(R.id.detaisl);
            accept = (Button) convertView.findViewById(R.id.button);
            decline = (Button) convertView.findViewById(R.id.decline);

            started = (Button) convertView.findViewById(R.id.started);*/

            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("","inside convertview click"+parent.getChildAt(childPosition));
                }
            });


            holder.started.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (flag) {
                        AsyncHttpClient client = new AsyncHttpClient();
                        client.get(null, CommonUtility.SERVER_URI + "person-started/" + Long.parseLong(order.get(groupPosition).getOrderId().trim()), new JsonHttpResponseHandler() {
                            @Override
                            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                                super.onSuccess(statusCode, headers, response);

                                try {
                                    if (response.getString("STATUS").equalsIgnoreCase("true")) {

                                        mListener.onStarttrip(order.get(groupPosition).getOrderId(), order.get(groupPosition).getCustomerName(), order.get(groupPosition).getCustomerContact());
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }

                            @Override
                            public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                                super.onFailure(statusCode, headers, throwable, errorResponse);

                                if (statusCode == 404) {
                                    Toast.makeText(getActivity(), "Requested resource not found", Toast.LENGTH_LONG).show();
                                }
                                // When Http response code is '500'
                                else if (statusCode == 500) {
                                    Toast.makeText(getActivity(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
                                }
                                // When Http response code other than 404, 500
                                else {
                                    Toast.makeText(getActivity(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]", Toast.LENGTH_LONG).show();
                                }
                            }

                        });


                    } else {
                        Toast.makeText(getActivity(), "Before started you have to accept the order", Toast.LENGTH_LONG).show();
                    }

                }

            });

            holder.orderNumber.setText(order.get(groupPosition).getCustomerName());
            holder.taskType.setText(order.get(groupPosition).getCustomerContact());
            holder.detaisl.setText(order.get(groupPosition).getOrderId());

            holder.accept.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {


                    Log.e("", "group positioins" + groupPosition);
                    String associateId = sessionManager.getUserDetails().get("associate_id");
                    String oredeerid = order.get(groupPosition).getOrderId();

                    RequestParams params = new RequestParams();
                    params.put("oid", oredeerid);
                    params.put("did", associateId);
                    requestForAcceptOrder(params, groupPosition);


                }

            });
            holder.decline.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    holder.decline.setFocusable(false);
                    holder.decline.setEnabled(false);
                    holder.decline.setClickable(false);
                    sessionManager.setDclineId(order.get(groupPosition).getOrderId());
                    Toast.makeText(getActivity(), "Order is declined", Toast.LENGTH_LONG).show();
                    Intent myIntent = new Intent(getActivity(), DeclineReceiver.class);
                    PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, myIntent, 0);
                    Long timeInMillis = new GregorianCalendar().getTimeInMillis() + 1000;
                    Log.d("pass", "!!!!!!!");
                    AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
                    alarmManager.setRepeating(AlarmManager.RTC, timeInMillis, 5400000, pendingIntent);
                    notifyDataSetChanged();

                }
            });

            return convertView;
        }

        @Override
        public int getChildrenCount(final int groupPosition) {

            return 1;
        }

        @Override
        public Object getGroup(final int groupPosition) {

            return this.headings.get(groupPosition);
        }

        @Override
        public int getGroupCount() {

            return this.headings.size();
        }

        @Override
        public long getGroupId(final int groupPosition) {

            return groupPosition;
        }

        @SuppressLint("NewApi")
        @Override
        public View getGroupView(final int groupPosition,
                                 final boolean isExpanded, View convertView,
                                 final ViewGroup parent) {
            final String headerTitle = (String) this.getGroup(groupPosition);
           /* final ExpandableListView mExpandableListView = (ExpandableListView) parent;
           mExpandableListView.expandGroup(0);*/

            // to display indicator of Expandable view from left to right
            if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {

                CabOrderFragment.this.expnListView
                        .setIndicatorBounds(
                                CabOrderFragment.this.expnListView
                                        .getWidth() - 50,
                                CabOrderFragment.this.expnListView
                                        .getWidth());
            } else {
                CabOrderFragment.this.expnListView
                        .setIndicatorBoundsRelative(
                                CabOrderFragment.this.expnListView
                                        .getWidth() - 50,
                                CabOrderFragment.this.expnListView
                                        .getWidth());

            }
            final LayoutInflater infalInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(
                    R.layout.expandlistview_heading_group, null);
            // final ImageView image = (ImageView) convertView
            // .findViewById(R.id.ivHeadeImage);
            final TextView lblListHeader = (TextView) convertView
                    .findViewById(R.id.tvListHeader);
            // image.setImageResource(this.images[groupPosition]);

            lblListHeader.setTypeface(null, Typeface.BOLD);
            lblListHeader.setText(headerTitle.toUpperCase(Locale.getDefault()));
            lblListHeader.setTextSize(10);

            return convertView;
        }

        @Override
        public boolean hasStableIds() {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean isChildSelectable(final int groupPosition,
                                         final int childPosition) {
            // TODO Auto-generated method stub
            return true;
        }


        private void requestForAcceptOrder(RequestParams params, final int groupPosition) {
            AsyncHttpClient client = new AsyncHttpClient();
            client.get(null, CommonUtility.SERVER_URI + "accept", params, new JsonHttpResponseHandler() {


                @Override
                public void onSuccess(int statusCode, Header[] headers, JSONObject obj) {
                    try {
                        if (obj.getInt("code") == 0) {

                            holder.accept.setVisibility(View.GONE);
                            holder.decline.setVisibility(View.INVISIBLE);
                            holder.started.setVisibility(View.VISIBLE);
                            Toast.makeText(getActivity(), "Oeder is  Placed", Toast.LENGTH_LONG);
                            Intent myIntent = new Intent(getActivity(), DriverOrdersReceiver.class);
                            PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, myIntent, 0);
                            Log.d("stop alarm", "!!!!!!!");
                            AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
                            alarmManager.cancel(pendingIntent);
                            flag = true;

                            Log.e("", "object data" + obj.toString());

                        } else {

                            Toast.makeText(getActivity(), "Oeder is Not Placed", Toast.LENGTH_LONG);
                        }


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }


                @Override
                public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
                    super.onFailure(statusCode, headers, throwable, errorResponse);

                    if (statusCode == 404) {
                        Toast.makeText(getActivity(), "Requested resource not found", Toast.LENGTH_LONG).show();
                    }
                    // When Http response code is '500'
                    else if (statusCode == 500) {
                        Toast.makeText(getActivity(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
                    }
                    // When Http response code other than 404, 500
                    else {
                        Toast.makeText(getActivity(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]", Toast.LENGTH_LONG).show();
                    }
                }


            });


        }

        /*private view holder class*/
        private class GroupHolder {
            TextView orderNumber;
            TextView taskType;
            TextView detaisl;
            Button accept;
            Button decline;
            Button started;

        }


    }
Rami
  • 7,879
  • 12
  • 36
  • 66
Ramesh Reddy
  • 59
  • 12

2 Answers2

0

Your problem is due to Recycling mechanism, you need to reset the text color in getChildView() method.

This can be done by an if{}else{} case.

eg:

if(yourTextMustTakeTheDefaultColor){
   // set the default color in your text
} else {
   // set the other color
}

UPDATE:

1) First don't use the holder as class variable.

Remove the class variable GroupHolder holder; from your adapter.

And change your getChildView() code like that:

GroupHolder holder;
if (convertView == null) {
    final LayoutInflater inflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.driver_child_layout, null);
    holder = new GroupHolder();
    //the rest of code...
    convertView.setTag(holder);
} else {
    holder = (GroupHolder) convertView.getTag();
}
//the rest of code...

2) Like i've already told you to do: add if else case in getChildView() to update your view (i think you want to update accept/declined/started visibility) depending on child data.

eg:

if(order.get(groupPosition).isAcceptedVisible()){
   holder.accept.setVisibility(View.VISIBLE);
} else {
   holder.accept.setVisibility(View.GONE);
}

3) Change your requestForAcceptOrder() to pass the also the child position.

private void requestForAcceptOrder(RequestParams params, final int groupPosition, final int childPosition)

4) In onSuccess() method you have the groupPosition and childPosition, update your List's data/model and call notifyDataSetChanged();, the system will take care of updating your views.

Community
  • 1
  • 1
Rami
  • 7,879
  • 12
  • 36
  • 66
  • why text color has to do anything in this? i am also busting my head on this problem as i am stuck on this too, but your solution has nothing to do with this problem, read once more dude... Thing is that if we do make any changes in one child_view then during scrolling or even not scrolling(in my case) it updates other child_views simultaneously.. now problem should be clear.! :/ help if u know...! – mfaisalhyder Nov 12 '15 at 07:05
  • Okay but how can i use if and else conditions here.my button is accept button in above code .For example i am clicking on gropuposition 1 and in that am clicking on child item button (accept) ,then am changing button there,this whole logic is in accept.setOnClickListener() method.Please look into above code accept onClick Listener – Ramesh Reddy Nov 12 '15 at 07:22
  • yeah correct @M Faisal Hyder...My problem is same as yours...it updating to other child items even in case of not scrolling the view – Ramesh Reddy Nov 12 '15 at 07:26
  • @MFaisalHyder in the OP he said *"Here I am clicking on accept button of one child ,then it should change to another button but **while scrolling** all child items buttons also changing"* and i'am talking about text color because he mentioned this on [his duplicated question](http://stackoverflow.com/questions/33628104/button-with-onclick-listeners-in-expandable-lisitview-affects-getchildview-metho) – Rami Nov 12 '15 at 08:14
  • @RameshReddy what "requestForAcceptOrder()" method do? – Rami Nov 12 '15 at 08:20
  • the "requestForAcceptOrder()" method will get the data from server and in that method i have written code for changing text on button ,from "accpet" to "started" But this is effecting all accept buttons in other child items – Ramesh Reddy Nov 12 '15 at 08:55
  • @RameshReddy can you please post the code of this method? – Rami Nov 12 '15 at 09:06
  • @Rami i have posted the "requestForAcceptOrder()" method ,please look into that in above code – Ramesh Reddy Nov 12 '15 at 09:51
  • @RameshReddy you're using *holder* object in onSuccess() method, but in your code don't see from where you get this *holder*?? – Rami Nov 12 '15 at 09:56
  • @Rami ,can i post that holder class ? – Ramesh Reddy Nov 12 '15 at 10:01
  • @RameshReddy I know you have a holder class, i'am talking about the holder object/instance... You should update the child view/holder not the the holder class. – Rami Nov 12 '15 at 10:02
  • @Rami Now i posted full code for ExpandableListviewAdapter ,please look into that .Suggest me how to update holder insatnce in that – Ramesh Reddy Nov 12 '15 at 10:17
  • Thanks @Rami But what isAcceptedVisible() method contains and if i dont decalre Holder as field variable ,how can i access holder instance inside OnClickListeners of accept button...Please suggest me here – Ramesh Reddy Nov 12 '15 at 11:52
  • @RameshReddy isAcceptedVisible() (or name it as you want) is a method in your Model class that will return boolean value to know if Child should hide or not the accepted button. Don't update the holder inside you ClickListeners, you should update your data list (the child model), then call *notifyDataSetChanged()* to refresh the views. Maybe you also need to add isDeclined() and isStarted() methods in your model to save declined and started state. – Rami Nov 12 '15 at 12:05
  • I think this is the last comment for this Question.Thanks @Rami..Its Wworking fine now..Thanks a lot.. – Ramesh Reddy Nov 12 '15 at 13:37
0

From what I can see the problem is that you aren't using the view in your onclick method to make the changes required in your UI. So the problem is context here. Holder object is created and destroyed for each group and you should use v from public void onClick(View v) to make any changes to the UI always, be it for the click on the group header of child views.

PS. I would suggest to have the Async task change data in a Model and just call notifydatasetchanged() on the adapter on returning a success.

Cheers

EmptyCup
  • 421
  • 4
  • 14