1

I am creating an expandable layout that has a header and child. I am passing information to both with strings.xml. However, it seems to overwrite my website and phone number with the latest section added. For example I want the first section to have activity1_website but when I added the second section it overwrote my website to activity2_website. Thanks in advance for the help.

public class ActivitiesActivity extends AppCompatActivity {

public int activity_name_int;
public int activity_phone_int;
public int activity_website_int;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activities);


    ExpandableLayout layout = (ExpandableLayout) findViewById(R.id.activities_expandable_layout);
    layout.setRenderer(new ExpandableLayout.Renderer<ActivitiesHeader, ActivitiesChild>() {
        @Override
        public void renderParent(View view, ActivitiesHeader activitiesHeader, boolean b, int i) {
            ((TextView) view.findViewById(R.id.activity_names)).setText(activitiesHeader.activity_name);
        }

        @Override
        public void renderChild(View view, ActivitiesChild activitiesChild, int i, int i1) {
            ((TextView) view.findViewById(R.id.textview_website)).setText(activitiesChild.website);

            ((TextView) view.findViewById(R.id.textview_website)).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Uri uri = Uri.parse(getResources().getString(activity_website_int)); // missing 'http://' will cause crashed
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                }
            });

            ((TextView) view.findViewById(R.id.textview_phone)).setText(activitiesChild.phone);
            ((TextView) view.findViewById(R.id.textview_phone)).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(ActivitiesActivity.this, activity_phone_int, Toast.LENGTH_SHORT).show();
                }
            });
        }
    });


    layout.addSection(getSection((R.string.activity1_name), (R.string.activity1_website), (R.string.activity1_phone_number)));
    layout.addSection(getSection((R.string.activity2_name), (R.string.activity2_website), (R.string.activity2_phone_number)));
}


// === Creates the List of Activities ===
private Section<ActivitiesHeader,ActivitiesChild> getSection(int name, int website, int phone) {
    activity_name_int = name;
    activity_website_int = website;
    activity_phone_int = phone;

    String activity_name = getResources().getString(activity_name_int);
    String activity_website = getResources().getString(activity_website_int);
    String activity_phone = getResources().getString(activity_phone_int);


    Section<ActivitiesHeader, ActivitiesChild> section = new Section<>();
    ActivitiesHeader activitiesHeader = new ActivitiesHeader(activity_name);


    List<ActivitiesChild> listActivities = new ArrayList<>();
    listActivities.add(new ActivitiesChild(activity_website,activity_phone));
    section.parent = activitiesHeader;
    section.children.addAll(listActivities);


    return section;
}
}

public class ActivitiesHeader {
public String activity_name;

    public ActivitiesHeader(String activity_name) {
        this.activity_name = activity_name;
    }
}

public class ActivitiesChild {
public String phone;
public String website;

public ActivitiesChild(String website, String phone) {
        this.phone = phone;
        this.website = website;
    }
}

0 Answers0