Question

I have three pages in a FragmentPagerAdapter, and I try to catch the longClick event from a different fragment (not in the pageradapter) and through interfacing use this event to change the text in a textview. It worked to some degree when I saved the fragments in an arraylist in my MainActivity, but then I tried to remove the arraylist and grab the existing fragments from the pageradapter to fix a problem I had with fragments disappearing from my arraylist, and now the textview won't change at all (it would change part of the time before).

Here is the relevant parts (according to Tushar's request I bring only the most relevant parts, if anything else is needed to understand the code please mention it and I will add it):

    @Override
    public void onLocationSelected(String location) {
    setCurrentLocation(location);
    Fragment frag;
    for (int i = adapter.getCount() - 1; i >= 0; i--) {
        frag = adapter.getItem(i);
        if (frag instanceof LocationChangedListener) {
        ((LocationChangedListener) frag).onLocationChanged();
        }
    }
    }

    private class WeatherAppPagerAdapter extends FragmentPagerAdapter {

    private String[] titles;

    public WeatherAppPagerAdapter(FragmentManager fm) {
        super(fm);
        titles = getResources().getStringArray(R.array.titles);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
        case 0:
        return new OptionsFrag();
        case 1:
        return new ConditionsFrag();
        case 2:
        return new ForcastFrag();
        default:
        return null;
        }
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }

    }

Hopefully this helps you help me, because my TextView is in desperate need for some text.

EDIT: Here's wher the TextView get's the text:

@Override
public void onLocationChanged() {
if (view == null) {
    return;
}
((TextView) view.findViewById(R.id.tvLocationName)).setText(mainActivity
    .getCurrentLocation());
}
Was it helpful?

Solution

Okay, the solution I found was to set the text from the activity, I guess the view is inaccessible from the fragment. No matter how I tried to get it, the view was always null. It means giving different id's to widgets, but at least it works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top