Upsilon42
Upsilon42

Reputation: 241

How to use the inner, static PlaceholderFragment class which is automaticly created in Android studio?

In Android studio, when I create a new Activity with a fragment, it makes a inner static class in my activity. The problem is that, because it's a static and inner class, I can't do much with it.

    /**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {


    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //ArrayList<String> contactList=new ArrayList<String>();
        View rootView = inflater.inflate(R.layout.fragment_search, container, false);
        //contactList.add("One test);

        //ArrayAdapter <String> arrayAdapter= new ArrayAdapter<String> (getActivity(),R.layout.list_item_forecast,
        //        R.id.list_item_forecast_textview,contactList);
        //ListView lv= (ListView)rootView.findViewById(R.id.listview_forecast);
        //lv.setAdapter(arrayAdapter);
        return rootView;
    }
}

The code I commented is what I added, and works. But I would like to do more: add listener to that ListView items, etc..

If Andoid studio put the placeholderFragment as a static inner class, it means that it's a good and easy way, but I don't know how to continue it.

So, to be more specific, for example, How can a put a listener for the listview items? Thanks a lot.

Upvotes: 1

Views: 1060

Answers (1)

Sweder Schellens
Sweder Schellens

Reputation: 480

The PlaceholderFragment should not be used. It is a placeholder only. If you add your own fragments, you should finally delete the place holder.

You should create a Fragment class of your own (via new->fragment) and add your logic to it. In your activity, implement the methods to be called when an item in your drawer is clicked. Then, implement the logic to activate your fragment.

If you need more information or code examples, please let me know.

Upvotes: 2

Related Questions