Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to pass "title" data from addItems to another activity? #21

Closed
yourtechcode opened this issue Mar 10, 2018 · 5 comments
Closed

How to pass "title" data from addItems to another activity? #21

yourtechcode opened this issue Mar 10, 2018 · 5 comments

Comments

@yourtechcode
Copy link

yourtechcode commented Mar 10, 2018

I am able to pass data of subItem but i am not able to pass title data. I have to assign it in a String Variable but it catches the last data from addItem i.e Bart. Sorry for the noob question i am new to the android development @diegodobelo

@diegodobelo
Copy link
Owner

Could you please send snipet of your code?
Also, were you able to run the sample code in this repository?

@yourtechcode
Copy link
Author

yourtechcode commented Mar 10, 2018

private void createItems() {
addItem("John", new String[]{"House", "Boat", "Candy", "Collection", "Sport", "Ball", "Head"}, R.color.pink, R.drawable.ic_ghost);
addItem("Mary", new String[]{"Dog", "Horse", "Boat"}, R.color.blue, R.drawable.ic_ghost);
addItem("Ana", new String[]{"Cat"}, R.color.purple, R.drawable.ic_ghost);
addItem("Peter", new String[]{"Parrot", "Elephant", "Coffee"}, R.color.yellow, R.drawable.ic_ghost);
addItem("Joseph", new String[]{}, R.color.orange, R.drawable.ic_ghost);
addItem("Paul", new String[]{"Golf", "Football"}, R.color.green, R.drawable.ic_ghost);
addItem("Larry", new String[]{"Ferrari", "Mazda", "Honda", "Toyota", "Fiat"}, R.color.blue, R.drawable.ic_ghost);
addItem("Moe", new String[]{"Beans", "Rice", "Meat"}, R.color.yellow, R.drawable.ic_ghost);
addItem("Bart", new String[]{"Hamburger", "Ice cream", "Candy"}, R.color.purple, R.drawable.ic_ghost);
}

private void addItem(String title, String[] subItems, int colorRes, int iconRes) {
    //Let's create an item with R.layout.expanding_layout
    final ExpandingItem item = mExpandingList.createNewItem(R.layout.expanding_layout);

    //If item creation is successful, let's configure it
    if (item != null) {
        item.setIndicatorColorRes(colorRes);
        item.setIndicatorIconRes(iconRes);
        //It is possible to get any view inside the inflated layout. Let's set the text in the item
        ((TextView) item.findViewById(R.id.title)).setText(title);

        //We can create items in batch.
        item.createSubItems(subItems.length);
        for (int i = 0; i < item.getSubItemsCount(); i++) {
            //Let's get the created sub item by its index
            final View view = item.getSubItemView(i);

            //Let's set some values in
            configureSubItem(item, view, subItems[i]);
        }



    }
}

private void configureSubItem(final ExpandingItem item, final View view, final String subTitle) {
    ((TextView) view.findViewById(R.id.sub_title)).setText(subTitle);
    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(),subTitle,Toast.LENGTH_LONG).show();
        }
    });
}`

in this code whenever i pressed something from subItem it shows me the toast message of the click subitem for example if i clicked on 'house' it shows me house in toast. how can I do same with title one? i mean whenever i clicked something from subitem then it will show me the title in which the subitem is present. for example if i clicked on 'dog' which is present in title "Mary" then the toast message will show me "Mary"
Thank you for your time and helping me!
@diegodobelo

@yourtechcode
Copy link
Author

Please need your help sorry for the noob question @diegodobelo

@diegodobelo
Copy link
Owner

diegodobelo commented Mar 10, 2018

Like this:

private void configureSubItem(final ExpandingItem item, final View view, final String subTitle) {
        ((TextView) view.findViewById(R.id.sub_title)).setText(subTitle);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String title = ((TextView) item.findViewById(R.id.title)).getText().toString();
                String text = title + " - " + subTitle;
                Toast.makeText(getApplicationContext(),text,Toast.LENGTH_LONG).show();
            }
        });
    }

@yourtechcode
Copy link
Author

Thanks, alot sir. I really mean it for sparing your precious time and effort to solve my problem!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants