0

I have two question about expandablelistview click events on groupView's (not child)

  1. When i have child in my GroupView, i'm able to get the groupPosition using below snippet

    expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
            Log.d("Clicked on item: ", ""+groupPosition);
            return false;
        }
    });
    

but when i don't have any child, Unfortunately 'MyApp' has stopped on mouse click. In this case, How to get the groupPosition?

  1. I have two buttons in my expandablelistview's groupView layout. now i need to register two different click events for both the buttons. This groupView also didn't has child. I searched in SO thoroughly, but i got no clue on this.

        <LinearLayout
            android:layout_below="@id/qtd_prc_cont"
            android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center">
            <Textview
                android:layout_width="wrap_content" android:layout_height="wrap_content"  android:id="@+id/group_text" android:text="Group 1" />
            <Button
                android:id="@+id/btn_reject"
                android:layout_width="90dp" android:layout_height="30dp" android:text="Reject" android:textAllCaps="false" android:background="@drawable/reject_button_styles"
                android:layout_marginRight="60dp" android:textColor="#a1a2a1" android:textSize="13dp"/>
            <Button
                android:id="@+id/btn_accept"
                android:layout_width="90dp" android:layout_height="30dp" android:text="Accept" android:textAllCaps="false" android:background="@drawable/button_radius"
                android:textColor="@color/colorWhite" android:textSize="13dp"/>
        </LinearLayout>
    

Here's my adapter class:

public class OfferedTrucksAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<QuotedListGroup> groups;

public OfferedTrucksAdapter(Context context, ArrayList<QuotedListGroup> groups) {
    this.context = context;
    this.groups = groups;
}

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

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

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

    QuotedListItem child = (QuotedListItem) getChild(groupPosition, childPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.activity_quoted_list_item, null);
    }
    TextView container_label = (TextView) convertView.findViewById(R.id.container);
    container_label.setText(child.getContainer());

    TextView truck_number = (TextView) convertView.findViewById(R.id.truck_number);
    truck_number.setText(child.getTruckNumber().toString());

    TextView truck_weight = (TextView) convertView.findViewById(R.id.truck_weight);
    truck_weight.setText(child.getTruckWeight().toString());

    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    ArrayList<QuotedListItem> 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) {
    QuotedListGroup group = (QuotedListGroup) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater inf = (LayoutInflater) context
                .getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = inf.inflate(R.layout.activity_offered_trucks_group, null);
    }

    if(groupPosition%2 == 0) {
        convertView.findViewById(R.id.offered_trucks_group).setVisibility(View.VISIBLE);
        convertView.findViewById(R.id.offered_price_group).setVisibility(View.GONE);

        TextView destination = (TextView) convertView.findViewById(R.id.offered_destination);
        destination.setText(group.getDestination());

        TextView trucks = (TextView) convertView.findViewById(R.id.offered_trucks);
        trucks.setText(group.getTrucks() + "");

        TextView date_label = (TextView) convertView.findViewById(R.id.offered_date_label);
        date_label.setText(group.getDateLabel().toString());

        TextView material = (TextView) convertView.findViewById(R.id.offered_material);
        material.setText(group.getMaterial().toString());

        TruckaroIconView see_hide_icon = (TruckaroIconView) convertView.findViewById(R.id.offered_see_hide_icon);
        TextView see_hide_label = (TextView) convertView.findViewById(R.id.offered_see_hide_label);
        if(isExpanded) {
            see_hide_label.setText("Hide Trucks");
            see_hide_icon.setRotation(180);
        } else {
            see_hide_label.setText("See Trucks");
            see_hide_icon.setRotation(0);
        }
    } else {
        convertView.findViewById(R.id.offered_trucks_group).setVisibility(View.GONE);
        convertView.findViewById(R.id.offered_price_group).setVisibility(View.VISIBLE);

        TextView offeredPrice = (TextView) convertView.findViewById(R.id.offered_price);
        offeredPrice.setText(group.getOfferedPrice());

        TextView quotedPrice = (TextView) convertView.findViewById(R.id.quoted_price);
        quotedPrice.setText(group.getQuotedPrice());

    }
    return convertView;
}

protected static class RowViewHolder {
    public Button buttonReject, buttonAccept;
}

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

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

}

Based on accept/reject button clicks, i need to pass the Textview's data to next activity through intent.

Here is how my expandable listview looks like: enter image description here

In the above image, there are four list items. 1st and 3rd have children but 2nd and 4th has no child. When i click on 2nd element accept button, i need to pass the 1st elemnt data to next activity. I'm unable to set click events on those buttons.

Please help me out of this.

Thanks in advance.

