0

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());
}
  • 1
    Please only post the relevant parts of the code. – Tushar Jan 16 '13 at 19:25
  • @Tushar, only the most relevant code is left now. If anything else needed to understand the code tell me and I will add it. –  Jan 16 '13 at 19:38
  • Where exactly are you setting the TextView? If it's in setCurrentLocation, please post that as well. – Tushar Jan 16 '13 at 19:43
  • it's in xml layout file. setCurrentLocation is a simple setter, no more: public void setCurrentLocation(String currentLocation) { this.currentLocation = currentLocation; } –  Jan 16 '13 at 19:49
  • Are you actually calling setText() at any point though? Post that code. – Tushar Jan 16 '13 at 19:52
  • @Tushar I posted the setText() calling function. –  Jan 16 '13 at 20:07
  • 1
    Have you done some debugging to see if it actually makes it into the onLocationChanged() and then past the `view == null`? Could you just add some log statements and see where it is stopping? – Tushar Jan 16 '13 at 20:12
  • Okay, it never passes the view == null test, but if I remove it the app crashes. Also, at least one view should be alive, the one on the screen. No? –  Jan 16 '13 at 20:22
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/22859/discussion-between-tushar-and-yekhezkel-yovel) – Tushar Jan 16 '13 at 20:27

1 Answers1

0

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.