1

I have craeted a CustomAdapter class that extends BaseAdapter (so I cannot extend the class to Fragment).

In CustomAdapter class I create my custom ListView with items. Uppon button clicked I would like to go another Fragment.

CustomAdapter class is being initialized in MyLocationFragment.java. In this Fragment I offer the user to select an Item from ListView, and when the user clicks the Button (which is next to the TextView / Item) I want to go to HomeScreenFragment and do something with the stored value from the TextView that the Button belonged to (same position).

MyLocationFragment (parts that matter in this case, otherwise too big):

if(adminMode) {
            ArrayList<String> data = new ArrayList<>(Arrays.asList(locationsArray));
            CustomAdapter adapter = new CustomAdapter(data, getActivity());

            // Assign adapter to ListView
            listView.setAdapter(adapter);

        }

CustomAdapter.java (partially,where the Listeners comes in):

holder.localData.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        fMyLocation = new MyLocationFragment();
        // Store selected values into Strings and pass them to setter method
        String selectedLocation = (String) holder.items.getText().toString();
        String [] locationsArrayValues = v.getResources().getStringArray(R.array.locations_array_value);
        String selectedLocationUrl = locationsArrayValues[position];
        fMyLocation.setInputLocation(selectedLocation);
        fMyLocation.setInputLocationUrl(selectedLocationUrl);

        FragmentActivity myContext;

        // On Item Click, go to the HomeScreen
        Fragment fragment = new HomeScreenFragment();
        FragmentManager fManager = myContext.getSupportFragmentManager();
        FragmentTransaction fTransaction = fManager.beginTransaction();
        fTransaction.replace(R.id.fragment_holder, fragment);
        fTransaction.commit();






    }
});

The above code results into Cannot Resolve getFragmentManager();

Any idea how could I get to the HomeScreenFragment from non-Fragment class ?

Any help will be appreciated.

David Kasabji
  • 1,049
  • 3
  • 15
  • 32
  • You could create a method requiring the object you want to pass on your activity which you call from your adapter, which then fires a new fragment from the activity. There is nested fragment support in android 4.2 and later but you cut off some 30% of android users from your app that way. – G_V Dec 15 '14 at 12:21
  • Not sure if I understand it correctly. But isn't there any easier method? I mean ListViews are used alot, aren't they used inside Fragments or what? Can't find tutorials on this. I just want to store a value uppon a Button Click from ListView and use it other Fragment. – David Kasabji Dec 15 '14 at 12:25
  • You can use the support library to get access to getChildFragmentManager too but personally I think nested fragments should only be used if you're actually nesting something visually in the GUI. Really though, the above method is 1. Create method on activity 2. Pass activity to Adapter (use SoftReference) 3. call activity().yourMethod(yourObject) 4. Open new activity/fragment – G_V Dec 15 '14 at 12:56

1 Answers1

0

What you should do is that in the class which implements BaseAdapter create a field for Context and use that instead of what you have done. Initialization of that field will be done through the constructor.

upenpat
  • 685
  • 3
  • 12
  • I do have field Context in my CustomAdapter class, but this does not help meto accomplish what I need. If I add `context.getFragmentManager()`nothing happens. The context is being passed in the constructor of the CustomAdapter class. – David Kasabji Dec 15 '14 at 12:20
  • you mean to say that `myContext` in the above piece of code is the same one that is initialized via constructor? And btw are using the support library? Since in your code you have written `getSupportFragmentManager` but the error comes as `Cannot Resolve getFragmentManager();`. I suggest you organize the imports. – upenpat Dec 15 '14 at 12:26
  • No, it isn't that was just me testing something out if it might work. The Context in constructor is `Context ctx`. In general: No I dont use support library for the FragmentManager, that was also a test if it might work that way – David Kasabji Dec 15 '14 at 12:28
  • `class CustomAdapter{Context mContext; ......... }` Now where you write that `listView.setAdapter(yourAdapter);listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { Toast.makeText(getApplicationContext(), "Click ListItem Number " + position, Toast.LENGTH_LONG) .show(); } });` – upenpat Dec 15 '14 at 12:36
  • I understand your propossal, but I have two buttons in my ListView. And have their on click listeners inside CustomAdapter class. Should I somehow transfer them inside Fragment instead ? – David Kasabji Dec 15 '14 at 12:39
  • okay...in that case I suggest you take a look here http://stackoverflow.com/a/1776328/3405876 – upenpat Dec 15 '14 at 12:42