Mr.7
  • 2,292
  • 1
  • 20
  • 33
  • What error did you receive when your app crashed? – AL. May 06 '16 at 07:27
  • @McAwesomville I'm not getting any error. It is simply going back to previous activity. and it's working fine, when i add child to the same group. Does expandable listview's group element can't be empty(without child)? – Mr.7 May 06 '16 at 07:29
  • What was the **Unfortunately 'MyApp' has stopped** ? – AL. May 06 '16 at 07:30
  • Sorry. Tis a little bit confusing. As how I understand it, you are having trouble retrieving the `groupPosition` value when the specific `groupView` has no `childView`s? – AL. May 06 '16 at 07:32
  • @McAwesomville Without any error, it's simply leaving the message **Unfortunately MyAPp has stopped.** (FYI 'MyApp' is my app name) – Mr.7 May 06 '16 at 07:35
  • Exactly @McAwesomville – Mr.7 May 06 '16 at 07:36
  • I'm having a hard time picturing how your flow goes, can you post some screenshots? It would help a lot. – AL. May 06 '16 at 07:38
  • @McAwesomville I have updated pictures, can check it once? – Mr.7 May 06 '16 at 09:12
  • So to verify. In the screenshot you provided. There are four `GroupView` Items. 1st and 3rd have `Child` items (I think it's the See Trucks?). And the *Our Price* Items are the ones you are referring to as the ones with no `Child`s. If this is the case, I think you need to modify this in your `Adapter` class. Set the `onClickListener()` of those buttons. The thing that's confusing me here is that why pass the value from the upper `GroupView` item? – AL. May 06 '16 at 09:55
  • @McAwesomville i tried onClickListener() using button id. But when i click on 2nd item's accept button, it's returning 4th items button(i.e, last element button). I think the button's id's are overriding. Does it happens? – Mr.7 May 06 '16 at 10:05
  • @McAwesomville FYI, what you understood about my question is correct. – Mr.7 May 06 '16 at 10:11
  • Hmm. It seems that it's retrieving the wrong ID, maybe retrieving the wrong value. Try adding logs, see the value you are retrieving for the ID. I'd take a look at this more when I get back. Cheers! :D – AL. May 06 '16 at 14:51
  • Can you post your adapter class? – AL. May 09 '16 at 06:37
  • @McAwesomville I have posted my adapter class, please check it once. Thank you. – Mr.7 May 09 '16 at 07:08

1 Answers1

1

Okay. If I understand your concern correctly, the behavior you're aiming for is to pass an element of the upper item (in your screenshot, if Accept button is clicked, a value from the upper item will be retrieved and passed to another activity through an intent.

Here's what I got:

@Override
    public View getGroupView(final int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        QuotedListGroup group = (QuotedListGroup) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater inf = (LayoutInflater) context
                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = inf.inflate(R.layout.activity_offered_trucks_group, null);
        }

        if (groupPosition % 2 == 0) {

            // Removed some code here.

        } else {
            convertView.findViewById(R.id.offered_trucks_group).setVisibility(View.GONE);
            convertView.findViewById(R.id.offered_price_group).setVisibility(View.VISIBLE);

            TextView offeredPrice = (TextView) convertView.findViewById(R.id.offered_price);
            offeredPrice.setText(group.getOfferedPrice());

            TextView quotedPrice = (TextView) convertView.findViewById(R.id.quoted_price);
            quotedPrice.setText(group.getQuotedPrice());

            // Added code here ----

            // Initialize the buttons.
            Button btnAccept = (Button) convertView.findViewById( < insert_btn_id_here >);
            Button btnReject = (Button) convertView.findViewById( < insert_btn_id_here >);

            // Set onClickListeners for the buttons.
            btnAccept.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    /*
                    Modify with the object you have, then pass the group position - 1
                    (double check this, you just need to pass the position of the upper item).
                    */
                    Object groupObject = getGroup(groupPosition - 1);

                    /*
                    Get the value from the groupObject that you want to include in the intent.
                    NOTE: I'm just using some sample codes, modify it to what you need.
                    */
                    String sampleStr = groupObject.destination.getText().toString();

                    /*
                    Intent stuff, I think you know this already.
                     */

                    Intent intent = new Intent(context, SampleActivity.class);
                    intent.putExtra("SAMPLE_KEY", sampleStr);
                    context.startActivity(intent);

                }
            });

            btnReject.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // your code for reject button here.
                }
            });

            // Until here ----

        }

        return convertView;
    }

I simply added the listeners for your buttons (accept, reject) inside the else statement. You need to modify it to what you need since I don't have visibility with the rest of your code. Let me know if you have questions. Cheers! :D

AL.
  • 36,815
  • 10
  • 142
  • 281
  • PS: Do look at my comments inside the code snippet. ;) – AL. May 09 '16 at 07:50
  • Thank you very much for you help and i have a doubt. now i'm getting groupObject, but how can i get the destination property? Using .(dot) notation, i'm getting this error: cannot find symbol variable destination – Mr.7 May 09 '16 at 09:04
  • As I see it. You already have the `RowViewHolder` static class. You can utilize it. Seeing your adapter, I think you can optimize it more. This [post](http://stackoverflow.com/q/22583689/4625829) looks good. After you manage to utilize the `ViewHolder` object, you can call it as you would normally do: `ViewHolder vh = getGroup(groupPosition - 1);` then `vh.destination.getText().toString();`. Something like that. Good luck mate. Cheers! :D – AL. May 09 '16 at 09:12